-
Notifications
You must be signed in to change notification settings - Fork 11
/
logger.go
179 lines (138 loc) Β· 3.58 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
package lecho
import (
"fmt"
"io"
"github.com/labstack/gommon/log"
"github.com/rs/zerolog"
)
// Logger is a wrapper around `zerolog.Logger` that provides an implementation of `echo.Logger` interface
type Logger struct {
log zerolog.Logger
out io.Writer
level log.Lvl
prefix string
setters []Setter
}
// New returns a new Logger instance
func New(out io.Writer, setters ...Setter) *Logger {
switch l := out.(type) {
case zerolog.Logger:
return newLogger(l, setters)
default:
return newLogger(zerolog.New(out), setters)
}
}
// From returns a new Logger instance using existing zerolog log.
func From(log zerolog.Logger, setters ...Setter) *Logger {
return newLogger(log, setters)
}
func newLogger(log zerolog.Logger, setters []Setter) *Logger {
opts := newOptions(log, setters)
return &Logger{
log: opts.context.Logger(),
out: nil,
level: opts.level,
prefix: opts.prefix,
setters: setters,
}
}
func (l Logger) Debug(i ...interface{}) {
l.log.Debug().Msg(fmt.Sprint(i...))
}
func (l Logger) Debugf(format string, i ...interface{}) {
l.log.Debug().Msgf(format, i...)
}
func (l Logger) Debugj(j log.JSON) {
l.logJSON(l.log.Debug(), j)
}
func (l Logger) Info(i ...interface{}) {
l.log.Info().Msg(fmt.Sprint(i...))
}
func (l Logger) Infof(format string, i ...interface{}) {
l.log.Info().Msgf(format, i...)
}
func (l Logger) Infoj(j log.JSON) {
l.logJSON(l.log.Info(), j)
}
func (l Logger) Warn(i ...interface{}) {
l.log.Warn().Msg(fmt.Sprint(i...))
}
func (l Logger) Warnf(format string, i ...interface{}) {
l.log.Warn().Msgf(format, i...)
}
func (l Logger) Warnj(j log.JSON) {
l.logJSON(l.log.Warn(), j)
}
func (l Logger) Error(i ...interface{}) {
l.log.Error().Msg(fmt.Sprint(i...))
}
func (l Logger) Errorf(format string, i ...interface{}) {
l.log.Error().Msgf(format, i...)
}
func (l Logger) Errorj(j log.JSON) {
l.logJSON(l.log.Error(), j)
}
func (l Logger) Fatal(i ...interface{}) {
l.log.Fatal().Msg(fmt.Sprint(i...))
}
func (l Logger) Fatalf(format string, i ...interface{}) {
l.log.Fatal().Msgf(format, i...)
}
func (l Logger) Fatalj(j log.JSON) {
l.logJSON(l.log.Fatal(), j)
}
func (l Logger) Panic(i ...interface{}) {
l.log.Panic().Msg(fmt.Sprint(i...))
}
func (l Logger) Panicf(format string, i ...interface{}) {
l.log.Panic().Msgf(format, i...)
}
func (l Logger) Panicj(j log.JSON) {
l.logJSON(l.log.Panic(), j)
}
func (l Logger) Print(i ...interface{}) {
l.log.WithLevel(zerolog.NoLevel).Str("level", "-").Msg(fmt.Sprint(i...))
}
func (l Logger) Printf(format string, i ...interface{}) {
l.log.WithLevel(zerolog.NoLevel).Str("level", "-").Msgf(format, i...)
}
func (l Logger) Printj(j log.JSON) {
l.logJSON(l.log.WithLevel(zerolog.NoLevel).Str("level", "-"), j)
}
func (l Logger) Output() io.Writer {
return l.log
}
func (l *Logger) SetOutput(newOut io.Writer) {
l.out = newOut
l.log = l.log.Output(newOut)
}
func (l Logger) Level() log.Lvl {
return l.level
}
func (l *Logger) SetLevel(level log.Lvl) {
zlvl, elvl := MatchEchoLevel(level)
l.setters = append(l.setters, WithLevel(elvl))
l.level = elvl
l.log = l.log.Level(zlvl)
}
func (l Logger) Prefix() string {
return l.prefix
}
func (l Logger) SetHeader(h string) {
// not implemented
}
func (l *Logger) SetPrefix(newPrefix string) {
l.setters = append(l.setters, WithPrefix(newPrefix))
opts := newOptions(l.log, l.setters)
l.prefix = newPrefix
l.log = opts.context.Logger()
}
func (l *Logger) Unwrap() zerolog.Logger {
return l.log
}
func (l *Logger) logJSON(event *zerolog.Event, j log.JSON) {
for k, v := range j {
event = event.Interface(k, v)
}
event.Msg("")
}