From ad055c1bc080c32bba8a574499d7f346ded7c36f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 27 Feb 2020 17:11:31 +0800 Subject: [PATCH] add ContextLogger interface --- log/logger_context.go | 86 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 log/logger_context.go diff --git a/log/logger_context.go b/log/logger_context.go new file mode 100644 index 00000000..b55b0670 --- /dev/null +++ b/log/logger_context.go @@ -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() +}