-
Notifications
You must be signed in to change notification settings - Fork 11
/
logger.go
96 lines (76 loc) · 2.21 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
86
87
88
89
90
91
92
93
94
95
96
package freki
import (
"fmt"
stdlog "log"
"os"
"reflect"
)
var logger Logger
func init() {
logger = NewStandardLogger(stdlog.New(os.Stderr, "", stdlog.LstdFlags))
}
func GetDefaultLogger() Logger {
return logger
}
func SetDefaultLogger(log Logger) {
logger = log
logger.Debugf("[freki ] default logger set to %s", reflect.TypeOf(log))
}
type Logger interface {
Debug(args ...interface{})
Debugf(format string, args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Info(args ...interface{})
Infof(format string, args ...interface{})
Panic(args ...interface{})
Panicf(format string, args ...interface{})
Warn(args ...interface{})
Warnf(format string, args ...interface{})
}
type StandardLogger struct {
slog *stdlog.Logger
}
func NewStandardLogger(slog *stdlog.Logger) *StandardLogger {
return &StandardLogger{
slog: slog,
}
}
func (s *StandardLogger) Debug(args ...interface{}) {
s.slog.Printf("DEBUG %s", fmt.Sprint(args...))
}
func (s *StandardLogger) Debugf(format string, args ...interface{}) {
s.Debug(fmt.Sprintf(format, args...))
}
func (s *StandardLogger) Error(args ...interface{}) {
s.slog.Printf("ERROR %s", fmt.Sprint(args...))
}
func (s *StandardLogger) Errorf(format string, args ...interface{}) {
s.Error(fmt.Sprintf(format, args...))
}
func (s *StandardLogger) Fatal(args ...interface{}) {
s.slog.Fatalf("FATAL %s", fmt.Sprint(args...))
}
func (s *StandardLogger) Fatalf(format string, args ...interface{}) {
s.Fatal(fmt.Sprintf(format, args...))
}
func (s *StandardLogger) Info(args ...interface{}) {
s.slog.Printf("INFO %s", fmt.Sprint(args...))
}
func (s *StandardLogger) Infof(format string, args ...interface{}) {
s.Info(fmt.Sprintf(format, args...))
}
func (s *StandardLogger) Panic(args ...interface{}) {
s.slog.Panicf("PANIC %s", fmt.Sprint(args...))
}
func (s *StandardLogger) Panicf(format string, args ...interface{}) {
s.Panic(fmt.Sprintf(format, args...))
}
func (s *StandardLogger) Warn(args ...interface{}) {
s.slog.Printf("WARN %s", fmt.Sprint(args...))
}
func (s *StandardLogger) Warnf(format string, args ...interface{}) {
s.Warn(fmt.Sprintf(format, args...))
}