From 01e192edc9e11ceb92dd02eb79efde087838831a Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Tue, 4 Oct 2022 23:18:48 -0400 Subject: logging: Better `console` encoder defaults (#5109) This is something that has bothered me for a while, so I figured I'd do something about it now since I'm playing in the logging code lately. The `console` encoder doesn't actually match the defaults that zap's default logger uses. This makes it match better with the rest of the logs when using the `console` encoder alongside somekind of filter, which requires you to configure an encoder to wrap. --- modules/logging/encoders.go | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'modules/logging') diff --git a/modules/logging/encoders.go b/modules/logging/encoders.go index 38083db..d8825d3 100644 --- a/modules/logging/encoders.go +++ b/modules/logging/encoders.go @@ -45,15 +45,21 @@ func (ConsoleEncoder) CaddyModule() caddy.ModuleInfo { // Provision sets up the encoder. func (ce *ConsoleEncoder) Provision(_ caddy.Context) error { + if ce.LevelFormat == "" { + ce.LevelFormat = "color" + } + if ce.TimeFormat == "" { + ce.TimeFormat = "wall_milli" + } ce.Encoder = zapcore.NewConsoleEncoder(ce.ZapcoreEncoderConfig()) return nil } // UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax: // -// console { -// -// } +// console { +// +// } // // See the godoc on the LogEncoderConfig type for the syntax of // subdirectives that are common to most/all encoders. @@ -92,9 +98,9 @@ func (je *JSONEncoder) Provision(_ caddy.Context) error { // UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax: // -// json { -// -// } +// json { +// +// } // // See the godoc on the LogEncoderConfig type for the syntax of // subdirectives that are common to most/all encoders. @@ -127,19 +133,18 @@ type LogEncoderConfig struct { // UnmarshalCaddyfile populates the struct from Caddyfile tokens. Syntax: // -// { -// message_key -// level_key -// time_key -// name_key -// caller_key -// stacktrace_key -// line_ending -// time_format -// duration_format -// level_format -// } -// +// { +// message_key +// level_key +// time_key +// name_key +// caller_key +// stacktrace_key +// line_ending +// time_format +// duration_format +// level_format +// } func (lec *LogEncoderConfig) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { for nesting := d.Nesting(); d.NextBlock(nesting); { subdir := d.Val() -- cgit v1.2.3