logging/base.go

80 lines
1.3 KiB
Go
Raw Normal View History

2024-04-09 10:58:40 +08:00
package oceanlogger
import (
"errors"
"path"
"runtime"
"strings"
"time"
)
//控制台日志对象定义
type ConsoleLogger struct {
level LogLevel
}
//自定义日志级别
type LogLevel uint8
const (
Failed LogLevel = 0
DEBUG LogLevel = iota
INFO
FATAL
ERROR
)
// 获取当前时间字符串
func getNowTimeStr() (timestr string) {
timestr = time.Now().Format("2006-01-02 15:04:05")
return
}
//通过结构体获取日志级别返回字符串
func getLogStr(l LogLevel) string {
switch l {
case DEBUG:
return "DEBUG"
case INFO:
return "INFO"
case ERROR:
return "ERROR"
case FATAL:
return "FATAL"
default:
return "get log string error,check please!"
}
}
//通过字符串获取结构体日志级别
func getLogStruct(s string) LogLevel {
switch strings.ToUpper(s) {
case "DEBUG":
return DEBUG
case "INFO":
return INFO
case "ERROR":
return ERROR
case "FATAL":
return FATAL
default:
return Failed
}
}
// 获取方法名,文件名,行号函数
func getInfo(skip int) (funcName, fileName string, line int, err error) {
pc, file, line, ok := runtime.Caller(skip)
if !ok {
return "", "", 0, errors.New("get funcName,File,Line error,please check")
}
funcName = runtime.FuncForPC(pc).Name()
fileName = path.Base(file)
return funcName, fileName, line, nil
}