Denmaseno 7 years ago
commit
f7fe75e752
2 changed files with 260 additions and 0 deletions
  1. 83 0
      formatter.go
  2. 177 0
      slog.go

+ 83 - 0
formatter.go

@@ -0,0 +1,83 @@
+package slog
+
+import (
+	"bytes"
+	"fmt"
+
+	"github.com/sirupsen/logrus"
+)
+
+const (
+	nocolor = 0
+	red     = 31
+	green   = 32
+	yellow  = 33
+	blue    = 34
+	gray    = 39
+)
+
+var (
+	formatParams = map[logrus.Level]formatParam{
+		logrus.DebugLevel: formatParam{
+			color:     blue,
+			levelText: "DEBUG",
+		},
+		logrus.InfoLevel: formatParam{
+			color:     green,
+			levelText: "INFO",
+		},
+		logrus.WarnLevel: formatParam{
+			color:     yellow,
+			levelText: "WARN",
+		},
+		logrus.ErrorLevel: formatParam{
+			color:     red,
+			levelText: "ERROR",
+		},
+	}
+)
+
+type (
+	formatter struct {
+		logrus.TextFormatter
+	}
+
+	formatParam struct {
+		color     int
+		levelText string
+	}
+)
+
+func newFormatter(color bool) *formatter {
+	return &formatter{
+		logrus.TextFormatter{
+			ForceColors:   color,
+			DisableColors: !color,
+		},
+	}
+}
+
+/**************************
+ formatter function
+**************************/
+func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) {
+	formatParams := formatParams[entry.Level]
+	b := &bytes.Buffer{}
+	if f.ForceColors && !f.DisableColors {
+		fmt.Fprintf(b,
+			"\x1b[%dm[%-5s]\x1b[0m %s",
+			formatParams.color,
+			formatParams.levelText,
+			entry.Message,
+		)
+	} else {
+		fmt.Fprintf(b,
+			"[%-5s] %s",
+			formatParams.levelText,
+			entry.Message,
+		)
+	}
+
+	b.WriteByte('\n')
+	return b.Bytes(), nil
+}

+ 177 - 0
slog.go

@@ -0,0 +1,177 @@
+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...)
+}