-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext_handler.go
46 lines (39 loc) · 965 Bytes
/
text_handler.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
package clog
import (
"log"
"os"
)
// TextHandler logs messages directly to the console
type TextHandler struct {
logger *log.Logger
}
// NewTextHandler creates a new handler to send logs to the console
func NewTextHandler(prefix string, flag int) *TextHandler {
handler := &TextHandler{
logger: log.New(os.Stdout, prefix, flag),
}
return handler
}
// LogEntry sends a log entry with the specified level
func (h *TextHandler) LogEntry(logEntry LogEntry) error {
return h.logger.Output(logEntry.Calldepth+2, h.levelPrefix(logEntry.Level)+logEntry.GetMessage())
}
// SetPrefix sets a prefix on every log message
func (h *TextHandler) SetPrefix(prefix string) Handler {
h.logger.SetPrefix(prefix)
return h
}
func (h *TextHandler) levelPrefix(logLevel LogLevel) string {
switch logLevel {
case LevelError:
return "Error: "
case LevelWarning:
return "Warning: "
default:
return ""
}
}
// Verify interface
var (
_ Handler = &TextHandler{}
)