Skip to content

Commit 8cc57c0

Browse files
Tim Cooperrobpike
Tim Cooper
authored andcommitted
log: add Lmsgprefix flag
The Lmsgprefix flag moves the logger's prefix from the beginning of the line to after the log header. For example, a logger with the prefix "LOG " and LstdFlags would output: LOG 2009/11/10 23:00:00 entry text Adding the Lmsgprefix flag would output: 2009/11/10 23:00:00 LOG entry text Fixes #32062 Change-Id: I9f7c9739abeb53c424112aaeed33444eeefdfbbc Reviewed-on: https://go-review.googlesource.com/c/go/+/186182 Run-TryBot: Rob Pike <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rob Pike <[email protected]>
1 parent 24781a1 commit 8cc57c0

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/log/log.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import (
2525

2626
// These flags define which text to prefix to each log entry generated by the Logger.
2727
// 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).
3031
// The prefix is followed by a colon only when Llongfile or Lshortfile
3132
// is specified.
3233
// For example, flags Ldate | Ltime (or LstdFlags) produce,
@@ -40,6 +41,7 @@ const (
4041
Llongfile // full file name and line number: /a/b/c/d.go:23
4142
Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
4243
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
4345
LstdFlags = Ldate | Ltime // initial values for the standard logger
4446
)
4547

@@ -49,15 +51,16 @@ const (
4951
// multiple goroutines; it guarantees to serialize access to the Writer.
5052
type Logger struct {
5153
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)
5355
flag int // properties
5456
out io.Writer // destination for output
5557
buf []byte // for accumulating text to write
5658
}
5759

5860
// New creates a new Logger. The out variable sets the
5961
// 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.
6164
// The flag argument defines the logging properties.
6265
func New(out io.Writer, prefix string, flag int) *Logger {
6366
return &Logger{out: out, prefix: prefix, flag: flag}
@@ -90,11 +93,14 @@ func itoa(buf *[]byte, i int, wid int) {
9093
}
9194

9295
// 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),
9497
// * 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).
96100
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+
}
98104
if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 {
99105
if l.flag&LUTC != 0 {
100106
t = t.UTC()
@@ -138,6 +144,9 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
138144
itoa(buf, line, -1)
139145
*buf = append(*buf, ": "...)
140146
}
147+
if l.flag&Lmsgprefix != 0 {
148+
*buf = append(*buf, l.prefix...)
149+
}
141150
}
142151

143152
// Output writes the output for a logging event. The string s contains

src/log/log_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
Rdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
2121
Rtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
2222
Rmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
23-
Rline = `(57|59):` // must update if the calls to l.Printf / l.Print below move
23+
Rline = `(60|62):` // must update if the calls to l.Printf / l.Print below move
2424
Rlongfile = `.*/[A-Za-z0-9_\-]+\.go:` + Rline
2525
Rshortfile = `[A-Za-z0-9_\-]+\.go:` + Rline
2626
)
@@ -37,6 +37,7 @@ var tests = []tester{
3737
{0, "XXX", "XXX"},
3838
{Ldate, "", Rdate + " "},
3939
{Ltime, "", Rtime + " "},
40+
{Ltime | Lmsgprefix, "XXX", Rtime + " XXX"},
4041
{Ltime | Lmicroseconds, "", Rtime + Rmicroseconds + " "},
4142
{Lmicroseconds, "", Rtime + Rmicroseconds + " "}, // microsec implies time
4243
{Llongfile, "", Rlongfile + " "},
@@ -45,6 +46,8 @@ var tests = []tester{
4546
// everything at once:
4647
{Ldate | Ltime | Lmicroseconds | Llongfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rlongfile + " "},
4748
{Ldate | Ltime | Lmicroseconds | Lshortfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " "},
49+
{Ldate | Ltime | Lmicroseconds | Llongfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmicroseconds + " " + Rlongfile + " XXX"},
50+
{Ldate | Ltime | Lmicroseconds | Lshortfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " XXX"},
4851
}
4952

5053
// Test using Println("hello", 23, "world") or using Printf("hello %d world", 23)

0 commit comments

Comments
 (0)