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
|
package logging
/*
golang's `log` package sucks, so we wrap it.
*/
import (
"fmt"
"log"
"runtime"
)
type Logger struct {
delegate *log.Logger
prefix string
debugHeader string
infoHeader string
warningHeader string
errorHeader string
fatalHeader string
}
const (
levelDebug = "DEBUG"
levelInfo = "INFO"
levelWarning = "WARNING"
levelError = "ERROR"
levelFatal = "FATAL"
)
func NewWrappedLogger(delegate *log.Logger, name string) Logger {
return Logger{
delegate: delegate,
debugHeader: fmt.Sprintf("[%v][%v]", name, levelDebug),
infoHeader: fmt.Sprintf("[%v][%v]", name, levelInfo),
warningHeader: fmt.Sprintf("[%v][%v]", name, levelWarning),
errorHeader: fmt.Sprintf("[%v][%v]", name, levelError),
fatalHeader: fmt.Sprintf("[%v][%v]", name, levelFatal),
}
}
func getCallerInfo() string {
_, file, line, ok := runtime.Caller(2)
if !ok {
file = "???"
line = 0
}
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
return fmt.Sprintf("[%v:%v]", file, line)
}
func (l Logger) Debug(format string, v ...any) {
l.delegate.Printf(l.debugHeader+getCallerInfo()+" "+format, v...)
}
func (l Logger) Info(format string, v ...any) {
l.delegate.Printf(l.infoHeader+getCallerInfo()+" "+format, v...)
}
func (l Logger) Warning(format string, v ...any) {
l.delegate.Printf(l.warningHeader+getCallerInfo()+" "+format, v...)
}
func (l Logger) Error(format string, v ...any) {
l.delegate.Printf(l.errorHeader+getCallerInfo()+" "+format, v...)
}
func (l Logger) Fatal(format string, v ...any) {
l.delegate.Printf(l.fatalHeader+getCallerInfo()+" "+format, v...)
}
|