代码拉取完成,页面将自动刷新
package logger
import (
"fmt"
"io"
"log"
"os"
"strings"
"time"
"github.com/lestrrat-go/file-rotatelogs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
DebugLevel Level = "DEBUG"
InfoLevel Level = "INFO"
WarnLevel Level = "WARN"
ErrorLevel Level = "ERROR"
FatalLevel Level = "FATAL"
logTmFmtWithMS = "2006-01-02 15:04:05.000"
)
// Colors
const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Magenta = "\033[35m"
Cyan = "\033[36m"
White = "\033[37m"
BlueBold = "\033[34;1m"
MagentaBold = "\033[35;1m"
RedBold = "\033[31;1m"
YellowBold = "\033[33;1m"
)
type Level string
func (logLevel Level) String() string {
return strings.ToUpper(string(logLevel))
}
// 把字符串转换为日志级别(数字)
func convertLevel(logLevel Level) zapcore.Level {
// 不区分大小写
var level zapcore.Level
switch logLevel {
case DebugLevel:
level = zap.DebugLevel
case InfoLevel:
level = zap.InfoLevel
case WarnLevel:
level = zap.WarnLevel
case ErrorLevel:
level = zap.ErrorLevel
case FatalLevel:
level = zap.FatalLevel
default:
level = zap.InfoLevel
}
return level
}
func getWriter(filename string, maxAge, rotationTime uint) io.Writer {
// 生成rotatelogs的Logger 实际生成的文件名 demo.log.YYmmddHH
// demo.log是指向最新日志的链接
// 保存7天内的日志,每天分割一次日志
hook, err := rotatelogs.New(
filename+".%Y-%m-%d", // 没有使用go风格反人类的format格式
rotatelogs.WithLinkName(filename),
rotatelogs.WithMaxAge(time.Hour*24*time.Duration(maxAge)),
rotatelogs.WithRotationTime(time.Hour*time.Duration(rotationTime)),
)
if err != nil {
panic(err)
}
return hook
}
func enableConsoleOut() (info, warn zapcore.WriteSyncer) {
writeInfoConsole := zapcore.AddSync(os.Stdout)
writeWarnConsole := zapcore.AddSync(os.Stderr)
return writeInfoConsole, writeWarnConsole
}
func enableFileOut(infoWriter, warnWriter io.Writer) (info, warn zapcore.WriteSyncer) {
writeInfoFile := zapcore.AddSync(infoWriter)
writeWarnFile := zapcore.AddSync(warnWriter)
return writeInfoFile, writeWarnFile
}
// 检查文件是否存在并且创建文件
func CheckFileAndCreate(path string) (err error) {
if _, err := os.Stat(path); err == nil {
return nil
} else {
fmt.Println("path not exists ", path)
err := os.MkdirAll(path, 0711)
if err != nil {
log.Println("Error creating directory")
log.Println(err)
return err
}
return nil
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。