-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
84 lines (70 loc) · 1.8 KB
/
options.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
package gopherlogs
import (
"io"
"os"
"github.com/jpmcb/gopherlogs/pkg/colors"
)
// LoggerOptions an option to be passed to the NewLogger constructor
type LoggerOptions interface {
apply(*CMDLogger) error
}
type loggerOptionAdapter func(*CMDLogger) error
func (l loggerOptionAdapter) apply(o *CMDLogger) error {
err := l(o)
return err
}
func WithTty(isTty bool) LoggerOptions {
return loggerOptionAdapter(func(l *CMDLogger) error {
l.tty = isTty
return nil
})
}
func WithLogVerbosity(verbosity int) LoggerOptions {
return loggerOptionAdapter(func(l *CMDLogger) error {
l.verbosity = verbosity
return nil
})
}
func WithLogLevel(logLevel int) LoggerOptions {
return loggerOptionAdapter(func(l *CMDLogger) error {
l.logLevel = logLevel
return nil
})
}
func WithLeftPadIndent(indent int) LoggerOptions {
return loggerOptionAdapter(func(l *CMDLogger) error {
l.indent = indent
return nil
})
}
func WithTermFileDescriptor(termFd int) LoggerOptions {
return loggerOptionAdapter(func(l *CMDLogger) error {
l.termFd = termFd
return nil
})
}
func WithColor(color colors.Attribute) LoggerOptions {
return loggerOptionAdapter(func(l *CMDLogger) error {
l.logColor = color
return nil
})
}
func WithOutputWriter(writer io.Writer) LoggerOptions {
return loggerOptionAdapter(func(l *CMDLogger) error {
l.output = writer
l.defaultWriter = writer
return nil
})
}
func WithLogFile(filePath string) LoggerOptions {
return loggerOptionAdapter(func(l *CMDLogger) error {
logFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
if err != nil {
l.Warnf("Failed to open log file %q: %v", filePath, err)
return err
}
// Set the output to the new multiwriter while keeping the default writer
l.output = io.MultiWriter(logFile, l.defaultWriter)
return nil
})
}