-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
152 lines (126 loc) · 2.93 KB
/
logger.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @Author : nopsky
* @Email : cnnopsky@gmail.com
* @Date : 2021/3/18 14:33
*/
package zlog
import (
log2 "log"
"github.com/go-warrior/pkg/zlog/zap"
)
type Logger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Panicf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
SetLevel(level string)
Debug(args ...interface{})
Info(args ...interface{})
Warn(args ...interface{})
Error(args ...interface{})
Panic(args ...interface{})
Fatal(args ...interface{})
}
var log Logger
func NewLogger(optsFunc ...OptionFunc) Logger {
opts := NewOptionsWithConfig()
for _, o := range optsFunc {
o(&opts)
}
return zap.NewLogger(
zap.Format(opts.Format),
zap.Level(opts.Level),
zap.Output(opts.Output),
zap.Filename(opts.Filename),
zap.MaxSize(opts.MaxSize),
zap.MaxBackups(opts.MaxBackups),
zap.MaxAge(opts.MaxAge),
zap.Compress(opts.Compress),
zap.AccessKey(opts.AccessKey),
zap.AccessKeySecret(opts.AccessKeySecret),
zap.EndPoint(opts.EndPoint),
zap.Project(opts.Project),
zap.LogStore(opts.LogStore),
zap.Topic(opts.Topic),
)
}
func NewLoggerWithOption(optsFunc ...OptionFunc) Logger {
opts := options{
Driver: "zap",
Output: "console",
Level: "debug",
Format: "text",
Filename: "./access.log",
MaxSize: 10,
MaxBackups: 5,
MaxAge: 30,
Compress: false,
}
for _, o := range optsFunc {
o(&opts)
}
log2.Println(opts.Output)
return zap.NewLogger(
zap.Format(opts.Format),
zap.Level(opts.Level),
zap.Output(opts.Output),
zap.Filename(opts.Filename),
zap.MaxSize(opts.MaxSize),
zap.MaxBackups(opts.MaxBackups),
zap.MaxAge(opts.MaxAge),
zap.Compress(opts.Compress),
zap.AccessKey(opts.AccessKey),
zap.AccessKeySecret(opts.AccessKeySecret),
zap.EndPoint(opts.EndPoint),
zap.Project(opts.Project),
zap.LogStore(opts.LogStore),
zap.Topic(opts.Topic),
)
}
func Init() {
log = NewLogger()
}
func Instance() Logger {
return log
}
func Debugf(format string, args ...interface{}) {
log.Debugf(format, args...)
}
func Infof(format string, args ...interface{}) {
log.Infof(format, args...)
}
func Warnf(format string, args ...interface{}) {
log.Warnf(format, args...)
}
func Errorf(format string, args ...interface{}) {
log.Errorf(format, args...)
}
func Panicf(format string, args ...interface{}) {
log.Panicf(format, args...)
}
func Fatalf(format string, args ...interface{}) {
log.Fatalf(format, args...)
}
func Debug(args ...interface{}) {
log.Debug(args...)
}
func Info(args ...interface{}) {
log.Info(args...)
}
func Warn(args ...interface{}) {
log.Warn(args...)
}
func Error(args ...interface{}) {
log.Error(args...)
}
func Panic(args ...interface{}) {
log.Panic(args...)
}
func Fatal(args ...interface{}) {
log.Fatal(args...)
}
func SetLevel(level string) {
log.SetLevel(level)
}