-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_logger.go
90 lines (73 loc) · 1.95 KB
/
file_logger.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package backend
import (
"os"
"github.com/goharbor/harbor/src/common/utils/log"
)
// FileLogger is an implementation of logger.Interface.
// It outputs logs to the specified logfile.
type FileLogger struct {
backendLogger *log.Logger
streamRef *os.File
}
// NewFileLogger crates a new file logger
// nil might be returned
func NewFileLogger(level string, logPath string, depth int) (*FileLogger, error) {
f, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
logLevel := parseLevel(level)
backendLogger := log.New(f, log.NewTextFormatter(), logLevel, depth)
return &FileLogger{
backendLogger: backendLogger,
streamRef: f,
}, nil
}
// Close the opened io stream
// Implements logger.Closer interface
func (fl *FileLogger) Close() error {
if fl.streamRef != nil {
return fl.streamRef.Close()
}
return nil
}
// Debug ...
func (fl *FileLogger) Debug(v ...interface{}) {
fl.backendLogger.Debug(v...)
}
// Debugf with format
func (fl *FileLogger) Debugf(format string, v ...interface{}) {
fl.backendLogger.Debugf(format, v...)
}
// Info ...
func (fl *FileLogger) Info(v ...interface{}) {
fl.backendLogger.Info(v...)
}
// Infof with format
func (fl *FileLogger) Infof(format string, v ...interface{}) {
fl.backendLogger.Infof(format, v...)
}
// Warning ...
func (fl *FileLogger) Warning(v ...interface{}) {
fl.backendLogger.Warning(v...)
}
// Warningf with format
func (fl *FileLogger) Warningf(format string, v ...interface{}) {
fl.backendLogger.Warningf(format, v...)
}
// Error ...
func (fl *FileLogger) Error(v ...interface{}) {
fl.backendLogger.Error(v...)
}
// Errorf with format
func (fl *FileLogger) Errorf(format string, v ...interface{}) {
fl.backendLogger.Errorf(format, v...)
}
// Fatal error
func (fl *FileLogger) Fatal(v ...interface{}) {
fl.backendLogger.Fatal(v...)
}
// Fatalf error
func (fl *FileLogger) Fatalf(format string, v ...interface{}) {
fl.backendLogger.Fatalf(format, v...)
}