mirror of
https://github.com/IceWhaleTech/CasaOS.git
synced 2025-09-18 09:31:57 +00:00
* fix bug * updata UI * 0.3.2 ### Added - [Files] Files can now be selected multiple files and downloaded, deleted, moved, etc. - [Apps] Support to modify the application opening address.([#204](https://github.com/IceWhaleTech/CasaOS/issues/204)) ### Changed - [Apps] Hide the display of non-essential environment variables in the application. - [System] Network, disk, cpu, memory, etc. information is modified to be pushed via socket. - [System] Optimize opening speed.([#214](https://github.com/IceWhaleTech/CasaOS/issues/214)) ### Fixed - [System] Fixed the problem that sync data cannot submit the device ID ([#68](https://github.com/IceWhaleTech/CasaOS/issues/68)) - [Files] Fixed the code editor center alignment display problem.([#210](https://github.com/IceWhaleTech/CasaOS/issues/210)) - [Files] Fixed the problem of wrong name when downloading files.([#240](https://github.com/IceWhaleTech/CasaOS/issues/240)) - [System] Fixed the network display as a negative number problem.([#224](https://github.com/IceWhaleTech/CasaOS/issues/224)) * Modify log help class * Fix some bugs in 0.3.2 * Solve the operation file queue problem * Exclude web folders * update UI
93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
/*
|
||
* @Author: LinkLeong link@icewhale.com
|
||
* @Date: 2022-06-02 15:09:38
|
||
* @LastEditors: LinkLeong
|
||
* @LastEditTime: 2022-06-02 17:43:38
|
||
* @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.LogSavePath, fmt.Sprintf("%s.%s",
|
||
config.AppInfo.LogSaveName,
|
||
config.AppInfo.LogFileExt,
|
||
)),
|
||
MaxSize: 100,
|
||
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.DebugLevel),
|
||
zapcore.NewCore(encoder, fileWriteSyncer, zapcore.DebugLevel),
|
||
)
|
||
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
|
||
}
|