147 lines
3.2 KiB
Go
147 lines
3.2 KiB
Go
package logUtilPlus
|
||
|
||
import (
|
||
"fmt"
|
||
"goutil/debugUtil"
|
||
"goutil/esLogUtil"
|
||
"goutil/logUtil"
|
||
"log"
|
||
"strings"
|
||
)
|
||
|
||
var (
|
||
enableDebugLog = true
|
||
enableInfoLog = true
|
||
enableWarnLog = true
|
||
)
|
||
|
||
// 设置日志级别
|
||
// debug / all 时全部开启
|
||
// info 记录 info/warn/error/fatal
|
||
// warn 记录 warn/error/fatal
|
||
// error 记录 error/fatal
|
||
func SetLevel(level string) {
|
||
// 大写不敏感
|
||
level = strings.ToLower(level)
|
||
|
||
//全部开启
|
||
if level == "debug" || level == "all" {
|
||
// debug / all 时全部开启
|
||
enableInfoLog = true
|
||
enableDebugLog = true
|
||
enableWarnLog = true
|
||
} else if level == "info" {
|
||
// info 记录 info/warn/error/fatal
|
||
enableDebugLog = false
|
||
enableInfoLog = true
|
||
enableWarnLog = true
|
||
} else if level == "warn" {
|
||
// warn 记录 warn/error/fatal
|
||
enableDebugLog = false
|
||
enableInfoLog = false
|
||
enableWarnLog = true
|
||
} else if level == "error" {
|
||
// error 记录 error/fatal
|
||
enableInfoLog = false
|
||
enableDebugLog = false
|
||
enableWarnLog = false
|
||
}
|
||
}
|
||
|
||
// 启动ES日志系统(不调用则不启动ES日志系统,默认记录本地)
|
||
// 参数:
|
||
//
|
||
// esUrls:ES地址(多个地址使用,分割)
|
||
// name:IndexName
|
||
// serverGroupId:服务器组Id
|
||
//
|
||
// 返回值:
|
||
//
|
||
// 结果状态
|
||
func Start(esUrls string, name string, serverGroupId int32) {
|
||
esLogUtil.Start(esUrls, name, serverGroupId)
|
||
}
|
||
|
||
// 停止服务
|
||
func Stop() {
|
||
esLogUtil.Stop()
|
||
}
|
||
|
||
// 调试日志
|
||
func InfoLog(format string, args ...interface{}) {
|
||
if !enableInfoLog {
|
||
return
|
||
}
|
||
PrintAndWriteLog(logUtil.Info, format, args...)
|
||
}
|
||
|
||
// 警告日志
|
||
func WarnLog(format string, args ...interface{}) {
|
||
if !enableWarnLog {
|
||
return
|
||
}
|
||
PrintAndWriteLog(logUtil.Warn, format, args...)
|
||
}
|
||
|
||
// 调试日志
|
||
func DebugLog(format string, args ...interface{}) {
|
||
if !enableDebugLog {
|
||
return
|
||
}
|
||
PrintAndWriteLog(logUtil.Debug, format, args...)
|
||
}
|
||
|
||
// 错误日志
|
||
func ErrorLog(format string, args ...interface{}) {
|
||
PrintAndWriteLog(logUtil.Error, format, args...)
|
||
}
|
||
|
||
// 致命错误日志
|
||
func FatalLog(format string, args ...interface{}) {
|
||
PrintAndWriteLog(logUtil.Fatal, format, args...)
|
||
}
|
||
|
||
// 打印到控制台并写日志
|
||
func PrintAndWriteLog(logType logUtil.LogType, format string, args ...interface{}) {
|
||
//控制台打印一行
|
||
PrintLog(format, args...)
|
||
|
||
if len(args) <= 0 {
|
||
logUtil.NormalLog(format, logType)
|
||
esLogUtil.NormalLog(format, logType)
|
||
} else {
|
||
logUtil.NormalLog(fmt.Sprintf(format, args...), logType)
|
||
esLogUtil.NormalLog(fmt.Sprintf(format, args...), logType)
|
||
}
|
||
|
||
if debugUtil.IsDebug() && (logType == logUtil.Warn || logType == logUtil.Error || logType == logUtil.Fatal) {
|
||
StdLog(format, args...)
|
||
}
|
||
}
|
||
|
||
// 控制台打印
|
||
func PrintLog(format string, args ...interface{}) {
|
||
//避免非DEBUG模式下组装字符串,提前判断一次
|
||
if debugUtil.IsDebug() {
|
||
debugUtil.Println(fmt.Sprintf(format, args...))
|
||
}
|
||
}
|
||
|
||
// 标准库控制台输出打印
|
||
func StdLog(format string, args ...interface{}) {
|
||
if len(args) <= 0 {
|
||
log.Print(format)
|
||
} else {
|
||
log.Print(fmt.Sprintf(format, args...))
|
||
}
|
||
}
|
||
|
||
// 记录未知错误日志
|
||
// 参数:
|
||
//
|
||
// err:recover获取到的错误对象
|
||
// args:附加参数
|
||
func LogUnknownError(err interface{}, args ...string) {
|
||
logUtil.LogUnknownError(err, args...)
|
||
}
|