From bae001d86bbb77870a9576fb4e42ef285937bccb Mon Sep 17 00:00:00 2001 From: Dave McCormick Date: Wed, 25 Jul 2018 18:05:55 +0100 Subject: [PATCH] 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) --- console.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/console.go b/console.go index c957571..98de5d7 100644 --- a/console.go +++ b/console.go @@ -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),