Skip to content

Commit

Permalink
Introduce more context to logging
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ross-spencer committed Oct 25, 2024
1 parent 7cd90a4 commit b27db87
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/roy/roy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions cmd/sf/sf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down
39 changes: 39 additions & 0 deletions internal/logformatter/log.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit b27db87

Please sign in to comment.