-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger_file_trunc.go
39 lines (34 loc) · 1.04 KB
/
logger_file_trunc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package logrusKit
import (
"github.com/richelieu-yang/chimera/v3/src/core/ioKit"
"github.com/richelieu-yang/chimera/v3/src/file/fileKit"
"github.com/sirupsen/logrus"
"os"
)
// NewTruncFileLogger
/*
@param filePath (1) 如果不存在,会自动创建;
(2) 如果 已经存在 && 是个文件,会截断该文件的内容.
*/
func NewTruncFileLogger(filePath string, options ...LoggerOption) (*logrus.Logger, error) {
file, err := fileKit.Create(filePath)
if err != nil {
return nil, err
}
options = append(options, WithOutput(file))
return NewLogger(options...), nil
}
// NewTruncFileAndStdoutLogger
/*
@param filePath (1) 如果不存在,会自动创建;
(2) 如果 已经存在 && 是个文件,会截断该文件的内容.
*/
func NewTruncFileAndStdoutLogger(filePath string, options ...LoggerOption) (*logrus.Logger, error) {
f, err := fileKit.Create(filePath)
if err != nil {
return nil, err
}
output := ioKit.MultiWriter(f, os.Stdout)
options = append(options, WithOutput(output))
return NewLogger(options...), nil
}