-
Notifications
You must be signed in to change notification settings - Fork 31
/
logger.go
135 lines (110 loc) · 3.38 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
package logger
import (
"fmt"
"io"
"github.com/fatih/color"
"github.com/aziontech/tablecli"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
log *zap.Logger
)
type Logger struct {
Debug bool
Silent bool
LogLevel string
}
func LogLevel(logger Logger) {
switch {
case logger.Debug:
New(zapcore.DebugLevel)
case logger.Silent:
New(zapcore.ErrorLevel)
case "debug" == logger.LogLevel:
New(zapcore.DebugLevel)
case "error" == logger.LogLevel:
New(zapcore.ErrorLevel)
default:
New(zapcore.InfoLevel)
}
}
func New(level zapcore.Level) {
var err error
config := zap.NewProductionConfig()
logLevel := zap.NewAtomicLevel()
logLevel.SetLevel(level)
config.Level = logLevel
encoderConfig := zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.EpochTimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
encoderConfig.TimeKey = "timestamp"
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.StacktraceKey = ""
config.EncoderConfig = encoderConfig
config.Encoding = "console"
config.OutputPaths = []string{"stdout"}
config.ErrorOutputPaths = []string{"stderr"}
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
log, err = config.Build(zap.AddCallerSkip(1))
if err != nil {
panic(err)
}
}
// FInfo I need to check if the debug is false because the error comes in the debug also as true
func FInfo(w io.Writer, message string) {
if !(log.Core().Enabled(zapcore.ErrorLevel) && !log.Core().Enabled(zapcore.DebugLevel)) ||
!(log.Core().Enabled(zapcore.ErrorLevel) && !log.Core().Enabled(zapcore.InfoLevel)) {
fmt.Fprintf(w, message) // nolint:all
}
}
func PrintHeader(table tablecli.Table, format string) {
if !(log.Core().Enabled(zapcore.ErrorLevel) && !log.Core().Enabled(zapcore.DebugLevel)) ||
!(log.Core().Enabled(zapcore.ErrorLevel) && !log.Core().Enabled(zapcore.InfoLevel)) {
table.PrintHeader(format)
}
}
func PrintRow(table tablecli.Table, format string, row []string) {
if !(log.Core().Enabled(zapcore.ErrorLevel) && !log.Core().Enabled(zapcore.DebugLevel)) ||
!(log.Core().Enabled(zapcore.ErrorLevel) && !log.Core().Enabled(zapcore.InfoLevel)) {
table.PrintRow(format, row)
}
}
func Info(message string, fields ...zap.Field) {
log.Info(message, fields...)
}
func Fatal(message string, fields ...zap.Field) {
log.Fatal(message, fields...)
}
func Debug(message string, fields ...zap.Field) {
log.Debug(message, fields...)
}
func Error(message string, fields ...zap.Field) {
log.Error(message, fields...)
}
func LogSuccess(w io.Writer, message string) {
formatSuccess := color.New(color.FgGreen).SprintfFunc()
FInfo(w, formatSuccess("🚀 %s\n", message))
}
func LogSuccessBad(w io.Writer, message string) {
formatSuccess := color.New(color.FgBlue).SprintfFunc()
FInfo(w, formatSuccess("👋 %s\n", message))
}
func LogWarning(w io.Writer, message string) {
formatWarning := color.New(color.FgYellow).SprintfFunc()
FInfo(w, formatWarning("⚠️ %s\n", message))
}
func LogError(w io.Writer, message string) {
formatError := color.New(color.FgRed).SprintfFunc()
FInfo(w, formatError("🐛 %s\n", message))
}