-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloggger.go
48 lines (38 loc) · 1.02 KB
/
loggger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package redisutil
import (
"context"
"encoding/json"
"fmt"
)
const (
LevelDebug = "DEBUG"
LevelInfo = "INFO"
LevelWarn = "WARN"
LevelError = "ERROR"
)
type LogData struct {
Level string
Content string
}
type Logger interface {
Errorf(ctx context.Context, format string, args ...interface{})
Infof(ctx context.Context, format string, args ...interface{})
}
var defaultLogger Logger = &DefaultLogger{}
func GetDefaultLogger() Logger {
return defaultLogger
}
func SetDefaultLogger(newLogger Logger) {
defaultLogger = newLogger
}
type DefaultLogger struct{}
func (l *DefaultLogger) Errorf(ctx context.Context, format string, args ...interface{}) {
content := fmt.Sprintf(format, args...)
logStr, _ := json.Marshal(LogData{Level: LevelError, Content: content})
fmt.Println(string(logStr))
}
func (l *DefaultLogger) Infof(ctx context.Context, format string, args ...interface{}) {
content := fmt.Sprintf(format, args...)
logStr, _ := json.Marshal(LogData{Level: LevelInfo, Content: content})
fmt.Println(string(logStr))
}