diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..68066d3 --- /dev/null +++ b/log/log.go @@ -0,0 +1,165 @@ +package log + +import ( + "fmt" + "os" + "runtime" + "strings" + + "github.com/sirupsen/logrus" + "github.com/x-cray/logrus-prefixed-formatter" +) + +type ( + Fields logrus.Fields +) + +var ( + mylog *logrus.Logger + fn bool +) + +func init() { + mylog = logrus.New() + mylog.Formatter = &logrus.JSONFormatter{} + mylog.Out = os.Stdout + mylog.Level = logrus.DebugLevel + + mylog.Formatter = &prefixed.TextFormatter{ + ForceFormatting: true, + QuoteEmptyFields: true, + TimestampFormat: "2006-01-02 15:04:05", + FullTimestamp: true, + ForceColors: true, + } + + fn = true +} + +func SetFn(val bool) { + fn = val +} + +func SetLogLevel(level logrus.Level) { + mylog.Level = level +} + +func SetLogFormatter(formatter logrus.Formatter) { + mylog.Formatter = formatter +} + +func NewLog(prefix string) *logrus.Entry { + return mylog.WithField("prefix", prefix) +} + +func fileInfo(skip int) string { + _, file, line, ok := runtime.Caller(skip) + if !ok { + file = "" + line = 1 + } else { + slash := strings.LastIndex(file, "/") + if slash >= 0 { + file = file[slash+1:] + } + } + return fmt.Sprintf("%s:%d", file, line) +} + +func Debug(args ...interface{}) { + if fn { + mylog.WithField("prefix", fileInfo(2)).Debug(args...) + } else { + mylog.Debug(args...) + } +} + +func DebugWithFields(fields Fields, args ...interface{}) { + entry := mylog.WithFields(logrus.Fields(fields)) + + if fn { + entry.Data["prefix"] = fileInfo(2) + } + entry.Debug(args...) + +} + +func Info(args ...interface{}) { + if fn { + mylog.WithField("prefix", fileInfo(2)).Info(args...) + } else { + mylog.Info(args...) + } +} + +func Infof(format string, args ...interface{}) { + if fn { + mylog.WithField("prefix", fileInfo(2)).Infof(format, args) + } else { + mylog.Infof(format, args) + } +} + +func InfoWithFields(fields Fields, args ...interface{}) { + entry := mylog.WithFields(logrus.Fields(fields)) + + if fn { + entry.Data["prefix"] = fileInfo(2) + } + entry.Info(args...) + +} + +func Error(args ...interface{}) { + if fn { + mylog.WithField("prefix", fileInfo(2)).Error(args...) + } else { + mylog.Error(args...) + } +} + +func ErrorWithFields(fields Fields, args ...interface{}) { + entry := mylog.WithFields(logrus.Fields(fields)) + + if fn { + entry.Data["prefix"] = fileInfo(2) + } + entry.Error(args...) + +} + +func Fatal(args ...interface{}) { + if fn { + mylog.WithField("prefix", fileInfo(2)).Fatal(args...) + } else { + mylog.Fatal(args...) + } +} + +func FatalWithFields(fields Fields, args ...interface{}) { + entry := mylog.WithFields(logrus.Fields(fields)) + + if fn { + entry.Data["prefix"] = fileInfo(2) + } + entry.Fatal(args...) + +} + +func Panic(args ...interface{}) { + if fn { + mylog.WithField("prefix", fileInfo(2)).Panic(args...) + } else { + mylog.Panic(args...) + } +} + +func PanicWithFields(fields Fields, args ...interface{}) { + entry := mylog.WithFields(logrus.Fields(fields)) + + if fn { + entry.Data["prefix"] = fileInfo(2) + } + entry.Info(args...) + +} diff --git a/log/log_test.go b/log/log_test.go new file mode 100644 index 0000000..cb2b253 --- /dev/null +++ b/log/log_test.go @@ -0,0 +1,46 @@ +package log + +import ( + "github.com/ehlxr/go-utils/log" + "github.com/sirupsen/logrus" +) + +func init() { + log.SetLogLevel(logrus.DebugLevel) + // log.SetLogFormatter(&logrus.JSONFormatter{TimestampFormat: "2006-01-02 15:04:05"}) + // log.SetFn(false) +} + +func TestLog() { + + log.Debug("debug text...") + log.Info("info text...") + log.Error("error text...") + // log.Fatal("fatal text...") + // log.Panic("panic text...") + + log.DebugWithFields(log.Fields{ + "id": "test", + "name": "jj", + }, "debug with fields text...") + + log.InfoWithFields(log.Fields{ + "id": "test", + "name": "jj", + }, "info with fields text...") + + log.ErrorWithFields(log.Fields{ + "id": "test", + "name": "jj", + }, "error with fields text...") + + log.FatalWithFields(log.Fields{ + "id": "test", + "name": "jj", + }, "fatal with fields text...") + + // log.Panic(log.Fields{ + // "id": "test", + // "name": "jj", + // }, "fatal with fields text...") +} diff --git a/main.go b/main.go index 7b399d5..dad8d34 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,46 @@ package main import ( - "fmt" + "github.com/ehlxr/go-utils/log" + "github.com/sirupsen/logrus" ) -func main() { - fmt.Println("go-utils") +func init() { + log.SetLogLevel(logrus.DebugLevel) + // log.SetLogFormatter(&logrus.JSONFormatter{TimestampFormat: "2006-01-02 15:04:05"}) + // log.SetFn(false) +} + +func main() { + + log.Debug("debug text...") + log.Info("info text...") + log.Error("error text...") + // log.Fatal("fatal text...") + // log.Panic("panic text...") + + log.DebugWithFields(log.Fields{ + "id": "test", + "name": "jj", + }, "debug with fields text...") + + log.InfoWithFields(log.Fields{ + "id": "test", + "name": "jj", + }, "info with fields text...") + + log.ErrorWithFields(log.Fields{ + "id": "test", + "name": "jj", + }, "error with fields text...") + + log.FatalWithFields(log.Fields{ + "id": "test", + "name": "jj", + }, "fatal with fields text...") + + // log.Panic(log.Fields{ + // "id": "test", + // "name": "jj", + // }, "fatal with fields text...") }