-
Notifications
You must be signed in to change notification settings - Fork 5
/
logging.go
84 lines (68 loc) · 1.52 KB
/
logging.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 logging
import (
"fmt"
"log"
"github.com/hashicorp/logutils"
)
type LogLevel string
const (
Trace = "TRACE"
Debug = "DEBUG"
Info = "INFO"
Warn = "WARN"
Error = "ERROR"
)
var Levels = []logutils.LogLevel{Trace, Debug, Info, Warn, Error}
func ByName(n string) logutils.LogLevel {
for _, l := range Levels {
if n == string(l) {
return l
}
}
return Trace
}
type Logger struct {
simple bool
client string
session string
format string
}
func New(sessid, client string) *Logger {
return &Logger{
simple: false,
client: client,
session: sessid,
format: "[%s] [client=%s] [sid=%s] %s",
}
}
func Simple() *Logger {
return &Logger{
simple: true,
format: "[%s] %s",
}
}
func (l *Logger) logimpl(level LogLevel, format string, data ...interface{}) {
if l.simple {
log.Printf(l.format, level, fmt.Sprintf(format, data...))
} else {
log.Printf(l.format, level, l.client, l.session, fmt.Sprintf(format, data...))
}
}
func (l *Logger) Log(level LogLevel, format string, data ...interface{}) {
l.logimpl(level, format, data...)
}
func (l *Logger) Tracef(format string, data ...interface{}) {
l.Log(Trace, format, data...)
}
func (l *Logger) Debugf(format string, data ...interface{}) {
l.Log(Debug, format, data...)
}
func (l *Logger) Infof(format string, data ...interface{}) {
l.Log(Info, format, data...)
}
func (l *Logger) Warnf(format string, data ...interface{}) {
l.Log(Warn, format, data...)
}
func (l *Logger) Errorf(format string, data ...interface{}) {
l.Log(Error, format, data...)
}