@@ -18,6 +18,7 @@ import (
18
18
"bufio"
19
19
"fmt"
20
20
"io"
21
+ "log"
21
22
"runtime"
22
23
"strings"
23
24
"time"
@@ -28,7 +29,7 @@ type Formatter interface {
28
29
Flush ()
29
30
}
30
31
31
- func NewStringFormatter (w io.Writer ) * StringFormatter {
32
+ func NewStringFormatter (w io.Writer ) Formatter {
32
33
return & StringFormatter {
33
34
w : bufio .NewWriter (w ),
34
35
}
@@ -104,3 +105,33 @@ func (c *PrettyFormatter) Format(pkg string, l LogLevel, depth int, entries ...i
104
105
func (c * PrettyFormatter ) Flush () {
105
106
c .w .Flush ()
106
107
}
108
+
109
+ // LogFormatter emulates the form of the traditional built-in logger.
110
+ type LogFormatter struct {
111
+ logger * log.Logger
112
+ prefix string
113
+ }
114
+
115
+ // NewLogFormatter is a helper to produce a new LogFormatter struct. It uses the
116
+ // golang log package to actually do the logging work so that logs look similar.
117
+ func NewLogFormatter (w io.Writer , prefix string , flag int ) Formatter {
118
+ return & LogFormatter {
119
+ logger : log .New (w , "" , flag ), // don't use prefix here
120
+ prefix : prefix , // save it instead
121
+ }
122
+ }
123
+
124
+ // Format builds a log message for the LogFormatter. The LogLevel is ignored.
125
+ func (lf * LogFormatter ) Format (pkg string , _ LogLevel , _ int , entries ... interface {}) {
126
+ str := fmt .Sprint (entries ... )
127
+ prefix := lf .prefix
128
+ if pkg != "" {
129
+ prefix = fmt .Sprintf ("%s%s: " , prefix , pkg )
130
+ }
131
+ lf .logger .Output (5 , fmt .Sprintf ("%s%v" , prefix , str )) // call depth is 5
132
+ }
133
+
134
+ // Flush is included so that the interface is complete, but is a no-op.
135
+ func (lf * LogFormatter ) Flush () {
136
+ // noop
137
+ }
0 commit comments