123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- package slog
- import (
- "os"
- "strings"
- logrus_stack "github.com/Gurpartap/logrus-stack"
- "github.com/mattn/go-colorable"
- "github.com/sirupsen/logrus"
- )
- type logger struct {
- *logrus.Logger
- }
- var rootLogger *logger
- func init() {
- rootLogger = newRootLogger()
- logrus.AddHook(logrus_stack.NewHook(nil, []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.WarnLevel}))
- logrus.SetLevel(logrus.InfoLevel)
- }
- func newRootLogger() *logger {
- switch strings.ToLower(os.Getenv("LOG_LEVEL")) {
- case "panic":
- logrus.SetLevel(logrus.PanicLevel)
- case "fatal":
- logrus.SetLevel(logrus.FatalLevel)
- case "error":
- logrus.SetLevel(logrus.ErrorLevel)
- case "warn", "warning":
- logrus.SetLevel(logrus.WarnLevel)
- case "info":
- logrus.SetLevel(logrus.InfoLevel)
- case "debug":
- logrus.SetLevel(logrus.DebugLevel)
- default:
- logrus.SetLevel(logrus.InfoLevel)
- }
- logrus.SetOutput(colorable.NewColorableStdout())
- logrus.SetFormatter(newFormatter(true))
- return &logger{logrus.StandardLogger()}
- }
- // SetLevelToDebug func
- func SetLevelToDebug() {
- logrus.SetLevel(logrus.DebugLevel)
- }
- // SetLevelToInfo func
- func SetLevelToInfo() {
- logrus.SetLevel(logrus.InfoLevel)
- }
- // SetLevelToWarn func
- func SetLevelToWarn() {
- logrus.SetLevel(logrus.WarnLevel)
- }
- // Debugf func
- func Debugf(format string, args ...interface{}) {
- if rootLogger.Level >= logrus.DebugLevel {
- if len(args) == 1 {
- if fx, ok := args[0].(func() interface{}); ok {
- rootLogger.Debugf(format, fx())
- return
- }
- if fx, ok := args[0].(func() []interface{}); ok {
- rootLogger.Debugf(format, fx()...)
- return
- }
- }
- rootLogger.Debugf(format, args...)
- }
- }
- // Infof func
- func Infof(format string, args ...interface{}) {
- if rootLogger.Level >= logrus.InfoLevel {
- if len(args) == 1 {
- if fx, ok := args[0].(func() interface{}); ok {
- rootLogger.Infof(format, fx())
- return
- }
- if fx, ok := args[0].(func() []interface{}); ok {
- rootLogger.Infof(format, fx()...)
- return
- }
- }
- rootLogger.Infof(format, args...)
- }
- }
- // Warnf func
- func Warnf(format string, args ...interface{}) {
- if len(args) == 1 {
- if fx, ok := args[0].(func() interface{}); ok {
- rootLogger.Warnf(format, fx())
- return
- }
- if fx, ok := args[0].(func() []interface{}); ok {
- rootLogger.Warnf(format, fx()...)
- return
- }
- }
- rootLogger.Warnf(format, args...)
- }
- // Errorf func
- func Errorf(format string, args ...interface{}) {
- if len(args) == 1 {
- if fx, ok := args[0].(func() interface{}); ok {
- rootLogger.Errorf(format, fx())
- return
- }
- if fx, ok := args[0].(func() []interface{}); ok {
- rootLogger.Errorf(format, fx()...)
- return
- }
- }
- rootLogger.Errorf(format, args...)
- }
- // Panicf func
- func Panicf(format string, args ...interface{}) {
- if len(args) == 1 {
- if fx, ok := args[0].(func() interface{}); ok {
- rootLogger.Panicf(format, fx())
- return
- }
- if fx, ok := args[0].(func() []interface{}); ok {
- rootLogger.Panicf(format, fx()...)
- return
- }
- }
- rootLogger.Panicf(format, args...)
- }
- // Debug func
- func Debug(args ...interface{}) {
- if rootLogger.Level >= logrus.DebugLevel {
- if len(args) == 1 {
- if fx, ok := args[0].(func() interface{}); ok {
- rootLogger.Debug(fx())
- return
- }
- if fx, ok := args[0].(func() []interface{}); ok {
- rootLogger.Debug(fx()...)
- return
- }
- }
- rootLogger.Debug(args...)
- }
- }
- // Info func
- func Info(args ...interface{}) {
- if rootLogger.Level >= logrus.InfoLevel {
- rootLogger.Info(args...)
- }
- }
- // Warn func
- func Warn(args ...interface{}) {
- rootLogger.Warn(args...)
- }
- // Error func
- func Error(args ...interface{}) {
- rootLogger.Error(args...)
- }
- // Panic func
- func Panic(args ...interface{}) {
- rootLogger.Panic(args...)
- }
|