generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
101 lines (75 loc) · 2.85 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
package logger
import (
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
// OutputFormatJSON is used to define output format as json.
OutputFormatJSON OutputFormat = "json"
// OutputFormatPlain is used to define output format as plain text.
OutputFormatPlain OutputFormat = "plain"
// KeyError is used as a named key for a log message with error.
KeyError = "error"
// KeyResult is used as a named key for a log message with result.
KeyResult = "result"
// KeySubAccountID is used as a named key for a log message with subaccount ID.
KeySubAccountID = "SubAccountID"
// KeyRuntimeID is used as a named key for a log message with runtime ID.
KeyRuntimeID = "RuntimeID"
// KeyRuntimeState is used as a named key for a log message with runtime state.
KeyRuntimeState = "RuntimeState"
// KeyProvisioningStatus is used as a named key for a log message with provisioning status.
KeyProvisioningStatus = "ProvisioningStatus"
// KeyDeprovisioningStatus is used as a named key for a log message with deprovisioning status.
KeyDeprovisioningStatus = "DeprovisioningStatus"
// KeyWorkerID is used as a named key for a log message with worker ID.
KeyWorkerID = "WorkerID"
// KeyRetry is used as named key for a log message which indicates the step will be retried.
KeyRetry = "willRetry"
// KeyRequeue is used as named key for a log message which indicates that it will be requeued.
KeyRequeue = "requeue"
// KeyShoot is used as a named key for a log message with shoot.
KeyShoot = "shoot"
// ValueFail is used as a value for a log message with failure.
ValueFail = "fail"
// ValueSuccess is used as a value for a log message with success.
ValueSuccess = "success"
// ValueTrue is used as a value for a message with true status.
ValueTrue = "true"
// ValueFalse is used as a value for a message with true status.
ValueFalse = "false"
)
var outputFormat = OutputFormatJSON
type OutputFormat string
func NewLogger(logLevel zapcore.Level) *zap.SugaredLogger {
return newLogger(logLevel).Sugar()
}
func newLogger(logLevel zapcore.Level) *zap.Logger {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
encoderConfig.TimeKey = "timestamp"
encoderConfig.MessageKey = "message"
encoderConfig.CallerKey = "caller"
var encoder zapcore.Encoder
switch outputFormat {
case OutputFormatPlain:
encoder = zapcore.NewConsoleEncoder(encoderConfig)
case OutputFormatJSON:
encoder = zapcore.NewJSONEncoder(encoderConfig)
}
return zap.New(
zapcore.NewCore(
encoder,
zapcore.Lock(os.Stderr),
zap.NewAtomicLevelAt(logLevel),
),
zap.AddCaller(),
zap.ErrorOutput(os.Stderr))
}
// SetOutputFormat sets the log output format.
func SetOutputFormat(of OutputFormat) {
outputFormat = of
}