Skip to content

Commit

Permalink
add logger SetDebugMode
Browse files Browse the repository at this point in the history
  • Loading branch information
aceld committed Mar 26, 2024
1 parent 46ad6ae commit 02223f6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/kis_func_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 24 additions & 6 deletions log/kis_default_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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{}) {
Expand All @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions log/kis_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 对象, 提供默认的日志打印方式, 均是打印在标准输出上。
Expand Down

0 comments on commit 02223f6

Please sign in to comment.