From cca0c8d44d6d0860df6dca28bae2fedd3549dad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sw=C3=A4rd?= Date: Thu, 26 Jan 2023 11:26:50 +0100 Subject: [PATCH 1/4] Add line number and file to log messages when at debug level. --- ocis-pkg/log/log.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ocis-pkg/log/log.go b/ocis-pkg/log/log.go index b217c77891..46cdfeb360 100644 --- a/ocis-pkg/log/log.go +++ b/ocis-pkg/log/log.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "os" + "regexp" + "runtime" "strings" "time" @@ -17,6 +19,10 @@ import ( var ( RequestIDString = "request-id" + + // Match all paths outside of ocis. Will break if path does not include '/ocis/', + // but this is intended for debugging purposes. + pathRegex = regexp.MustCompile(`.*/ocis/`) ) func init() { @@ -64,6 +70,19 @@ func NopLogger() Logger { return Logger{zerolog.Nop()} } +type LineInfoHook struct{} + +// Run is a hook to add line info to log messages. +// I found the zerolog example for this here: +// https://github.com/rs/zerolog/issues/22#issuecomment-1127295489 +func (h LineInfoHook) Run(e *zerolog.Event, l zerolog.Level, msg string) { + _, file, line, ok := runtime.Caller(3) + if ok { + file := pathRegex.ReplaceAllString(file, "") + e.Str("line", fmt.Sprintf("%s:%d", file, line)) + } +} + // NewLogger initializes a new logger instance. func NewLogger(opts ...Option) Logger { options := newOptions(opts...) @@ -119,6 +138,11 @@ func NewLogger(opts ...Option) Logger { Timestamp(). Logger().Level(logLevel) + if logLevel == zerolog.DebugLevel { + var lineInfoHook LineInfoHook + logger = logger.Hook(lineInfoHook) + } + return Logger{ logger, } From f7b51d6dfa24787d3527a83c49246e8219fbe9f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sw=C3=A4rd?= Date: Thu, 26 Jan 2023 12:58:07 +0100 Subject: [PATCH 2/4] Skipping regex path removal. --- ocis-pkg/log/log.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ocis-pkg/log/log.go b/ocis-pkg/log/log.go index 46cdfeb360..978ddcbe2d 100644 --- a/ocis-pkg/log/log.go +++ b/ocis-pkg/log/log.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "regexp" "runtime" "strings" "time" @@ -19,10 +18,6 @@ import ( var ( RequestIDString = "request-id" - - // Match all paths outside of ocis. Will break if path does not include '/ocis/', - // but this is intended for debugging purposes. - pathRegex = regexp.MustCompile(`.*/ocis/`) ) func init() { @@ -78,7 +73,6 @@ type LineInfoHook struct{} func (h LineInfoHook) Run(e *zerolog.Event, l zerolog.Level, msg string) { _, file, line, ok := runtime.Caller(3) if ok { - file := pathRegex.ReplaceAllString(file, "") e.Str("line", fmt.Sprintf("%s:%d", file, line)) } } From 77c27826b374cc01a245b5c696920c1397adb257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sw=C3=A4rd?= Date: Mon, 30 Jan 2023 11:28:29 +0100 Subject: [PATCH 3/4] Make line number info available on all log levels. --- ocis-pkg/log/log.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ocis-pkg/log/log.go b/ocis-pkg/log/log.go index 978ddcbe2d..5f60f941c8 100644 --- a/ocis-pkg/log/log.go +++ b/ocis-pkg/log/log.go @@ -132,10 +132,8 @@ func NewLogger(opts ...Option) Logger { Timestamp(). Logger().Level(logLevel) - if logLevel == zerolog.DebugLevel { - var lineInfoHook LineInfoHook - logger = logger.Hook(lineInfoHook) - } + var lineInfoHook LineInfoHook + logger = logger.Hook(lineInfoHook) return Logger{ logger, From 1a01d778ca23897385bfb7a9ecb0d4a49896da5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sw=C3=A4rd?= Date: Tue, 31 Jan 2023 10:27:34 +0100 Subject: [PATCH 4/4] Set line info logging to info log level and lower. --- ocis-pkg/log/log.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ocis-pkg/log/log.go b/ocis-pkg/log/log.go index 5f60f941c8..fda1fa050e 100644 --- a/ocis-pkg/log/log.go +++ b/ocis-pkg/log/log.go @@ -132,8 +132,10 @@ func NewLogger(opts ...Option) Logger { Timestamp(). Logger().Level(logLevel) - var lineInfoHook LineInfoHook - logger = logger.Hook(lineInfoHook) + if logLevel <= zerolog.InfoLevel { + var lineInfoHook LineInfoHook + logger = logger.Hook(lineInfoHook) + } return Logger{ logger,