-
Notifications
You must be signed in to change notification settings - Fork 211
/
log.go
143 lines (114 loc) · 3.43 KB
/
log.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
131
132
133
134
135
136
137
138
139
140
141
142
143
// Package log provides the both file and console (general) logging capabilities
// to spacemesh modules such as app and identity.
package log
import (
"context"
"io"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const mainLoggerName = "defaultLogger"
// determine the level of messages we show.
var debugMode = false
// should we format out logs in json
var jsonLog = false
// where logs go by default
var logwriter io.Writer
// default encoders
var defaultEncoder = zap.NewDevelopmentEncoderConfig()
// Level returns the zapcore level of logging.
func Level() zapcore.Level {
if debugMode {
return zapcore.DebugLevel
}
return zapcore.InfoLevel
}
// Logger is an interface for our logging API.
type Logger interface {
Info(format string, args ...interface{})
Debug(format string, args ...interface{})
Error(format string, args ...interface{})
Warning(format string, args ...interface{})
With() FieldLogger
WithContext(context.Context) Log
WithName(string) Log
}
func encoder() zapcore.Encoder {
if jsonLog {
return zapcore.NewJSONEncoder(defaultEncoder)
}
return zapcore.NewConsoleEncoder(defaultEncoder)
}
// AppLog is the local app singleton logger.
var AppLog Log
func init() {
logwriter = os.Stdout
// create a basic temp os.Stdout logger
initLogging()
}
func initLogging() {
AppLog = NewDefault(mainLoggerName)
}
// DebugMode sets log debug level
func DebugMode(mode bool) {
debugMode = mode
}
// JSONLog turns JSON format on or off
func JSONLog(b bool) {
jsonLog = b
// Need to reinitialize
initLogging()
}
// NewWithLevel creates a logger with a fixed level and with a set of (optional) hooks
func NewWithLevel(level zap.AtomicLevel, hooks ...func(zapcore.Entry) error) Log {
consoleSyncer := zapcore.AddSync(logwriter)
enc := encoder()
consoleCore := zapcore.NewCore(enc, consoleSyncer, level)
core := zapcore.RegisterHooks(consoleCore, hooks...)
return NewFromLog(zap.New(core))
}
// NewDefault creates a Log with the default log level
func NewDefault(module string) Log {
logger := NewWithLevel(zap.NewAtomicLevelAt(Level()))
return NewFromLog(logger.logger.Named(module))
}
// NewFromLog creates a Log from an existing zap-compatible log.
func NewFromLog(l *zap.Logger) Log {
return Log{l}
}
// InitSpacemeshLoggingSystemWithHooks sets up a logging system with one or more
// registered hooks
func InitSpacemeshLoggingSystemWithHooks(hooks ...func(zapcore.Entry) error) {
logger := NewWithLevel(zap.NewAtomicLevelAt(Level()), hooks...)
AppLog = NewFromLog(logger.logger.Named(mainLoggerName))
}
// public wrappers abstracting away logging lib impl
// Info prints formatted info level log message.
func Info(msg string, args ...interface{}) {
AppLog.Info(msg, args...)
}
// Debug prints formatted debug level log message.
func Debug(msg string, args ...interface{}) {
AppLog.Debug(msg, args...)
}
// Error prints formatted error level log message.
func Error(msg string, args ...interface{}) {
AppLog.Error(msg, args...)
}
// Warning prints formatted warning level log message.
func Warning(msg string, args ...interface{}) {
AppLog.Warning(msg, args...)
}
// With returns a FieldLogger which you can append fields to.
func With() FieldLogger {
return FieldLogger{AppLog.logger}
}
// Event returns a field logger with the Event field set to true.
func Event() FieldLogger {
return AppLog.Event()
}
// Panic writes the log message and then panics.
func Panic(msg string, args ...interface{}) {
AppLog.Panic(msg, args...)
}