/
logger.go
128 lines (104 loc) · 2.89 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
package logger
import (
"os"
"time"
"github.com/fatih/color"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/iudicium/pryingdeep/configs"
)
var Logger *zap.Logger
func InitLogger(silent bool) {
if !silent {
config := configs.GetConfig()
encoderConfig := zapcore.EncoderConfig{
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
CallerKey: "C",
FunctionKey: zapcore.OmitKey,
MessageKey: "M",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: customEncoderTime,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
// default encoder
encoder := zapcore.NewConsoleEncoder(encoderConfig)
var level zapcore.Level
switch config.Logger.Level {
case "debug":
level = zap.DebugLevel
case "info":
level = zap.InfoLevel
case "warn":
level = zap.WarnLevel
case "error":
level = zap.ErrorLevel
default:
level = zap.InfoLevel
}
//if config.Logger.Encoder == "json" {
// encoder = zapcore.NewJSONEncoder(encoderConfig)
//}
//TODO: fix writing to file later
//
//fileWriter := zapcore.AddSync(&lumberjack.Logger{
// Filename: fmt.Sprintf("provider-%s.log", time.Now.Format("2006-01-02")),
// MaxSize: 5, // megabytes
// MaxBackups: 3,
// MaxAge: 28, // days
//})
//
//fileEncoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
core := zapcore.NewTee(
zapcore.NewCore(encoder, zapcore.AddSync(os.Stderr), level),
//zapcore.NewCore(fileEncoder, fileWriter, level),
)
Logger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
} else {
color.Yellow("[!] Silent mode is on - no logging will appear in terminal")
Logger = zap.NewNop()
}
}
func customEncoderTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05"))
}
func Info(msg string, fields ...zap.Field) {
Logger.Info(msg, fields...)
}
func Infof(template string, args ...any) {
Logger.Sugar().Infof(template, args...)
}
func Warn(msg string, fields ...zap.Field) {
Logger.Warn(msg, fields...)
}
func Warnf(template string, args ...any) {
Logger.Sugar().Warnf(template, args...)
}
func Error(msg string, fields ...zap.Field) {
Logger.Error(msg, fields...)
}
func Errorf(template string, args ...any) {
Logger.Sugar().Errorf(template, args...)
}
func Debug(msg string, fields ...zap.Field) {
Logger.Debug(msg, fields...)
}
func Debugf(template string, args ...any) {
Logger.Sugar().Debugf(template, args...)
}
func Fatal(msg string, fields ...zap.Field) {
Logger.Fatal(msg, fields...)
}
func Fatalf(template string, args ...any) {
Logger.Sugar().Fatalf(template, args...)
}
func Panic(msg string, fields ...zap.Field) {
Logger.Panic(msg, fields...)
}
func Panicf(template string, args ...any) {
Logger.Sugar().Panicf(template, args...)
}