1 Star 0 Fork 0

江苏艾雨文承养老机器人有限公司 / aywc_judge

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
LogConfig.go 3.70 KB
一键复制 编辑 原始数据 按行查看 历史
dtal 提交于 2021-06-02 09:26 . init
package configuration
import (
"fmt"
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"github.com/onsi/ginkgo/reporters/stenographer/support/go-colorable"
"github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus"
"github.com/tietang/go-utils"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"io"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"time"
)
var formatter *prefixed.TextFormatter
var lfh *utils.LineNumLogrusHook
func init() {
// 定义日志格式
//formatter:=&log.TextFormatter{} log是小包名后面是struct 要用&
formatter = &prefixed.TextFormatter{}
///
//设置高亮显示的色彩样式
formatter.ForceColors = true
formatter.DisableColors = false
formatter.ForceFormatting = true
formatter.SetColorScheme(&prefixed.ColorScheme{ /// 设置级别压缩
InfoLevelStyle: "green",
WarnLevelStyle: "yellow",
ErrorLevelStyle: "red",
FatalLevelStyle: "41",
PanicLevelStyle: "41",
DebugLevelStyle: "blue",
PrefixStyle: "cyan",
TimestampStyle: "37",
})
//开启完整时间戳输出和时间戳格式
formatter.FullTimestamp = true
//设置时间格式
formatter.TimestampFormat = "2006-01-02.15:04:05.000000"
//设置日志formatter
log.SetFormatter(formatter)
log.SetOutput(colorable.NewColorableStdout())
//日志级别,通过环境变量来设置
// 后期可以变更到配置中来设置
if os.Getenv("log.debug") == "true" { /// os 获取系统环境变量
log.SetLevel(log.DebugLevel)
}
//开启调用函数、文件、代码行信息的输出
log.SetReportCaller(true)
//设置函数、文件、代码行信息的输出的hook
SetLineNumLogrusHook()
}
func SetLineNumLogrusHook() {
lfh = utils.NewLineNumLogrusHook()
lfh.EnableFileNameLog = true
lfh.EnableFuncNameLog = true
log.AddHook(lfh)
}
//将滚动日志writer共享给iris glog output
var log_writer io.Writer
//初始化log配置,配置logrus日志文件滚动生成和
func InitLog(serviceName string) {
//设置日志输出级别
level := GetYamlValue([]string{"config", "log_level"}, 0, "debug").(string)
if level == "debug" {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
lfh.EnableFileNameLog = true
lfh.EnableFuncNameLog = true
//配置日志输出目录
logDir := GetYamlValue([]string{"config", "log_path"}, 0, "/app/logs/").(string)
logName := GetYamlValue([]string{"config", "log_name_" + serviceName}, 0, "default").(string)
logPath := logDir //+ "/logs"
logFilePath, _ := filepath.Abs(logPath)
log.Infof("log dir: %s", logFilePath)
maxAge := time.Hour * 24
rotationTime := time.Hour * 4
os.MkdirAll(logPath, os.ModePerm)
baseLogPath := path.Join(logPath, logName)
//设置滚动日志输出writer
writer, err := rotatelogs.New(
strings.TrimSuffix(baseLogPath, ".log")+".%Y%m%d%H.log",
rotatelogs.WithLinkName(baseLogPath), // 生成软链,指向最新日志文件
rotatelogs.WithMaxAge(maxAge), // 文件最大保存时间
rotatelogs.WithRotationTime(rotationTime), // 日志切割时间间隔
)
if err != nil {
log.Errorf("config local file system logger error. %+v", err)
}
//设置日志文件输出的日志格式
formatter := &log.TextFormatter{}
formatter.CallerPrettyfier = func(frame *runtime.Frame) (function string, file string) {
function = frame.Function
dir, filename := path.Split(frame.File)
f := path.Base(dir)
return function, fmt.Sprintf("%s/%s:%d", f, filename, frame.Line)
}
lfHook := lfshook.NewHook(lfshook.WriterMap{
log.DebugLevel: writer, // 为不同级别设置不同的输出目的
log.InfoLevel: writer,
log.WarnLevel: writer,
log.ErrorLevel: writer,
log.FatalLevel: writer,
log.PanicLevel: writer,
}, formatter)
log.AddHook(lfHook)
//
log_writer = writer
}
Go
1
https://gitee.com/aywc_1/aywc_judge.git
git@gitee.com:aywc_1/aywc_judge.git
aywc_1
aywc_judge
aywc_judge
v0.6.11

搜索帮助