import (rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"io"
"github.com/sirupsen/logrus"
)
代码示例
logFilePath := fmt.Sprintf("%s/%s/", logFileBasePath, podName)
fileStr := strings.Trim(logFilePath, " \r\n")
var logPath string
index := strings.LastIndex(fileStr, string(os.PathSeparator))
if index >= 0 {
path := fileStr[:index] + "/"
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
os.MkdirAll(path, 0777)
}
}
logPath = path
}
writer, _ := rotatelogs.New(
logPath+"%Y%m%d"+FileSuffix,
rotatelogs.WithLinkName(logPath),
rotatelogs.WithRotationCount(MaxLogFileCount),
rotatelogs.WithRotationTime(time.Hour*24), //24小时分割文件,没有找到按天零点切割的配置,如有需要可自行实现
)
writers := []io.Writer{
writer,
os.Stdout}
fileAndStdoutWriter := io.MultiWriter(writers...)
logrus.SetOutput(fileAndStdoutWriter)
golang logrus日志框架配置标准输出和文件输出,文件按天切割
最新推荐文章于 2024-05-07 08:29:03 发布