-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
85 lines (73 loc) · 1.53 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
85
package smtp
import (
"fmt"
"io"
"log"
"os"
)
type LogLevel int
const (
// Silent silent log level
Silent LogLevel = iota + 1
// Error error log level
Error
// Warn warn log level
Warn
// Info info log level
Info
// Debug debug log level
Debug
)
// Logger interface is used by Server to report unexpected internal errors and other debug information.
type Logger interface {
LogMode(level LogLevel) Logger
Error(err error, msg string, v ...interface{})
Info(msg string, v ...interface{})
Debug(msg string, v ...interface{})
Warn(msg string, v ...interface{})
}
type logger struct {
l *log.Logger
level LogLevel
}
func (l logger) LogMode(level LogLevel) Logger {
l.level = level
return l
}
func (l logger) Error(err error, msg string, v ...interface{}) {
if l.level < Error {
return
}
l.l.Printf(fmt.Sprintf("error - %s", msg), v...)
l.l.Println(err)
}
func (l logger) Info(msg string, v ...interface{}) {
if l.level < Info {
return
}
l.l.Printf(fmt.Sprintf("info - %s", msg), v...)
}
func (l logger) Debug(msg string, v ...interface{}) {
if l.level < Debug {
return
}
l.l.Printf(fmt.Sprintf("debug - %s", msg), v...)
}
func (l logger) Warn(msg string, v ...interface{}) {
if l.level < Warn {
return
}
l.l.Printf(fmt.Sprintf("warn - %s", msg), v...)
}
func DefaultLogger() Logger {
return &logger{
l: log.New(os.Stdout, "smtp/server ", log.LstdFlags),
level: Info,
}
}
func CreateLogger(w io.Writer) Logger {
return &logger{
l: log.New(w, "smtp/server ", log.LstdFlags),
level: Info,
}
}