-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwelog.go
64 lines (52 loc) · 1.25 KB
/
welog.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
package welog
import (
"fmt"
"io"
"os"
)
func GenerateLoggerBuilder() *LoggerBuilder {
return &LoggerBuilder{
logger: &Logger{
level: Info,
mode: Basic,
writer: os.Stderr,
},
}
}
func (builder *LoggerBuilder) WithLogLevel(level LogLevel) *LoggerBuilder {
builder.logger.level = level
return builder
}
func (builder *LoggerBuilder) WithLogMode(mode LogMode) *LoggerBuilder {
builder.logger.mode = mode
return builder
}
func (builder *LoggerBuilder) WithLogFile(hasLogFile LogWrite) *LoggerBuilder {
var writer io.Writer
var file *os.File
var err error
if hasLogFile {
file, err = os.OpenFile("welog-errors.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create log file: %v\n", err)
writer = os.Stderr
} else {
writer = io.MultiWriter(os.Stderr, file)
}
} else {
writer = os.Stderr
}
builder.logger.writeFile = hasLogFile
builder.logger.writer = writer
builder.logger.logFile = file
return builder
}
func (builder *LoggerBuilder) Build() *Logger {
return builder.logger
}
func (logger *Logger) SetOutput(writer io.Writer) {
logger.writer = writer
}
func (logger *Logger) SetFormatter(formatter func(LogLevel, string) string) {
logger.formatter = formatter
}