forked from webdevops/azure-metrics-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
61 lines (48 loc) · 1.3 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
package main
import (
"fmt"
"net/http"
stringsCommon "github.com/webdevops/go-common/strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
logger *zap.SugaredLogger
)
func buildContextLoggerFromRequest(r *http.Request) *zap.SugaredLogger {
contextLogger := logger.With(zap.String("requestPath", r.URL.Path))
for name, value := range r.URL.Query() {
fieldName := fmt.Sprintf("param%s", stringsCommon.UppercaseFirst(name))
fieldValue := value
contextLogger = contextLogger.With(zap.Any(fieldName, fieldValue))
}
return contextLogger
}
func initLogger() *zap.SugaredLogger {
var config zap.Config
if opts.Logger.Development {
config = zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
} else {
config = zap.NewProductionConfig()
}
config.Encoding = "console"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
// debug level
if opts.Logger.Debug {
config.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
}
// json log format
if opts.Logger.Json {
config.Encoding = "json"
// if running in containers, logs already enriched with timestamp by the container runtime
config.EncoderConfig.TimeKey = ""
}
// build logger
log, err := config.Build()
if err != nil {
panic(err)
}
logger = log.Sugar()
return logger
}