1
0
mirror of https://github.com/IceWhaleTech/CasaOS.git synced 2025-07-09 23:45:31 +02:00
Files
CasaOS/pkg/utils/loger/log.go
link f99f49dd7e V0.3.3 (#330)
* switch branches

* update user interface

* switch branch

* switch branch

* change branch

* submit 0.3.3
2022-06-29 11:09:58 +08:00

93 lines
2.4 KiB
Go

/*
* @Author: LinkLeong link@icewhale.com
* @Date: 2022-06-02 15:09:38
* @LastEditors: LinkLeong
* @LastEditTime: 2022-06-27 15:47:49
* @FilePath: /CasaOS/pkg/utils/loger/log.go
* @Description:
* @Website: https://www.casaos.io
* Copyright (c) 2022 by icewhale, All Rights Reserved.
*/
package loger
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"github.com/IceWhaleTech/CasaOS/pkg/config"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
var loggers *zap.Logger
func getFileLogWriter() (writeSyncer zapcore.WriteSyncer) {
// 使用 lumberjack 实现 log rotate
lumberJackLogger := &lumberjack.Logger{
Filename: filepath.Join(config.AppInfo.LogPath, fmt.Sprintf("%s.%s",
config.AppInfo.LogSaveName,
config.AppInfo.LogFileExt,
)),
MaxSize: 10,
MaxBackups: 60,
MaxAge: 1,
Compress: true,
}
return zapcore.AddSync(lumberJackLogger)
}
func LogInit() {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.EpochTimeEncoder
encoder := zapcore.NewJSONEncoder(encoderConfig)
fileWriteSyncer := getFileLogWriter()
core := zapcore.NewTee(
zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), zapcore.InfoLevel),
zapcore.NewCore(encoder, fileWriteSyncer, zapcore.InfoLevel),
)
loggers = zap.New(core)
}
func Info(message string, fields ...zap.Field) {
callerFields := getCallerInfoForLog()
fields = append(fields, callerFields...)
loggers.Info(message, fields...)
}
func Debug(message string, fields ...zap.Field) {
callerFields := getCallerInfoForLog()
fields = append(fields, callerFields...)
loggers.Debug(message, fields...)
}
func Error(message string, fields ...zap.Field) {
callerFields := getCallerInfoForLog()
fields = append(fields, callerFields...)
loggers.Error(message, fields...)
}
func Warn(message string, fields ...zap.Field) {
callerFields := getCallerInfoForLog()
fields = append(fields, callerFields...)
loggers.Warn(message, fields...)
}
func getCallerInfoForLog() (callerFields []zap.Field) {
pc, file, line, ok := runtime.Caller(2) // 回溯两层,拿到写日志的调用方的函数信息
if !ok {
return
}
funcName := runtime.FuncForPC(pc).Name()
funcName = path.Base(funcName) //Base函数返回路径的最后一个元素,只保留函数名
callerFields = append(callerFields, zap.String("func", funcName), zap.String("file", file), zap.Int("line", line))
return
}