-
Notifications
You must be signed in to change notification settings - Fork 13
/
log.go
76 lines (62 loc) · 1.61 KB
/
log.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
package main
import (
"fmt"
"io"
"os"
)
// LogLevel identifies logging levels
type LogLevel int
// Definitions of log lgevels
const (
Debug LogLevel = iota
Info
Warning
Error
)
// Logger is the interface used for logging
type Logger interface {
// Info loggs a message of info level
Info(format string, a ...interface{})
Warning(format string, a ...interface{})
Error(format string, a ...interface{})
Fatalf(format string, a ...interface{})
Debug(format string, a ...interface{})
Level(level LogLevel)
}
// ConsoleLogger implements
type ConsoleLogger struct {
W io.Writer
}
// SetOutput sets the output to w
func (nl *ConsoleLogger) SetOutput(w io.Writer) {
nl.W = w
}
func (nl ConsoleLogger) output() io.Writer {
if nl.W == nil {
return os.Stderr
}
return nl.W
}
// Debug in the null logger does nothing
func (nl ConsoleLogger) Debug(format string, a ...interface{}) {
fmt.Fprintf(nl.output(), format, a...)
}
// Info in the null logger does nothing
func (nl ConsoleLogger) Info(format string, a ...interface{}) {
fmt.Fprintf(nl.output(), format, a...)
}
// Warning in the null logger does nothing
func (nl ConsoleLogger) Warning(format string, a ...interface{}) {
fmt.Fprintf(nl.output(), format, a...)
}
// Error in the null logger does nothing
func (nl ConsoleLogger) Error(format string, a ...interface{}) {
fmt.Fprintf(nl.output(), format, a...)
}
// Fatalf in the null logger does nothing
func (nl ConsoleLogger) Fatalf(format string, a ...interface{}) {
fmt.Fprintf(nl.output(), format, a...)
os.Exit(1)
}
// Level in the null logger does nothing
func (nl ConsoleLogger) Level(level LogLevel) {}