2 Star 0 Fork 0

TeamsHub/backend-gopkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
logs.go 5.57 KB
一键复制 编辑 原始数据 按行查看 历史
HCY 提交于 2024-05-10 13:07 . edit pkg
package ginlog
/*
var logger *logs.BeeLogger
func init() {
logger = logs.NewLogger()
//输出文件号
logger.EnableFuncCallDepth(true)
//异步输出
logger.Async()
//异步输出允许设置缓冲 chan 的大小
//logger.Async(1e3)
//输出到终端
logger.SetLogger(logs.AdapterConsole, `{"level":7,"color":true}`)
//输出到文件
logger.SetLogger(logs.AdapterFile,`{"filename":"project.log","level":7,"maxlines":0,"maxsize":0,"daily":true,"maxdays":10,"color":true}`)
//日志直接调用的层级
logger.SetLogFuncCallDepth(3)
//输出到 ElasticSearch:
//logs.SetLogger(logs.AdapterEs, `{"dsn":"http://localhost:9200/","level":1}`)
}*/
/*
级别依次降低,默认全部打印,但是一般我们在部署环境,可以通过设置界别设置日志级别:
LevelEmergency
LevelAlert
LevelCritical
LevelError
LevelWarning
LevelNotice
LevelInformational
LevelDebug*/
import (
"fmt"
myelastic "gitee.com/wuzheng0709/backend-gopkg/infrastructure/connector/elastic"
"github.com/go-stack/stack"
"github.com/pkg/errors"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"gopkg.in/sohlich/elogrus.v3"
"net"
"os"
"time"
)
// 定义键名
const (
TraceIDKey = "trace_id"
UserIDKey = "user_id"
VersionKey = "version"
ServiceNameKey = "service_name"
)
var logger *logrus.Logger
// debug: 使用text格式, Level是Debug, 打印所有级别
// not debug: 使用json格式, level是Info, 不打印Debug级别
func SetDebug(d bool) {
if d {
format := new(logrus.TextFormatter)
//format.ForceColors = true
format.TimestampFormat = "2006-01-02 15:04:05"
logger.Level = logrus.DebugLevel
logger.Formatter = format
} else {
format := new(logrus.JSONFormatter)
format.TimestampFormat = "2006-01-02 15:04:05"
logger.Level = logrus.InfoLevel
logger.Formatter = format
}
}
func WithField(key string, value interface{}) *logrus.Entry {
return withCaller(2).WithField(key, value)
}
func WithFields(fs logrus.Fields) *logrus.Entry {
return withCaller(2).WithFields(fs)
}
//todo 加入traceId 和 uid 日志染色追踪
/*func StartSpan(ctx context.Context, ) *logrus.Entry {
if ctx == nil {
ctx = context.Background()
}
fields := map[string]interface{}{
UserIDKey: fromUserIDContext(ctx),
TraceIDKey: fromTraceIDContext(ctx),
VersionKey: config.C.Service.Version,
ServiceNameKey: config.C.Service.Name,
}
return logger.WithFields(fields)
}
func fromUserIDContext(ctx context.Context) string {
v := ctx.Value(UserIDKey)
if v != nil {
if s, ok := v.(string); ok {
return s
}
}
return ""
}
func fromTraceIDContext(ctx context.Context) string {
v := ctx.Value(TraceIDKey)
if v != nil {
if s, ok := v.(string); ok {
return s
}
}
return ""
}*/
func withCaller(skip int) *logrus.Entry {
var key = "caller"
var value interface{}
value = fmt.Sprintf("%+v", stack.Caller(skip))
return logger.WithFields(logrus.Fields{key: value})
}
/**
* 使用级别,参照一下
* - Fatal:网站挂了,或者极度不正常
* - Error:跟遇到的用户说对不起,可能有bug
* - Warn:记录一下,某事又发生了
* - Info:提示一切正常
* - debug:没问题,就看看堆栈
**/
func Fatal(args ...interface{}) {
withCaller(2).Fatal(args...)
}
func Fatalf(format string, args ...interface{}) {
withCaller(2).Fatalf(format, args...)
}
func Error(args ...interface{}) {
withCaller(2).Error(args...)
}
func Errorf(format string, args ...interface{}) {
withCaller(2).Errorf(format, args...)
}
func Warn(args ...interface{}) {
withCaller(2).Warn(args...)
}
func Warnf(format string, args ...interface{}) {
withCaller(2).Warnf(format, args...)
}
func Info(args ...interface{}) {
withCaller(2).Info(args...)
}
func Infof(format string, args ...interface{}) {
withCaller(2).Infof(format, args...)
}
func Debug(args ...interface{}) {
withCaller(2).Debug(args...)
}
func Debugf(format string, args ...interface{}) {
withCaller(2).Debugf(format, args...)
}
// 输出日志到es
func configESLogger(esHOst, index string) {
esHook, err := elogrus.NewElasticHook(myelastic.Client, esHOst, logrus.DebugLevel, index)
if err != nil {
fmt.Println("config es logger error:", errors.WithStack(err))
return
}
logger.AddHook(esHook)
}
// 输出日志到文件
func configFileLogger(logPrefix string) {
logWriter, _ := rotatelogs.New(
logPrefix+"_%Y-%m-%d.log",
rotatelogs.WithMaxAge(7*24*time.Hour), // 文件最大保存时间
rotatelogs.WithRotationTime(24*time.Hour), // 日志切割时间间隔
)
writeMap := lfshook.WriterMap{
logrus.InfoLevel: logWriter,
logrus.FatalLevel: logWriter, //错误输出到另一个日志
}
lfHook := lfshook.NewHook(writeMap, &logrus.JSONFormatter{TimestampFormat: "2006-01-02 15:04:05"})
logger.AddHook(lfHook)
}
func ip() string {
netInterfaces, err := net.Interfaces()
if err != nil {
fmt.Println("net.Interfaces failed, err:", err.Error())
return ""
}
for i := 0; i < len(netInterfaces); i++ {
if (netInterfaces[i].Flags & net.FlagUp) != 0 {
if (netInterfaces[i].Name == "本地连接") || (netInterfaces[i].Name == "WLAN") || (netInterfaces[i].Name == "以太网") {
addrs, _ := netInterfaces[i].Addrs()
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
}
}
}
return ""
}
func init() {
logger = &logrus.Logger{
Out: os.Stdout,
Formatter: nil,
Hooks: make(logrus.LevelHooks),
Level: 1,
}
SetDebug(config.C.Debug)
// configESLogger(ip(), "mylog")
configFileLogger("api")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzheng0709/backend-gopkg.git
git@gitee.com:wuzheng0709/backend-gopkg.git
wuzheng0709
backend-gopkg
backend-gopkg
v1.3.11

搜索帮助