forked from databricks/databricks-sql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
160 lines (138 loc) · 4.6 KB
/
logger.go
File metadata and controls
160 lines (138 loc) · 4.6 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
155
156
157
158
159
160
package logger
import (
"context"
"io"
"os"
"runtime"
"time"
"github.com/mattn/go-isatty"
"github.com/rs/zerolog"
)
type DBSQLLogger struct {
zerolog.Logger
}
// Track is a simple utility function to use with logger to log a message with a timestamp.
// Recommended to use in conjunction with Duration.
//
// For example:
//
// msg, start := log.Track("Run operation")
// defer log.Duration(msg, start)
func (l *DBSQLLogger) Track(msg string) (string, time.Time) {
return msg, time.Now()
}
// Duration logs a debug message with the time elapsed between the provided start and the current time.
// Use in conjunction with Track.
//
// For example:
//
// msg, start := log.Track("Run operation")
// defer log.Duration(msg, start)
func (l *DBSQLLogger) Duration(msg string, start time.Time) {
l.Debug().Msgf("%v elapsed time: %v", msg, time.Since(start))
}
var Logger = &DBSQLLogger{
zerolog.New(os.Stderr).With().Timestamp().Logger(),
}
// Enable pretty printing for interactive terminals and json for production.
func init() {
// for tty terminal enable pretty logs
if isatty.IsTerminal(os.Stdout.Fd()) && runtime.GOOS != "windows" {
Logger = &DBSQLLogger{Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr})}
} else {
// UNIX Time is faster and smaller than most timestamps
// If you set zerolog.TimeFieldFormat to an empty string,
// logs will write with UNIX time.
zerolog.TimeFieldFormat = ""
}
// by default only log warns or above
loglvl := zerolog.WarnLevel
if lvst := os.Getenv("DATABRICKS_LOG_LEVEL"); lvst != "" {
if lv, err := zerolog.ParseLevel(lvst); err != nil {
Logger.Error().Msgf("log level %s not recognized", lvst)
} else {
loglvl = lv
}
}
Logger.Logger = Logger.Level(loglvl)
Logger.Info().Msgf("setting log level to %s", loglvl)
}
// Sets log level. Default is "warn"
// Available levels are: "trace" "debug" "info" "warn" "error" "fatal" "panic" or "disabled"
func SetLogLevel(l string) error {
if lv, err := zerolog.ParseLevel(l); err != nil {
return err
} else {
Logger.Logger = Logger.Level(lv)
return nil
}
}
// Sets logging output. Default is os.Stderr. If in terminal, pretty logs are enabled.
func SetLogOutput(w io.Writer) {
Logger.Logger = Logger.Output(w)
}
// Sets log to trace. -1
// You must call Msg on the returned event in order to send the event.
func Trace() *zerolog.Event {
return Logger.Trace()
}
// Sets log to debug. 0
// You must call Msg on the returned event in order to send the event.
func Debug() *zerolog.Event {
return Logger.Debug()
}
// Sets log to info. 1
// You must call Msg on the returned event in order to send the event.
func Info() *zerolog.Event {
return Logger.Info()
}
// Sets log to warn. 2
// You must call Msg on the returned event in order to send the event.
func Warn() *zerolog.Event {
return Logger.Warn()
}
// Sets log to error. 3
// You must call Msg on the returned event in order to send the event.
func Error() *zerolog.Event {
return Logger.Error()
}
// Sets log to fatal. 4
// You must call Msg on the returned event in order to send the event.
func Fatal() *zerolog.Event {
return Logger.Fatal()
}
// Sets log to panic. 5
// You must call Msg on the returned event in order to send the event.
func Panic() *zerolog.Event {
return Logger.Panic()
}
// 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)
}
// Ctx returns a DBSQLLogger from the provided context. If no logger is found,
// the default logger is returned.
func Ctx(ctx context.Context) *DBSQLLogger {
l := zerolog.Ctx(ctx)
if l == zerolog.DefaultContextLogger {
return Logger
}
return &DBSQLLogger{*l}
}
// AddContext sets connectionId, correlationId, and queryId as fields on the provided logger.
func AddContext(l *DBSQLLogger, connectionId string, correlationId string, queryId string) *DBSQLLogger {
return &DBSQLLogger{l.With().Str("connId", connectionId).Str("corrId", correlationId).Str("queryId", queryId).Logger()}
}
// WithContext sets connectionId, correlationId, and queryId to be used as fields.
func WithContext(connectionId string, correlationId string, queryId string) *DBSQLLogger {
return AddContext(Logger, connectionId, correlationId, queryId)
}
// Track is a convenience function to track time spent
func Track(msg string) (string, time.Time) {
return msg, time.Now()
}
// Duration is a convenience function to log elapsed time. Often used with Track
func Duration(msg string, start time.Time) {
Logger.Debug().Msgf("%v elapsed time: %v", msg, time.Since(start))
}