3 Star 2 Fork 1

Gitee 极速下载/Go-Logger

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/phachon/go-logger
克隆/下载
console.go 3.14 KB
一键复制 编辑 原始数据 按行查看 历史
phachon 提交于 2019-12-15 11:20 . update go mod
package go_logger
import (
"errors"
"github.com/fatih/color"
"io"
"os"
"reflect"
"sync"
)
const CONSOLE_ADAPTER_NAME = "console"
var levelColors = map[int]color.Attribute{
LOGGER_LEVEL_EMERGENCY: color.FgWhite, //white
LOGGER_LEVEL_ALERT: color.FgCyan, //cyan
LOGGER_LEVEL_CRITICAL: color.FgMagenta, //magenta
LOGGER_LEVEL_ERROR: color.FgRed, //red
LOGGER_LEVEL_WARNING: color.FgYellow, //yellow
LOGGER_LEVEL_NOTICE: color.FgGreen, //green
LOGGER_LEVEL_INFO: color.FgBlue, //blue
LOGGER_LEVEL_DEBUG: color.BgBlue, //background blue
}
// adapter console
type AdapterConsole struct {
write *ConsoleWriter
config *ConsoleConfig
}
// console writer
type ConsoleWriter struct {
lock sync.Mutex
writer io.Writer
}
// console config
type ConsoleConfig struct {
// console text is show color
Color bool
// is json format
JsonFormat bool
// jsonFormat is false, please input format string
// if format is empty, default format "%millisecond_format% [%level_string%] %body%"
//
// Timestamp "%timestamp%"
// TimestampFormat "%timestamp_format%"
// Millisecond "%millisecond%"
// MillisecondFormat "%millisecond_format%"
// Level int "%level%"
// LevelString "%level_string%"
// Body string "%body%"
// File string "%file%"
// Line int "%line%"
// Function "%function%"
//
// example: format = "%millisecond_format% [%level_string%] %body%"
Format string
}
func (cc *ConsoleConfig) Name() string {
return CONSOLE_ADAPTER_NAME
}
func NewAdapterConsole() LoggerAbstract {
consoleWrite := &ConsoleWriter{
writer: os.Stdout,
}
config := &ConsoleConfig{}
return &AdapterConsole{
write: consoleWrite,
config: config,
}
}
func (adapterConsole *AdapterConsole) Init(consoleConfig Config) error {
if consoleConfig.Name() != CONSOLE_ADAPTER_NAME {
return errors.New("logger console adapter init error, config must ConsoleConfig")
}
vc := reflect.ValueOf(consoleConfig)
cc := vc.Interface().(*ConsoleConfig)
adapterConsole.config = cc
if cc.JsonFormat == false && cc.Format == "" {
cc.Format = defaultLoggerMessageFormat
}
return nil
}
func (adapterConsole *AdapterConsole) Write(loggerMsg *loggerMessage) error {
msg := ""
if adapterConsole.config.JsonFormat == true {
//jsonByte, _ := json.Marshal(loggerMsg)
jsonByte, _ := loggerMsg.MarshalJSON()
msg = string(jsonByte)
} else {
msg = loggerMessageFormat(adapterConsole.config.Format, loggerMsg)
}
consoleWriter := adapterConsole.write
if adapterConsole.config.Color {
colorAttr := adapterConsole.getColorByLevel(loggerMsg.Level, msg)
consoleWriter.lock.Lock()
color.New(colorAttr).Println(msg)
consoleWriter.lock.Unlock()
return nil
}
consoleWriter.lock.Lock()
consoleWriter.writer.Write([]byte(msg + "\n"))
consoleWriter.lock.Unlock()
return nil
}
func (adapterConsole *AdapterConsole) Name() string {
return CONSOLE_ADAPTER_NAME
}
func (adapterConsole *AdapterConsole) Flush() {
}
func (adapterConsole *AdapterConsole) getColorByLevel(level int, content string) color.Attribute {
lc, ok := levelColors[level]
if !ok {
lc = color.FgWhite
}
return lc
}
func init() {
Register(CONSOLE_ADAPTER_NAME, NewAdapterConsole)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/Go-Logger.git
git@gitee.com:mirrors/Go-Logger.git
mirrors
Go-Logger
Go-Logger
master

搜索帮助