-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
104 lines (84 loc) · 2.35 KB
/
log.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package golog
import (
"context"
)
type Logger struct {
h *Handlers
}
type Config struct {
FileConfig *FileConfig `yaml:"fileConfig"`
}
var (
logger Logger
)
func Init(c *Config) {
if c == nil {
fileConfig := &FileConfig{
Console: true,
}
c = &Config{FileConfig: fileConfig}
}
if c.FileConfig.Console {
fileHandler := NewConsoleFileHandler(c.FileConfig)
logger.h = NewHandlers(fileHandler)
} else {
fileHandler := NewFileHandler(c.FileConfig)
logger.h = NewHandlers(fileHandler)
}
}
func NewLogger(c *Config) *Logger {
var logger = &Logger{}
if c == nil {
fileConfig := &FileConfig{
Console: true,
}
c = &Config{FileConfig: fileConfig}
}
if c.FileConfig.Console {
fileHandler := NewConsoleFileHandler(c.FileConfig)
logger.h = NewHandlers(fileHandler)
} else {
fileHandler := NewFileHandler(c.FileConfig)
logger.h = NewHandlers(fileHandler)
}
return logger
}
func (logger *Logger) SetHandlers(hs *Handlers) {
logger.h = hs
}
func Debug(format string, args ...interface{}) {
logger.h.Log(context.Background(), DebugLevel, format, args...)
}
func Info(format string, args ...interface{}) {
logger.h.Log(context.Background(), InfoLevel, format, args...)
}
func Warn(format string, args ...interface{}) {
logger.h.Log(context.Background(), WarnLevel, format, args...)
}
func Error(format string, args ...interface{}) {
logger.h.Log(context.Background(), ErrorLevel, format, args...)
}
func Panic(format string, args ...interface{}) {
logger.h.Log(context.Background(), PanicLevel, format, args...)
}
func Fatal(format string, args ...interface{}) {
logger.h.Log(context.Background(), FatalLevel, format, args...)
}
func DebugC(ctx context.Context, format string, args ...interface{}) {
logger.h.Log(ctx, DebugLevel, format, args...)
}
func InfoC(ctx context.Context, format string, args ...interface{}) {
logger.h.Log(ctx, InfoLevel, format, args...)
}
func WarnC(ctx context.Context, format string, args ...interface{}) {
logger.h.Log(ctx, WarnLevel, format, args...)
}
func ErrorC(ctx context.Context, format string, args ...interface{}) {
logger.h.Log(ctx, ErrorLevel, format, args...)
}
func PanicC(ctx context.Context, format string, args ...interface{}) {
logger.h.Log(ctx, PanicLevel, format, args...)
}
func FatalC(ctx context.Context, format string, args ...interface{}) {
logger.h.Log(ctx, FatalLevel, format, args...)
}