-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
130 lines (105 loc) · 2.69 KB
/
json.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
package log
import (
"fmt"
"io"
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/go-angle/angle/config"
"github.com/go-angle/angle/di"
"github.com/go-angle/angle/util"
)
func init() {
di.Provide(newLogger)
}
// jsonLog impl Logger via zerolog
type jsonLog struct {
logger zerolog.Logger
ctx zerolog.Context
}
func newLogger(c *config.Config) Logger {
ctx := log.With().
Str("app", c.Name).
Str("stage", c.Stage).Str("hostname", util.Hostname())
if c.IsDevelopment() {
ctx = ctx.Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr}).With()
}
return &jsonLog{
logger: ctx.Logger(),
ctx: ctx,
}
}
// Output set output
func (l *jsonLog) Output(w io.Writer) Logger {
logger := l.logger.Output(w)
return &jsonLog{logger: logger, ctx: logger.With()}
}
// WithFields add extra fields
func (l *jsonLog) WithFields(fields Fields) Logger {
ctx := l.ctx.Fields(fields)
return &jsonLog{logger: ctx.Logger(), ctx: ctx}
}
// Debugf debug
func (l *jsonLog) Debugf(format string, args ...interface{}) {
l.logger.Debug().Msgf(format, args...)
}
// Infof info
func (l *jsonLog) Infof(format string, args ...interface{}) {
l.logger.Info().Msgf(format, args...)
}
// Printf print
func (l *jsonLog) Printf(format string, args ...interface{}) {
l.logger.Printf(format, args...)
}
// Warnf warnf
func (l *jsonLog) Warnf(format string, args ...interface{}) {
l.logger.Warn().Msgf(format, args...)
}
// Warningf warn
func (l *jsonLog) Warningf(format string, args ...interface{}) {
l.Warnf(format, args...)
}
// Errorf error
func (l *jsonLog) Errorf(format string, args ...interface{}) {
l.logger.Error().Msgf(format, args...)
}
// Fatalf fatalf
func (l *jsonLog) Fatalf(format string, args ...interface{}) {
l.logger.Fatal().Msgf(format, args...)
}
// Panicf panic
func (l *jsonLog) Panicf(format string, args ...interface{}) {
l.logger.Panic().Msgf(format, args...)
}
// Debug debug
func (l *jsonLog) Debug(args ...interface{}) {
l.logger.Debug().Msg(fmt.Sprint(args...))
}
// Info info
func (l *jsonLog) Info(args ...interface{}) {
l.logger.Info().Msg(fmt.Sprint(args...))
}
// Print print
func (l *jsonLog) Print(args ...interface{}) {
l.logger.Print(fmt.Sprint(args...))
}
// Warn warn
func (l *jsonLog) Warn(args ...interface{}) {
l.logger.Warn().Msg(fmt.Sprint(args...))
}
// Warning warn
func (l *jsonLog) Warning(args ...interface{}) {
l.Warn(args...)
}
// Error error
func (l *jsonLog) Error(args ...interface{}) {
l.logger.Error().Msg(fmt.Sprint(args...))
}
// Fatal fatal
func (l *jsonLog) Fatal(args ...interface{}) {
l.logger.Fatal().Msg(fmt.Sprint(args...))
}
// Panic panic
func (l *jsonLog) Panic(args ...interface{}) {
l.logger.Panic().Msg(fmt.Sprint(args...))
}