This commit is contained in:
wangchuxiao
2023-03-13 10:03:23 +08:00
parent a348af74fa
commit d73ab781fe
4 changed files with 26 additions and 13 deletions
+17
View File
@@ -0,0 +1,17 @@
package log
import "context"
type Logger interface {
Debug(ctx context.Context, msg string, keysAndValues ...interface{})
Info(ctx context.Context, msg string, keysAndValues ...interface{})
Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{})
Error(ctx context.Context, msg string, err error, keysAndValues ...interface{})
WithValues(keysAndValues ...interface{}) LogrusLogger
WithName(name string) LogrusLogger
WithCallDepth(depth int) LogrusLogger
WithItemSampler() LogrusLogger
// WithoutSampler returns the original logger without sampling
WithoutSampler() LogrusLogger
}
+7 -7
View File
@@ -16,10 +16,10 @@ import (
"github.com/sirupsen/logrus"
)
var logger *Logger
var ctxLogger *Logger
var logger *LogrusLogger
var ctxLogger *LogrusLogger
type Logger struct {
type LogrusLogger struct {
*logrus.Logger
Pid int
Type string
@@ -35,7 +35,7 @@ func NewPrivateLog(moduleName string) {
ctxLogger = ctxLoggerInit(moduleName)
}
func ctxLoggerInit(moduleName string) *Logger {
func ctxLoggerInit(moduleName string) *LogrusLogger {
var ctxLogger = logrus.New()
ctxLogger.SetLevel(logrus.Level(config.Config.Log.RemainLogLevel))
src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
@@ -55,14 +55,14 @@ func ctxLoggerInit(moduleName string) *Logger {
//Log file segmentation hook
hook := NewLfsHook(time.Duration(config.Config.Log.RotationTime)*time.Hour, config.Config.Log.RemainRotationCount, moduleName)
ctxLogger.AddHook(hook)
return &Logger{
return &LogrusLogger{
ctxLogger,
os.Getpid(),
"ctxLogger",
}
}
func loggerInit(moduleName string) *Logger {
func loggerInit(moduleName string) *LogrusLogger {
var logger = logrus.New()
//All logs will be printed
logger.SetLevel(logrus.Level(config.Config.Log.RemainLogLevel))
@@ -93,7 +93,7 @@ func loggerInit(moduleName string) *Logger {
//Log file segmentation hook
hook := NewLfsHook(time.Duration(config.Config.Log.RotationTime)*time.Hour, config.Config.Log.RemainRotationCount, moduleName)
logger.AddHook(hook)
return &Logger{
return &LogrusLogger{
logger,
os.Getpid(),
"",