-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
189 lines (154 loc) · 3.96 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package rndc
import (
"context"
"fmt"
"io"
"os"
"runtime"
"strings"
"sync/atomic"
"time"
)
type Level uint32
const (
CriticalLevel Level = iota
ErrorLevel
WarnLevel
InfoLevel
DebugLevel
)
func (level Level) String() string {
switch level {
case DebugLevel:
return "debug"
case InfoLevel:
return "info"
case WarnLevel:
return "warning"
case ErrorLevel:
return "error"
case CriticalLevel:
return "critical"
}
return "unknown"
}
func StringToLevel(level string) Level {
switch level {
case "critical":
return CriticalLevel
case "error":
return ErrorLevel
case "warn", "warning":
return WarnLevel
case "debug":
return DebugLevel
case "info":
return InfoLevel
}
return InfoLevel
}
var logger = NewLogger().WithDepth(4)
func Info(ctx context.Context, format string, v ...interface{}) {
logger.Info(ctx, format, v...)
}
func Debug(ctx context.Context, format string, v ...interface{}) {
logger.Debug(ctx, format, v...)
}
func Warn(ctx context.Context, format string, v ...interface{}) {
logger.Warn(ctx, format, v...)
}
func Error(ctx context.Context, format string, v ...interface{}) {
logger.Error(ctx, format, v...)
}
func Critical(ctx context.Context, format string, v ...interface{}) {
logger.Critical(ctx, format, v...)
}
func SetOutput(output io.Writer) {
logger.SetOutput(output)
}
var globalLogLevel = InfoLevel
func SetLevelByString(level string) {
logger.SetLevelByString(level)
globalLogLevel = StringToLevel(level)
}
func NewLogger() *Logger {
return &Logger{
Level: globalLogLevel,
output: os.Stdout,
depth: 3,
}
}
type Logger struct {
Level Level
output io.Writer
hideCallstack bool
depth int
}
func (logger *Logger) level() Level {
return Level(atomic.LoadUint32((*uint32)(&logger.Level)))
}
func (logger *Logger) SetLevel(level Level) {
atomic.StoreUint32((*uint32)(&logger.Level), uint32(level))
}
func (logger *Logger) SetLevelByString(level string) {
logger.SetLevel(StringToLevel(level))
}
var replacer = strings.NewReplacer("\r", "\\r", "\n", "\\n")
func (logger *Logger) formatOutput(ctx context.Context, level Level, output string) string {
now := time.Now().Format("2006-01-02 15:04:05.99999")
output = replacer.Replace(output)
var suffix string
if logger.hideCallstack {
return fmt.Sprintf("%-25s -%s- %s%s",
now, strings.ToUpper(level.String()), output, suffix)
} else {
_, file, line, ok := runtime.Caller(logger.depth)
if !ok {
file = "???"
line = 0
}
// short file name
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
file = file[i+1:]
break
}
}
// 2023-07-31 16:01:21.8168 -INFO- cmd: sync test123.com (main.go:21)
return fmt.Sprintf("%-25s -%s- %s (%s:%d)%s",
now, strings.ToUpper(level.String()), output, file, line, suffix)
}
}
func (logger *Logger) logf(ctx context.Context, level Level, format string, args ...interface{}) {
if logger.level() < level {
return
}
fmt.Fprintln(logger.output, logger.formatOutput(ctx, level, fmt.Sprintf(format, args...)))
}
func (logger *Logger) Debug(ctx context.Context, format string, args ...interface{}) {
logger.logf(ctx, DebugLevel, format, args...)
}
func (logger *Logger) Info(ctx context.Context, format string, args ...interface{}) {
logger.logf(ctx, InfoLevel, format, args...)
}
func (logger *Logger) Warn(ctx context.Context, format string, args ...interface{}) {
logger.logf(ctx, WarnLevel, format, args...)
}
func (logger *Logger) Error(ctx context.Context, format string, args ...interface{}) {
logger.logf(ctx, ErrorLevel, format, args...)
}
func (logger *Logger) Critical(ctx context.Context, format string, args ...interface{}) {
logger.logf(ctx, CriticalLevel, format, args...)
}
func (logger *Logger) SetOutput(output io.Writer) *Logger {
logger.output = output
return logger
}
func (logger *Logger) HideCallstack() *Logger {
logger.hideCallstack = true
return logger
}
func (logger *Logger) WithDepth(depth int) *Logger {
logger.depth = depth
return logger
}