Commit 8cc57c0c authored by Tim Cooper's avatar Tim Cooper Committed by Rob Pike

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 <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRob Pike <r@golang.org>
parent 24781a1f
...@@ -25,8 +25,9 @@ import ( ...@@ -25,8 +25,9 @@ import (
// These flags define which text to prefix to each log entry generated by the Logger. // These flags define which text to prefix to each log entry generated by the Logger.
// Bits are or'ed together to control what's printed. // Bits are or'ed together to control what's printed.
// There is no control over the order they appear (the order listed // With the exception of the Lmsgprefix flag, there is no
// here) or the format they present (as described in the comments). // control over the order they appear (the order listed here)
// or the format they present (as described in the comments).
// The prefix is followed by a colon only when Llongfile or Lshortfile // The prefix is followed by a colon only when Llongfile or Lshortfile
// is specified. // is specified.
// For example, flags Ldate | Ltime (or LstdFlags) produce, // For example, flags Ldate | Ltime (or LstdFlags) produce,
...@@ -40,6 +41,7 @@ const ( ...@@ -40,6 +41,7 @@ const (
Llongfile // full file name and line number: /a/b/c/d.go:23 Llongfile // full file name and line number: /a/b/c/d.go:23
Lshortfile // final file name element and line number: d.go:23. overrides Llongfile Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
Lmsgprefix // move the "prefix" from the beginning of the line to before the message
LstdFlags = Ldate | Ltime // initial values for the standard logger LstdFlags = Ldate | Ltime // initial values for the standard logger
) )
...@@ -49,7 +51,7 @@ const ( ...@@ -49,7 +51,7 @@ const (
// multiple goroutines; it guarantees to serialize access to the Writer. // multiple goroutines; it guarantees to serialize access to the Writer.
type Logger struct { type Logger struct {
mu sync.Mutex // ensures atomic writes; protects the following fields mu sync.Mutex // ensures atomic writes; protects the following fields
prefix string // prefix to write at beginning of each line prefix string // prefix on each line to identify the logger (but see Lmsgprefix)
flag int // properties flag int // properties
out io.Writer // destination for output out io.Writer // destination for output
buf []byte // for accumulating text to write buf []byte // for accumulating text to write
...@@ -57,7 +59,8 @@ type Logger struct { ...@@ -57,7 +59,8 @@ type Logger struct {
// New creates a new Logger. The out variable sets the // New creates a new Logger. The out variable sets the
// destination to which log data will be written. // destination to which log data will be written.
// The prefix appears at the beginning of each generated log line. // The prefix appears at the beginning of each generated log line, or
// after the log header if the Lmsgprefix flag is provided.
// The flag argument defines the logging properties. // The flag argument defines the logging properties.
func New(out io.Writer, prefix string, flag int) *Logger { func New(out io.Writer, prefix string, flag int) *Logger {
return &Logger{out: out, prefix: prefix, flag: flag} return &Logger{out: out, prefix: prefix, flag: flag}
...@@ -90,11 +93,14 @@ func itoa(buf *[]byte, i int, wid int) { ...@@ -90,11 +93,14 @@ func itoa(buf *[]byte, i int, wid int) {
} }
// formatHeader writes log header to buf in following order: // formatHeader writes log header to buf in following order:
// * l.prefix (if it's not blank), // * l.prefix (if it's not blank and Lmsgprefix is unset),
// * date and/or time (if corresponding flags are provided), // * date and/or time (if corresponding flags are provided),
// * file and line number (if corresponding flags are provided). // * file and line number (if corresponding flags are provided),
// * l.prefix (if it's not blank and Lmsgprefix is set).
func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) { func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
*buf = append(*buf, l.prefix...) if l.flag&Lmsgprefix == 0 {
*buf = append(*buf, l.prefix...)
}
if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 {
if l.flag&LUTC != 0 { if l.flag&LUTC != 0 {
t = t.UTC() t = t.UTC()
...@@ -138,6 +144,9 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) { ...@@ -138,6 +144,9 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
itoa(buf, line, -1) itoa(buf, line, -1)
*buf = append(*buf, ": "...) *buf = append(*buf, ": "...)
} }
if l.flag&Lmsgprefix != 0 {
*buf = append(*buf, l.prefix...)
}
} }
// Output writes the output for a logging event. The string s contains // Output writes the output for a logging event. The string s contains
......
...@@ -20,7 +20,7 @@ const ( ...@@ -20,7 +20,7 @@ const (
Rdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]` Rdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
Rtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]` Rtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
Rmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]` Rmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
Rline = `(57|59):` // must update if the calls to l.Printf / l.Print below move Rline = `(60|62):` // must update if the calls to l.Printf / l.Print below move
Rlongfile = `.*/[A-Za-z0-9_\-]+\.go:` + Rline Rlongfile = `.*/[A-Za-z0-9_\-]+\.go:` + Rline
Rshortfile = `[A-Za-z0-9_\-]+\.go:` + Rline Rshortfile = `[A-Za-z0-9_\-]+\.go:` + Rline
) )
...@@ -37,6 +37,7 @@ var tests = []tester{ ...@@ -37,6 +37,7 @@ var tests = []tester{
{0, "XXX", "XXX"}, {0, "XXX", "XXX"},
{Ldate, "", Rdate + " "}, {Ldate, "", Rdate + " "},
{Ltime, "", Rtime + " "}, {Ltime, "", Rtime + " "},
{Ltime | Lmsgprefix, "XXX", Rtime + " XXX"},
{Ltime | Lmicroseconds, "", Rtime + Rmicroseconds + " "}, {Ltime | Lmicroseconds, "", Rtime + Rmicroseconds + " "},
{Lmicroseconds, "", Rtime + Rmicroseconds + " "}, // microsec implies time {Lmicroseconds, "", Rtime + Rmicroseconds + " "}, // microsec implies time
{Llongfile, "", Rlongfile + " "}, {Llongfile, "", Rlongfile + " "},
...@@ -45,6 +46,8 @@ var tests = []tester{ ...@@ -45,6 +46,8 @@ var tests = []tester{
// everything at once: // everything at once:
{Ldate | Ltime | Lmicroseconds | Llongfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rlongfile + " "}, {Ldate | Ltime | Lmicroseconds | Llongfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rlongfile + " "},
{Ldate | Ltime | Lmicroseconds | Lshortfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " "}, {Ldate | Ltime | Lmicroseconds | Lshortfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " "},
{Ldate | Ltime | Lmicroseconds | Llongfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmicroseconds + " " + Rlongfile + " XXX"},
{Ldate | Ltime | Lmicroseconds | Lshortfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " XXX"},
} }
// Test using Println("hello", 23, "world") or using Printf("hello %d world", 23) // Test using Println("hello", 23, "world") or using Printf("hello %d world", 23)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment