potato/pkg/rotatelogs_unix.go

38 lines
998 B
Go
Raw Permalink Normal View History

2021-03-23 00:55:26 +08:00
/*
* @Date: 2021-03-22 16:44:06
* @LastEditors: viletyy
2021-06-10 15:37:36 +08:00
* @LastEditTime: 2021-06-10 15:18:37
* @FilePath: /potato/pkg/rotatelogs_unix.go
2021-03-23 00:55:26 +08:00
*/
// +build !windows
2021-06-10 15:37:36 +08:00
package pkg
2021-03-23 00:55:26 +08:00
import (
"os"
"path"
"time"
zaprotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/viletyy/potato/global"
"go.uber.org/zap/zapcore"
)
//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: GetWriteSyncer
//@description: zap logger中加入file-rotatelogs
//@return: zapcore.WriteSyncer, error
func GetWriteSyncer() (zapcore.WriteSyncer, error) {
fileWriter, err := zaprotatelogs.New(
path.Join(global.GO_CONFIG.Zap.Director, "%Y-%m-%d.log"),
zaprotatelogs.WithLinkName(global.GO_CONFIG.Zap.LinkName),
zaprotatelogs.WithMaxAge(7*24*time.Hour),
zaprotatelogs.WithRotationTime(24*time.Hour),
)
if global.GO_CONFIG.Zap.LogInConsole {
return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err
}
return zapcore.AddSync(fileWriter), err
}