forked from tehsphinx/log
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
154 lines (130 loc) · 4 KB
/
log.go
File metadata and controls
154 lines (130 loc) · 4 KB
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
// Package log provides a global logger for zerolog.
package log
import (
"context"
"fmt"
"os"
"time"
"github.com/cosygreen/errs"
"github.com/rs/zerolog"
)
// Setup is used to set up logging.
// If a context is passed, the logger will be added to the context and the context returned.
func Setup(ctx context.Context, opts ...SetupOption) context.Context {
cfg := getOptions(opts)
zerolog.TimeFieldFormat = time.RFC3339Nano
zerolog.SetGlobalLevel(cfg.level)
//nolint:reassign // zerolog global variables are meant to be reassigned for setup.
zerolog.ErrorMarshalFunc = zerologErrorMarshalFunc
//nolint:reassign
zerolog.ErrorStackMarshaler = func(err error) interface{} {
return errs.FormatStack(err)
}
out := getOutput(cfg.output, cfg.format)
if len(cfg.extraWriters) > 0 {
out = zerolog.MultiLevelWriter(append(cfg.extraWriters, out)...)
}
loggerCtx := zerolog.New(out).With().Timestamp()
if !cfg.hideCaller {
loggerCtx = loggerCtx.Caller()
}
logger := loggerCtx.Stack().Logger()
logger.UpdateContext(cfg.updateCtx)
logger.UpdateContext(func(c zerolog.Context) zerolog.Context {
switch cfg.serviceName {
case "", "unknown":
default:
c = c.Str("service", cfg.serviceName)
}
if cfg.hostName != "" {
c = c.Str("host", cfg.hostName)
}
if cfg.region != "" {
c = c.Str("region", cfg.region)
}
if cfg.publicIP != "" {
c = c.Str("publicIP", cfg.publicIP)
}
return c
})
Logger = logger
zerolog.DefaultContextLogger = &logger
if ctx != nil {
ctx = logger.WithContext(ctx)
}
return ctx
}
// Logger is the global logger.
//
//nolint:gochecknoglobals
var Logger = zerolog.New(os.Stderr).With().Timestamp().Logger()
// Err starts a new message with error level with err as a field if not nil or
// with info level if err is nil.
//
// You must call Msg on the returned event in order to send the event.
func Err(err error) *zerolog.Event {
return Logger.Err(err)
}
// Print sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
Logger.Debug().CallerSkipFrame(1).Msg(fmt.Sprint(v...))
}
// Printf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
Logger.Debug().CallerSkipFrame(1).Msgf(format, v...)
}
// Errorf sends a log event using error level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Errorf(format string, v ...interface{}) {
Logger.Error().CallerSkipFrame(1).Msgf(format, v...)
}
// Trace starts a new message with trace level.
//
// You must call Msg on the returned event in order to send the event.
func Trace() *zerolog.Event {
return Logger.Trace()
}
// Debug starts a new message with debug level.
//
// You must call Msg on the returned event in order to send the event.
func Debug() *zerolog.Event {
return Logger.Debug()
}
// Info starts a new message with info level.
//
// You must call Msg on the returned event in order to send the event.
func Info() *zerolog.Event {
return Logger.Info()
}
// Warn starts a new message with warn level.
//
// You must call Msg on the returned event in order to send the event.
func Warn() *zerolog.Event {
return Logger.Warn()
}
// Error starts a new message with error level.
//
// You must call Msg on the returned event in order to send the event.
func Error() *zerolog.Event {
return Logger.Error()
}
// Fatal starts a new message with fatal level.
//
// You must call Msg on the returned event in order to send the event.
func Fatal() *zerolog.Event {
return Logger.WithLevel(zerolog.FatalLevel)
}
// Panic starts a new message with panic level. The message is also sent
// to the panic function.
//
// You must call Msg on the returned event in order to send the event.
func Panic() *zerolog.Event {
return Logger.Panic()
}
// Ctx returns the Logger associated with the ctx. If no logger
// is associated, a disabled logger is returned.
func Ctx(ctx context.Context) *zerolog.Logger {
return zerolog.Ctx(ctx)
}