update at 2021-01-01 23:12:00 by ehlxr
This commit is contained in:
parent
c03451395f
commit
7eee167d91
11
log.go
11
log.go
@ -35,6 +35,7 @@ type logConfig struct {
|
|||||||
CrashLogFilename string
|
CrashLogFilename string
|
||||||
ErrorLogFilename string
|
ErrorLogFilename string
|
||||||
EnableLineNumber bool
|
EnableLineNumber bool
|
||||||
|
AddCallerSkip int
|
||||||
|
|
||||||
// enable the truncation of the level text to 4 characters.
|
// enable the truncation of the level text to 4 characters.
|
||||||
EnableLevelTruncation bool
|
EnableLevelTruncation bool
|
||||||
@ -62,6 +63,10 @@ func (config *logConfig) Init() {
|
|||||||
logger = config.newLogger().Sugar()
|
logger = config.newLogger().Sugar()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config *logConfig) New() *zap.SugaredLogger {
|
||||||
|
return config.newLogger().Sugar()
|
||||||
|
}
|
||||||
|
|
||||||
func NewLogConfig() *logConfig {
|
func NewLogConfig() *logConfig {
|
||||||
return &logConfig{
|
return &logConfig{
|
||||||
Level: DebugLevel,
|
Level: DebugLevel,
|
||||||
@ -113,7 +118,7 @@ func (config *logConfig) newLogger() *zap.Logger {
|
|||||||
var options []zap.Option
|
var options []zap.Option
|
||||||
|
|
||||||
if config.EnableLineNumber {
|
if config.EnableLineNumber {
|
||||||
options = append(options, zap.AddCaller(), zap.AddCallerSkip(1))
|
options = append(options, zap.AddCaller(), zap.AddCallerSkip(config.AddCallerSkip))
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.EnableErrorStacktrace {
|
if config.EnableErrorStacktrace {
|
||||||
@ -265,3 +270,7 @@ func writeCrashLog(file string) {
|
|||||||
func Fields(args ...interface{}) {
|
func Fields(args ...interface{}) {
|
||||||
logger = logger.With(args...)
|
logger = logger.With(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func With(l *zap.SugaredLogger, args ...interface{}) *zap.SugaredLogger {
|
||||||
|
return l.With(args...)
|
||||||
|
}
|
||||||
|
@ -21,10 +21,10 @@ func TestLogWithConfig(t *testing.T) {
|
|||||||
|
|
||||||
config.Init()
|
config.Init()
|
||||||
|
|
||||||
With("traceid", float64(21221212122))
|
Fields("traceid", float64(21221212122))
|
||||||
Debugf("this is %s message", "debug")
|
Debugf("this is %s message", "debug")
|
||||||
config.Init()
|
config.Init()
|
||||||
With(zap.String("traceid", "12123123123"))
|
Fields(zap.String("traceid", "12123123123"))
|
||||||
Infof("this is %s message", "info")
|
Infof("this is %s message", "info")
|
||||||
// Errorf("this is %s message", "error")
|
// Errorf("this is %s message", "error")
|
||||||
// Panicf("this is %s message", "panic")
|
// Panicf("this is %s message", "panic")
|
||||||
|
@ -1,9 +1,5 @@
|
|||||||
package log
|
package log
|
||||||
|
|
||||||
func With(args ...interface{}) {
|
|
||||||
logger = logger.With(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Debug(args ...interface{}) {
|
func Debug(args ...interface{}) {
|
||||||
logger.Debug(args...)
|
logger.Debug(args...)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user