1 Star 0 Fork 0

zhonglin.liu / dao_socket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
log.go 3.93 KB
一键复制 编辑 原始数据 按行查看 历史
zhonglin.liu 提交于 2022-05-03 00:09 . log
package utils
import (
"encoding/json"
"fmt"
"log"
"os"
"runtime"
)
type (
Level int
)
const (
LevelFatal = iota
LevelError
LevelWarning
LevelInfo
LevelDebug
fatalColor = "\033[35m"
errColor = "\033[31m"
warningColor = "\033[33m"
infoColor = "\033[32m"
debugColor = "\033[36m"
colorEnd = "\033[0m"
)
var logger = New()
func Fatal(v ...interface{}) {
p(LevelFatal, v...)
os.Exit(1)
}
func Fatalf(format string, v ...interface{}) {
logger.Output(LevelFatal, fmt.Sprintf(format, v...))
os.Exit(1)
}
func Error(v ...interface{}) {
p(LevelError, v...)
}
func Errorf(format string, v ...interface{}) {
logger.Output(LevelError, fmt.Sprintf(format, v...))
}
func Warn(v ...interface{}) {
p(LevelWarning, v...)
}
func Warnf(format string, v ...interface{}) {
logger.Output(LevelWarning, fmt.Sprintf(format, v...))
}
func Info(v ...interface{}) {
p(LevelInfo, v...)
}
func InfoJSON(v interface{}) {
data, _ := json.MarshalIndent(v, "", "\t")
p(LevelInfo, string(data))
}
func Stack(msg string) {
stack := msg + "\n"
for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
stack += fmt.Sprintln(fmt.Sprintf("%s:%d", file, line))
}
p(LevelError, stack)
}
func Infof(format string, v ...interface{}) {
logger.Output(LevelInfo, fmt.Sprintf(format, v...))
}
func Debug(v ...interface{}) {
p(LevelDebug, v...)
}
func Debugf(format string, v ...interface{}) {
logger.Output(LevelDebug, fmt.Sprintf(format, v...))
}
func Check(s string, err error) {
if err == nil {
logger.Output(LevelInfo, s+" ok")
} else {
logger.Output(LevelFatal, fmt.Sprintf("%s %+v", s, err))
os.Exit(1)
}
}
func SetLogLevel(level Level) {
logger.SetLogLevel(level)
}
func p(level Level, v ...interface{}) {
var formatStr string
length := len(v)
for i := 0; i < length; i++ {
formatStr += "%+v "
}
logger.Output(level, fmt.Sprintf(formatStr, v...))
}
type logManager struct {
_log *log.Logger
//小于等于该级别的level才会被记录
logLevel Level
}
//NewLogger 实例化,供自定义
func NewLogger() *logManager {
return &logManager{_log: log.New(os.Stderr, "", log.Lshortfile|log.LstdFlags), logLevel: LevelDebug}
}
//New 实例化,供外部直接调用 log.XXXX
func New() *logManager {
return &logManager{_log: log.New(os.Stderr, "", log.Lshortfile|log.LstdFlags), logLevel: LevelDebug}
}
func (l *logManager) Output(level Level, s string) error {
if l.logLevel < level {
return nil
}
switch level {
case LevelFatal:
s = fmt.Sprintf("%s[FATAL] %s%s", fatalColor, s, colorEnd)
case LevelError:
s = fmt.Sprintf("%s[ERROR] %s%s", errColor, s, colorEnd)
case LevelWarning:
s = fmt.Sprintf("%s[WARNING] %s%s", warningColor, s, colorEnd)
case LevelInfo:
s = fmt.Sprintf("%s[INFO] %s%s", infoColor, s, colorEnd)
case LevelDebug:
s = fmt.Sprintf("%s[DEBUG] %s%s", debugColor, s, colorEnd)
default:
s = fmt.Sprintf("%s[UNKNOWN] %s%s", infoColor, s, colorEnd)
}
return l._log.Output(4, s)
}
func (l *logManager) Fatal(v ...interface{}) {
p(LevelFatal, v...)
os.Exit(1)
}
func (l *logManager) Fatalf(format string, v ...interface{}) {
l.Output(LevelFatal, fmt.Sprintf(format, v...))
os.Exit(1)
}
func (l *logManager) Error(v ...interface{}) {
p(LevelError, v...)
}
func (l *logManager) Errorf(format string, v ...interface{}) {
l.Output(LevelError, fmt.Sprintf(format, v...))
}
func (l *logManager) Warn(v ...interface{}) {
p(LevelWarning, v...)
}
func (l *logManager) Warnf(format string, v ...interface{}) {
l.Output(LevelWarning, fmt.Sprintf(format, v...))
}
func (l *logManager) Info(v ...interface{}) {
p(LevelInfo, v...)
}
func (l *logManager) Infof(format string, v ...interface{}) {
l.Output(LevelInfo, fmt.Sprintf(format, v...))
}
func (l *logManager) Debug(v ...interface{}) {
p(LevelDebug, v...)
}
func (l *logManager) Debugf(format string, v ...interface{}) {
l.Output(LevelDebug, fmt.Sprintf(format, v...))
}
func (l *logManager) SetLogLevel(level Level) {
l.logLevel = level
}
Go
1
https://gitee.com/zhonglin-liu/dao_socket.git
git@gitee.com:zhonglin-liu/dao_socket.git
zhonglin-liu
dao_socket
dao_socket
4f29f3d82524

搜索帮助