Merge pull request #12 from aceld/feature/aceld

add logger SetDebugMode
This commit is contained in:
刘丹冰 2024-03-26 15:28:27 +08:00 committed by GitHub
commit ef62e6ce54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 7 deletions

View File

@ -42,11 +42,11 @@ func NewFuncConfig(
config.FName = funcName
if source == nil {
log.Logger().ErrorF("funcName NewConfig Error, source is nil, funcName = %s\n", funcName)
defaultSource := KisSource{
Name: "unNamedSource",
}
source = &defaultSource
log.Logger().InfoF("funcName NewConfig source is nil, funcName = %s, use default unNamed Source.\n", funcName)
}
config.Source = *source

View File

@ -3,10 +3,20 @@ package log
import (
"context"
"fmt"
"sync"
)
// kisDefaultLog 默认提供的日志对象
type kisDefaultLog struct{}
type kisDefaultLog struct {
debugMode bool
mu sync.Mutex
}
func (log *kisDefaultLog) SetDebugMode(enable bool) {
log.mu.Lock()
defer log.mu.Unlock()
log.debugMode = enable
}
func (log *kisDefaultLog) InfoF(str string, v ...interface{}) {
fmt.Printf(str, v...)
@ -19,8 +29,12 @@ func (log *kisDefaultLog) ErrorF(str string, v ...interface{}) {
}
func (log *kisDefaultLog) DebugF(str string, v ...interface{}) {
fmt.Printf(str, v...)
fmt.Printf("\n")
log.mu.Lock()
defer log.mu.Unlock()
if log.debugMode {
fmt.Printf(str, v...)
fmt.Printf("\n")
}
}
func (log *kisDefaultLog) InfoFX(ctx context.Context, str string, v ...interface{}) {
@ -36,9 +50,13 @@ func (log *kisDefaultLog) ErrorFX(ctx context.Context, str string, v ...interfac
}
func (log *kisDefaultLog) DebugFX(ctx context.Context, str string, v ...interface{}) {
fmt.Println(ctx)
fmt.Printf(str, v...)
fmt.Printf("\n")
log.mu.Lock()
defer log.mu.Unlock()
if log.debugMode {
fmt.Println(ctx)
fmt.Printf(str, v...)
fmt.Printf("\n")
}
}
func init() {

View File

@ -16,6 +16,9 @@ type KisLogger interface {
ErrorF(str string, v ...interface{})
// DebugF 无上下文的Debug级别日志接口, format字符串格式
DebugF(str string, v ...interface{})
// SetDebugMode 设置Debug模式
SetDebugMode(enable bool)
}
// kisLog 默认的KisLog 对象, 提供默认的日志打印方式, 均是打印在标准输出上。