-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
161 lines (123 loc) · 2.98 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
package echotozero
import (
"context"
"fmt"
"io"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog"
)
// Enforce interface
var _ echo.Logger = (*Logger)(nil)
// Logger wraps `zerolog.Logger` and implements `echo.Logger`
type Logger struct {
ZLog zerolog.Logger
// Level used when Echo does not provide one
DefaultLevel zerolog.Level
}
func New(log zerolog.Logger) *Logger {
return &Logger{
ZLog: log,
DefaultLevel: zerolog.DebugLevel,
}
}
//
// Interface utility
//
func (l *Logger) Output() io.Writer {
return l.ZLog
}
func (l *Logger) Level() echoLevel {
return MapZeroToEcho[l.ZLog.GetLevel()]
}
func (l *Logger) WithContext(ctx context.Context) context.Context {
return l.ZLog.WithContext(ctx)
}
//
// Intentionally omitting some parts of the Echo interface
// Use zerolog features instead
//
func (l *Logger) SetOutput(newOut io.Writer) {}
func (l *Logger) SetHeader(h string) {}
func (l *Logger) SetLevel(level echoLevel) {}
func (l *Logger) SetPrefix(newPrefix string) {}
func (l *Logger) Prefix() string {
return ""
}
//
// Message
//
func (l *Logger) Debug(i ...interface{}) {
l.ZLog.Debug().Msg(fmt.Sprint(i...))
}
func (l *Logger) Info(i ...interface{}) {
l.ZLog.Info().Msg(fmt.Sprint(i...))
}
func (l *Logger) Warn(i ...interface{}) {
l.ZLog.Warn().Msg(fmt.Sprint(i...))
}
func (l *Logger) Error(i ...interface{}) {
l.ZLog.Error().Msg(fmt.Sprint(i...))
}
func (l *Logger) Fatal(i ...interface{}) {
l.ZLog.Fatal().Msg(fmt.Sprint(i...))
}
func (l *Logger) Panic(i ...interface{}) {
l.ZLog.Panic().Msg(fmt.Sprint(i...))
}
func (l *Logger) Print(i ...interface{}) {
l.ZLog.WithLevel(l.DefaultLevel).Msg(fmt.Sprint(i...))
}
//
// Format message
//
func (l *Logger) Debugf(format string, i ...interface{}) {
l.ZLog.Debug().Msgf(format, i...)
}
func (l *Logger) Infof(format string, i ...interface{}) {
l.ZLog.Info().Msgf(format, i...)
}
func (l *Logger) Warnf(format string, i ...interface{}) {
l.ZLog.Warn().Msgf(format, i...)
}
func (l *Logger) Errorf(format string, i ...interface{}) {
l.ZLog.Error().Msgf(format, i...)
}
func (l *Logger) Fatalf(format string, i ...interface{}) {
l.ZLog.Fatal().Msgf(format, i...)
}
func (l *Logger) Panicf(format string, i ...interface{}) {
l.ZLog.Panic().Msgf(format, i...)
}
func (l *Logger) Printf(format string, i ...interface{}) {
l.ZLog.WithLevel(l.DefaultLevel).Msgf(format, i...)
}
//
// JSON message
//
func (l *Logger) Debugj(j JSON) {
l.logJSON(l.ZLog.Debug(), j)
}
func (l *Logger) Infoj(j JSON) {
l.logJSON(l.ZLog.Info(), j)
}
func (l *Logger) Warnj(j JSON) {
l.logJSON(l.ZLog.Warn(), j)
}
func (l *Logger) Errorj(j JSON) {
l.logJSON(l.ZLog.Error(), j)
}
func (l *Logger) Fatalj(j JSON) {
l.logJSON(l.ZLog.Fatal(), j)
}
func (l *Logger) Panicj(j JSON) {
l.logJSON(l.ZLog.Panic(), j)
}
func (l *Logger) Printj(j JSON) {
l.logJSON(l.ZLog.WithLevel(l.DefaultLevel), j)
}
func (l *Logger) logJSON(event *zerolog.Event, j JSON) {
for k, v := range j {
event = event.Interface(k, v)
}
event.Msg("")
}