add ContextLogger interface

This commit is contained in:
Lunny Xiao 2020-02-27 17:11:31 +08:00
parent 2b62dc5a51
commit ad055c1bc0
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
1 changed files with 86 additions and 0 deletions

86
log/logger_context.go Normal file
View File

@ -0,0 +1,86 @@
// Copyright 2020 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package log
import (
"context"
"fmt"
"time"
)
// LogContext represents a log context
type LogContext struct {
Ctx context.Context
LogLevel LogLevel
LogData string // log content or SQL
Args []interface{} // if it's a SQL, it's the arguments
IsSQL bool
ExecuteTime time.Duration
}
// ContextLogger represents a logger interface with context
type ContextLogger interface {
Debug(ctx LogContext)
Error(ctx LogContext)
Info(ctx LogContext)
Warn(ctx LogContext)
Before(context LogContext)
Level() LogLevel
SetLevel(l LogLevel)
ShowSQL(show ...bool)
IsShowSQL() bool
}
var (
_ ContextLogger = &LoggerAdapter{}
)
// LoggerAdapter wraps a Logger interafce as LoggerContext interface
type LoggerAdapter struct {
logger Logger
}
func (l *LoggerAdapter) Before(ctx LogContext) {}
func (l *LoggerAdapter) Debug(ctx LogContext) {
l.logger.Debug(ctx.LogData)
}
func (l *LoggerAdapter) Error(ctx LogContext) {
l.logger.Error(ctx.LogData)
}
func (l *LoggerAdapter) Info(ctx LogContext) {
if !l.logger.IsShowSQL() && ctx.IsSQL {
return
}
if ctx.IsSQL {
l.logger.Info(fmt.Sprintf("[SQL] %v %v", ctx.LogData, ctx.Args))
} else {
l.logger.Info(ctx.LogData)
}
}
func (l *LoggerAdapter) Warn(ctx LogContext) {
l.logger.Warn(ctx.LogData)
}
func (l *LoggerAdapter) Level() LogLevel {
return l.logger.Level()
}
func (l *LoggerAdapter) SetLevel(lv LogLevel) {
l.logger.SetLevel(lv)
}
func (l *LoggerAdapter) ShowSQL(show ...bool) {
l.logger.ShowSQL(show...)
}
func (l *LoggerAdapter) IsShowSQL() bool {
return l.logger.IsShowSQL()
}