-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
logger.go
84 lines (71 loc) · 2.23 KB
/
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
package smtpmock
import (
"io"
"log"
"os"
)
// Logger interface
type logger interface {
infoActivity(string)
info(string)
warning(string)
error(string)
}
// Custom logger that supports 3 different log levels (info, warning, error)
type eventLogger struct {
eventInfo, eventWarning, eventError *log.Logger
logToStdout, logServerActivity bool
flag int
stdout, stderr io.Writer
}
// Logger builder. Returns pointer to builded new logger structure
func newLogger(logToStdout, logServerActivity bool) *eventLogger {
return &eventLogger{
logToStdout: logToStdout,
logServerActivity: logServerActivity,
flag: logFlag,
stdout: os.Stdout,
stderr: os.Stderr,
}
}
// logger methods
// Provides INFO log level for server activities. Writes to stdout for case when
// logger.logToStdout and logger.logServerActivity are enabled, suppressed otherwise
func (logger *eventLogger) infoActivity(message string) {
if logger.logToStdout && logger.logServerActivity {
if logger.eventInfo == nil {
logger.eventInfo = log.New(logger.stdout, infoLogLevel+": ", logger.flag)
}
logger.eventInfo.Println(message)
}
}
// Provides INFO log level. Writes to stdout for case when logger.logToStdout is enabled,
// suppressed otherwise
func (logger *eventLogger) info(message string) {
if logger.logToStdout {
if logger.eventInfo == nil {
logger.eventInfo = log.New(logger.stdout, infoLogLevel+": ", logger.flag)
}
logger.eventInfo.Println(message)
}
}
// Provides WARNING log level. Writes to stdout for case when logger.logToStdout is enabled,
// suppressed otherwise
func (logger *eventLogger) warning(message string) {
if logger.logToStdout {
if logger.eventWarning == nil {
logger.eventWarning = log.New(logger.stdout, warningLogLevel+": ", logger.flag)
}
logger.eventWarning.Println(message)
}
}
// Provides ERROR log level. Writes to stdout for case when logger.logToStdout is enabled,
// suppressed otherwise
func (logger *eventLogger) error(message string) {
if logger.logToStdout {
if logger.eventError == nil {
logger.eventError = log.New(logger.stderr, errorLogLevel+": ", logger.flag)
}
logger.eventError.Println(message)
}
}