From b27db87d79a7aeddc6c348e77b62e4ef5a26da89 Mon Sep 17 00:00:00 2001 From: ross-spencer Date: Sat, 3 Aug 2024 15:44:26 +0200 Subject: [PATCH] Introduce more context to logging A new log formatter is introduced that adds program contex to the log output, e.g. source file, and line of code where a log call is made. Additional program context can be added consistently. Datetime is formatted in ISO format and an option is provided to use UTC or local timezones. --- cmd/roy/roy.go | 13 ++++++++++++ cmd/sf/sf.go | 13 ++++++++++++ internal/logformatter/log.go | 39 ++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 internal/logformatter/log.go diff --git a/cmd/roy/roy.go b/cmd/roy/roy.go index d8edae5d..d8cfdbcf 100644 --- a/cmd/roy/roy.go +++ b/cmd/roy/roy.go @@ -27,6 +27,7 @@ import ( "github.com/richardlehane/siegfried" "github.com/richardlehane/siegfried/internal/chart" + "github.com/richardlehane/siegfried/internal/logformatter" "github.com/richardlehane/siegfried/pkg/config" "github.com/richardlehane/siegfried/pkg/core" "github.com/richardlehane/siegfried/pkg/loc" @@ -116,6 +117,18 @@ Additional flags: Use a different siegfried home directory. ` +func setLogger(utc bool) { + lw := new(logformatter.LogWriter) + lw.Appname = "roy" + lw.UTC = utc + log.SetOutput(lw) +} + +func init() { + // format the application logger before all other init. + setLogger(true) +} + var ( // BUILD, ADD flag sets build = flag.NewFlagSet("build | add", flag.ExitOnError) diff --git a/cmd/sf/sf.go b/cmd/sf/sf.go index 951f3f34..b17c4a0f 100644 --- a/cmd/sf/sf.go +++ b/cmd/sf/sf.go @@ -31,6 +31,7 @@ import ( "github.com/richardlehane/siegfried" "github.com/richardlehane/siegfried/internal/checksum" + "github.com/richardlehane/siegfried/internal/logformatter" "github.com/richardlehane/siegfried/internal/logger" "github.com/richardlehane/siegfried/pkg/config" "github.com/richardlehane/siegfried/pkg/core" @@ -42,6 +43,18 @@ import ( // defaults const maxMulti = 1024 +func setLogger(utc bool) { + lw := new(logformatter.LogWriter) + lw.Appname = "sf" + lw.UTC = utc + log.SetOutput(lw) +} + +func init() { + // format the application logger before all other init. + setLogger(true) +} + // flags var ( updateShort = flag.Bool("u", false, "update or install the default signature file") diff --git a/internal/logformatter/log.go b/internal/logformatter/log.go new file mode 100644 index 00000000..6a2c614d --- /dev/null +++ b/internal/logformatter/log.go @@ -0,0 +1,39 @@ +package logformatter + +import ( + "fmt" + "log" + "os" + "time" +) + +type LogWriter struct { + Appname string + UTC bool +} + +const logTimeFormat = "2006-01-02 15:04:05" + +// Write enables us to format a logging prefix for the application. The +// text will appear before the log message output by the caller. +// +// e.g. +// +// `// 2023-11-27 11:36:57 ERROR :: golang-app:100:main() :: this is an error message, ...some diagnosis` +func (lw *LogWriter) Write(logString []byte) (int, error) { + logTime := time.Now().UTC().Format(logTimeFormat) + if !lw.UTC { + logTime = time.Now().Format(logTimeFormat) + } + return fmt.Fprintf(os.Stderr, "%s :: %s :: %s", + logTime, + lw.Appname, + string(logString), + ) +} + +func init() { + // Configure logging to use a custom log writer with sensible defaults. + log.SetFlags(0 | log.Lshortfile | log.LUTC) + log.SetOutput(new(LogWriter)) +}