kis-flow/log/kis_default_log.go

68 lines
1.3 KiB
Go
Raw Normal View History

2024-01-02 15:38:51 +08:00
package log
import (
"context"
"fmt"
2024-03-26 15:27:24 +08:00
"sync"
2024-01-02 15:38:51 +08:00
)
// kisDefaultLog 默认提供的日志对象
2024-03-26 15:27:24 +08:00
type kisDefaultLog struct {
debugMode bool
mu sync.Mutex
}
func (log *kisDefaultLog) SetDebugMode(enable bool) {
log.mu.Lock()
defer log.mu.Unlock()
log.debugMode = enable
}
2024-01-02 15:38:51 +08:00
func (log *kisDefaultLog) InfoF(str string, v ...interface{}) {
fmt.Printf(str, v...)
2024-01-03 10:16:54 +08:00
fmt.Printf("\n")
2024-01-02 15:38:51 +08:00
}
func (log *kisDefaultLog) ErrorF(str string, v ...interface{}) {
fmt.Printf(str, v...)
2024-01-03 10:16:54 +08:00
fmt.Printf("\n")
2024-01-02 15:38:51 +08:00
}
func (log *kisDefaultLog) DebugF(str string, v ...interface{}) {
2024-03-26 15:27:24 +08:00
log.mu.Lock()
defer log.mu.Unlock()
if log.debugMode {
fmt.Printf(str, v...)
fmt.Printf("\n")
}
2024-01-02 15:38:51 +08:00
}
func (log *kisDefaultLog) InfoFX(ctx context.Context, str string, v ...interface{}) {
fmt.Println(ctx)
fmt.Printf(str, v...)
2024-01-03 10:16:54 +08:00
fmt.Printf("\n")
2024-01-02 15:38:51 +08:00
}
func (log *kisDefaultLog) ErrorFX(ctx context.Context, str string, v ...interface{}) {
fmt.Println(ctx)
fmt.Printf(str, v...)
2024-01-03 10:16:54 +08:00
fmt.Printf("\n")
2024-01-02 15:38:51 +08:00
}
func (log *kisDefaultLog) DebugFX(ctx context.Context, str string, v ...interface{}) {
2024-03-26 15:27:24 +08:00
log.mu.Lock()
defer log.mu.Unlock()
if log.debugMode {
fmt.Println(ctx)
fmt.Printf(str, v...)
fmt.Printf("\n")
}
2024-01-02 15:38:51 +08:00
}
func init() {
// 如果没有设置Logger, 则启动时使用默认的kisDefaultLog对象
if Logger() == nil {
SetLogger(&kisDefaultLog{})
}
}