/
compat.go
141 lines (120 loc) · 3.22 KB
/
compat.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package log
import (
"fmt"
"github.com/One-com/gone/log/syslog"
"io"
"os"
)
// New will instantiate a logger with the same functionality (and limitations) as the std lib logger.
func New(out io.Writer, prefix string, flags int) *Logger {
h := NewStdFormatter(SyncWriter(out), prefix, flags)
l := NewLogger(LvlDEFAULT, h) // not a part of the hierarchy
l.DoTime(true)
return l
}
// Output compatible with the standard lib logger
func (l *Logger) Output(calldepth int, s string) error {
return l.output(calldepth, s)
}
// Fatal compatible with the standard lib logger
func (l *Logger) Fatal(v ...interface{}) {
lvl := syslog.LOG_ALERT
s := fmt.Sprint(v...)
l.log(lvl, s)
os.Exit(1)
}
// Fatalf compatible with the standard lib logger
func (l *Logger) Fatalf(format string, v ...interface{}) {
lvl := syslog.LOG_ALERT
s := fmt.Sprintf(format, v...)
l.log(lvl, s)
os.Exit(1)
}
// Fatalln compatible with the standard lib logger
func (l *Logger) Fatalln(v ...interface{}) {
lvl := syslog.LOG_ALERT
s := fmt.Sprint(v...)
l.log(lvl, s)
os.Exit(1)
}
// Panic compatible with the standard lib logger
func (l *Logger) Panic(v ...interface{}) {
lvl := syslog.LOG_ALERT
s := fmt.Sprint(v...)
l.log(lvl, s)
panic(s)
}
// Panicf compatible with the standard lib logger
func (l *Logger) Panicf(format string, v ...interface{}) {
lvl := syslog.LOG_ALERT
s := fmt.Sprintf(format, v...)
l.log(lvl, s)
panic(s)
}
// Panicln compatible with the standard lib logger
func (l *Logger) Panicln(v ...interface{}) {
lvl := syslog.LOG_ALERT
s := fmt.Sprint(v...)
l.log(lvl, s)
panic(s)
}
// Print compatible with the standard lib logger
func (l *Logger) Print(v ...interface{}) {
if lvl, ok := l.DoingDefaultLevel(); ok {
s := fmt.Sprint(v...)
l.log(lvl, s)
}
}
// Printf compatible with the standard lib logger
func (l *Logger) Printf(format string, v ...interface{}) {
if lvl, ok := l.DoingDefaultLevel(); ok {
s := fmt.Sprintf(format, v...)
l.log(lvl, s)
}
}
// Println compatible with the standard lib logger
func (l *Logger) Println(v ...interface{}) {
if lvl, ok := l.DoingDefaultLevel(); ok {
s := fmt.Sprint(v...)
l.log(lvl, s)
}
}
//---
// These functions have been delegated to the swapper, since some of them might
// need to replace the handler.
// If these functions have no meaning for the actual Handler attached, then they
// result in a NOOP.
// Flags compatible with the standard lib logger
func (l *Logger) Flags() int {
return l.h.Flags()
}
// Prefix compatible with the standard lib logger
func (l *Logger) Prefix() string {
return l.h.Prefix()
}
// SetFlags compatible with the standard lib logger
func (l *Logger) SetFlags(flag int) {
// First activate needed book keeping
if flag&(Ldate|Ltime|Lmicroseconds) != 0 {
l.DoTime(true)
}
if flag&(Llongfile|Lshortfile) != 0 {
l.DoCodeInfo(true)
}
l.h.SetFlags(flag)
// De-activate unneeded book keeping
if flag&(Ldate|Ltime|Lmicroseconds) == 0 {
l.DoTime(false)
}
if flag&(Llongfile|Lshortfile) == 0 {
l.DoCodeInfo(false)
}
}
// SetPrefix compatible with the standard lib logger
func (l *Logger) SetPrefix(prefix string) {
l.h.SetPrefix(prefix)
}
// SetOutput compatible with the standard lib logger
func (l *Logger) SetOutput(w io.Writer) {
l.h.SetOutput(w)
}