Allow devs to change the width of the logging level column in consolewriter (#87)

* Allow user to change the width of the logging level column
* Change default level width to 0 (no dynamic width)
This commit is contained in:
Dave McCormick 2018-07-25 18:05:55 +01:00 committed by Olivier Poitrey
parent e8a8508f09
commit bae001d86b
1 changed files with 12 additions and 1 deletions

View File

@ -31,6 +31,10 @@ var consoleBufPool = sync.Pool{
},
}
// LevelWidth defines the desired character width of the log level column.
// Default 0 does not trim or pad (variable width based level text, e.g. "INFO" or "ERROR")
var LevelWidth = 0
// ConsoleWriter reads a JSON object per write operation and output an
// optionally colored human readable version on the Out writer.
type ConsoleWriter struct {
@ -55,7 +59,14 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) {
if !w.NoColor {
lvlColor = levelColor(l)
}
level = strings.ToUpper(l)[0:4]
level = strings.ToUpper(l)
if LevelWidth > 0 {
if padding := LevelWidth - len(level); padding > 0 {
level += strings.Repeat(" ", padding)
} else {
level = level[0:LevelWidth]
}
}
}
fmt.Fprintf(buf, "%s |%s| %s",
colorize(formatTime(event[TimestampFieldName]), cDarkGray, !w.NoColor),