@@ -25,8 +25,9 @@ import (
25
25
26
26
// These flags define which text to prefix to each log entry generated by the Logger.
27
27
// Bits are or'ed together to control what's printed.
28
- // There is no control over the order they appear (the order listed
29
- // here) or the format they present (as described in the comments).
28
+ // With the exception of the Lmsgprefix flag, there is no
29
+ // control over the order they appear (the order listed here)
30
+ // or the format they present (as described in the comments).
30
31
// The prefix is followed by a colon only when Llongfile or Lshortfile
31
32
// is specified.
32
33
// For example, flags Ldate | Ltime (or LstdFlags) produce,
@@ -40,6 +41,7 @@ const (
40
41
Llongfile // full file name and line number: /a/b/c/d.go:23
41
42
Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
42
43
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
44
+ Lmsgprefix // move the "prefix" from the beginning of the line to before the message
43
45
LstdFlags = Ldate | Ltime // initial values for the standard logger
44
46
)
45
47
@@ -49,15 +51,16 @@ const (
49
51
// multiple goroutines; it guarantees to serialize access to the Writer.
50
52
type Logger struct {
51
53
mu sync.Mutex // ensures atomic writes; protects the following fields
52
- prefix string // prefix to write at beginning of each line
54
+ prefix string // prefix on each line to identify the logger (but see Lmsgprefix)
53
55
flag int // properties
54
56
out io.Writer // destination for output
55
57
buf []byte // for accumulating text to write
56
58
}
57
59
58
60
// New creates a new Logger. The out variable sets the
59
61
// destination to which log data will be written.
60
- // The prefix appears at the beginning of each generated log line.
62
+ // The prefix appears at the beginning of each generated log line, or
63
+ // after the log header if the Lmsgprefix flag is provided.
61
64
// The flag argument defines the logging properties.
62
65
func New (out io.Writer , prefix string , flag int ) * Logger {
63
66
return & Logger {out : out , prefix : prefix , flag : flag }
@@ -90,11 +93,14 @@ func itoa(buf *[]byte, i int, wid int) {
90
93
}
91
94
92
95
// formatHeader writes log header to buf in following order:
93
- // * l.prefix (if it's not blank),
96
+ // * l.prefix (if it's not blank and Lmsgprefix is unset ),
94
97
// * date and/or time (if corresponding flags are provided),
95
- // * file and line number (if corresponding flags are provided).
98
+ // * file and line number (if corresponding flags are provided),
99
+ // * l.prefix (if it's not blank and Lmsgprefix is set).
96
100
func (l * Logger ) formatHeader (buf * []byte , t time.Time , file string , line int ) {
97
- * buf = append (* buf , l .prefix ... )
101
+ if l .flag & Lmsgprefix == 0 {
102
+ * buf = append (* buf , l .prefix ... )
103
+ }
98
104
if l .flag & (Ldate | Ltime | Lmicroseconds ) != 0 {
99
105
if l .flag & LUTC != 0 {
100
106
t = t .UTC ()
@@ -138,6 +144,9 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
138
144
itoa (buf , line , - 1 )
139
145
* buf = append (* buf , ": " ... )
140
146
}
147
+ if l .flag & Lmsgprefix != 0 {
148
+ * buf = append (* buf , l .prefix ... )
149
+ }
141
150
}
142
151
143
152
// Output writes the output for a logging event. The string s contains
0 commit comments