Skip to content

Commit 6324027

Browse files
author
zzm
committed
logger v2 fix:单独统计error、logrotate
1 parent 790bfcd commit 6324027

File tree

4 files changed

+81
-62
lines changed

4 files changed

+81
-62
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
go.uber.org/zap v1.24.0
1111
golang.org/x/net v0.17.0
1212
golang.org/x/tools v0.9.1
13+
gopkg.in/natefinch/lumberjack.v2 v2.0.0
1314
k8s.io/api v0.27.2
1415
k8s.io/apimachinery v0.27.2
1516
k8s.io/client-go v0.27.2

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
23
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
34
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
45
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
@@ -256,6 +257,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
256257
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
257258
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
258259
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
260+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
261+
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
259262
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
260263
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
261264
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

internal/logger/logger_utils.go

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"fmt"
66
"github.com/sirupsen/logrus"
7-
"os"
7+
"gopkg.in/natefinch/lumberjack.v2"
88
"path/filepath"
99
"time"
1010
)
@@ -26,9 +26,9 @@ type Logger struct {
2626
}
2727

2828
type Config struct {
29-
ModuleToFile map[string]string
30-
LogLevel LogLevel
31-
LogFormat logrus.Formatter
29+
ModuleNameToLogFileDir map[string]string
30+
LogLevel LogLevel
31+
LogFormat logrus.Formatter
3232
}
3333

3434
type TextVFormatter struct{}
@@ -42,48 +42,66 @@ func (f *TextVFormatter) Format(entry *logrus.Entry) ([]byte, error) {
4242
return []byte(log), nil
4343
}
4444

45-
var ModuleToLoggers map[string]*logrus.Logger
45+
var ModuleNameToLoggers map[string]*logrus.Logger
4646
var config Config
4747

4848
func Initialize(cfg Config) {
4949
config = cfg
50-
ModuleToLoggers = make(map[string]*logrus.Logger)
51-
52-
for module, file := range config.ModuleToFile {
53-
// setting log level
54-
switch config.LogLevel {
55-
case InfoLevel:
56-
logrus.SetLevel(logrus.InfoLevel)
57-
case WarnLevel:
58-
logrus.SetLevel(logrus.WarnLevel)
59-
case ErrorLevel:
60-
logrus.SetLevel(logrus.ErrorLevel)
61-
default:
62-
logrus.SetLevel(logrus.InfoLevel)
63-
}
64-
65-
path := filepath.Dir(file)
66-
if err := os.MkdirAll(path, os.ModePerm); err != nil {
67-
logrus.WithError(err).Fatal("cannot create log directory")
68-
}
69-
logFile, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
70-
if err != nil {
71-
logrus.WithError(err).Fatal("cannot open log file")
72-
}
73-
74-
// create logger instance for each module
75-
logger := logrus.New()
76-
// setting module log output
77-
logger.Out = logFile
78-
logger.Formatter = config.LogFormat
79-
ModuleToLoggers[module] = logger
50+
ModuleNameToLoggers = make(map[string]*logrus.Logger)
51+
setLogLevel()
52+
53+
for moduleName, logFileDir := range config.ModuleNameToLogFileDir {
54+
logger := createLogger(moduleName, logFileDir, "info")
55+
ModuleNameToLoggers[fmt.Sprintf("%s_%v", moduleName, "info")] = logger
56+
logger = createLogger(moduleName, logFileDir, "error")
57+
ModuleNameToLoggers[fmt.Sprintf("%s_%v", moduleName, "error")] = logger
8058
}
8159
}
8260

61+
func createLogger(moduleName, logFileDir, logLevel string) *logrus.Logger {
62+
logFilePath := getLogFilePath(logFileDir, moduleName, logLevel)
63+
logger := logrus.New()
64+
logger.Formatter = config.LogFormat
65+
logger.Out = &lumberjack.Logger{
66+
Filename: logFilePath,
67+
MaxSize: 1024,
68+
MaxBackups: 5,
69+
MaxAge: 28,
70+
Compress: false,
71+
LocalTime: true,
72+
}
73+
return logger
74+
}
75+
76+
func setLogLevel() {
77+
switch config.LogLevel {
78+
case InfoLevel:
79+
logrus.SetLevel(logrus.InfoLevel)
80+
case WarnLevel:
81+
logrus.SetLevel(logrus.WarnLevel)
82+
case ErrorLevel:
83+
logrus.SetLevel(logrus.ErrorLevel)
84+
default:
85+
logrus.SetLevel(logrus.InfoLevel)
86+
}
87+
}
88+
89+
func getLogFilePath(logDir, moduleName, logLevel string) string {
90+
fileName := fmt.Sprintf("%s_%s.log", moduleName, logLevel)
91+
return filepath.Join(logDir, fileName)
92+
}
93+
8394
func New(moduleName string) *Logger {
8495
return &Logger{
8596
moduleName: moduleName,
86-
logger: ModuleToLoggers[moduleName].WithField("", ""),
97+
logger: ModuleNameToLoggers[fmt.Sprintf("%s_%v", moduleName, "info")].WithField(TraceID, ""),
98+
}
99+
}
100+
101+
func NewError(moduleName string) *Logger {
102+
return &Logger{
103+
moduleName: moduleName,
104+
logger: ModuleNameToLoggers[fmt.Sprintf("%s_%v", moduleName, "error")].WithField(TraceID, ""),
87105
}
88106
}
89107

@@ -94,7 +112,7 @@ func NewFromContext(ctx context.Context, moduleName string) *Logger {
94112
}
95113
return &Logger{
96114
moduleName: moduleName,
97-
logger: ModuleToLoggers[moduleName].WithField(TraceID, traceID),
115+
logger: ModuleNameToLoggers[fmt.Sprintf("%s_%v", moduleName, "info")].WithField(TraceID, traceID),
98116
}
99117
}
100118

@@ -108,4 +126,6 @@ func (l *Logger) Warn(fields Fields, message string) {
108126

109127
func (l *Logger) Error(fields Fields, message string) {
110128
l.logger.WithFields(logrus.Fields(fields)).Error(message)
129+
130+
NewError(l.moduleName).logger.WithFields(logrus.Fields(fields)).Error(message)
111131
}

internal/logger/logger_utils_test.go

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package logger
22

33
import (
44
"context"
5+
"fmt"
56
"github.com/sirupsen/logrus"
67
"testing"
78
)
89

9-
func TestLogger_Schedule_Json(t *testing.T) {
10-
config := Config{ModuleToFile: map[string]string{
11-
"schedule": "logs/schedule.log",
12-
"module_controller": "logs/module_controller.log",
10+
func TestLogger_Json(t *testing.T) {
11+
config := Config{ModuleNameToLogFileDir: map[string]string{
12+
"schedule": "logs",
13+
"module_controller": "logs",
1314
},
1415
LogLevel: InfoLevel,
1516
LogFormat: &logrus.JSONFormatter{},
@@ -20,40 +21,34 @@ func TestLogger_Schedule_Json(t *testing.T) {
2021
logger := New("schedule")
2122
logger.Info(Fields{"hello": "word", "foo": "bar"}, "message")
2223
logger.Error(Fields{"key": "value"}, "a mc error occurred")
23-
}
24-
25-
func TestLogger_module_controller_CustomVText(t *testing.T) {
26-
config := Config{ModuleToFile: map[string]string{
27-
"schedule": "logs/schedule.log",
28-
"module_controller": "logs/module_controller.log",
29-
},
30-
LogLevel: InfoLevel,
31-
LogFormat: &TextVFormatter{},
32-
}
33-
34-
Initialize(config)
3524

3625
ctx := context.WithValue(context.Background(), TraceID, "12345")
37-
logger := NewFromContext(ctx, "module_controller")
26+
logger = NewFromContext(ctx, "module_controller")
3827
logger.Info(Fields{"hello": "word", "foo": "bar"}, "message")
3928
logger.Error(Fields{"key": "value"}, "a mc error occurred")
4029
}
4130

42-
func TestLogger_Warn(t *testing.T) {
43-
config := Config{ModuleToFile: map[string]string{
44-
"schedule": "logs/schedule.log",
45-
"module_controller": "logs/module_controller.log",
31+
func TestLogger_CustomVText(t *testing.T) {
32+
config := Config{ModuleNameToLogFileDir: map[string]string{
33+
"schedule": "logs",
34+
"module_controller": "logs",
4635
},
47-
LogLevel: InfoLevel,
48-
LogFormat: &logrus.TextFormatter{},
36+
LogLevel: ErrorLevel,
37+
LogFormat: &TextVFormatter{},
4938
}
5039

5140
Initialize(config)
5241

5342
logger := New("schedule")
54-
logger.Info(Fields{"hello": "word", "foo": "bar"}, "message")
43+
logger.Info(Fields{"hello": "word", "foo": "bar"}, "info message")
44+
logger.Error(Fields{"key": "value"}, "error message")
5545

5646
ctx := context.WithValue(context.Background(), TraceID, "12345")
5747
logger = NewFromContext(ctx, "module_controller")
58-
logger.Error(Fields{"key": "value"}, "a mc error occurred")
48+
for i := 0; i < 10; i++ {
49+
logger.Info(Fields{"hello": "word", "foo": "bar"}, fmt.Sprintf("info message %d", i))
50+
}
51+
for i := 0; i < 10; i++ {
52+
logger.Error(Fields{"hello": "word", "foo": "bar"}, fmt.Sprintf("info message %d", i))
53+
}
5954
}

0 commit comments

Comments
 (0)