Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce more context to logging #256

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions cmd/roy/roy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

"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,18 +117,23 @@
Use a different siegfried home directory.
`

var discoveredDroidFile string
func setLogger(utc bool) {
lw := new(logformatter.LogWriter)
lw.Appname = "roy"
lw.UTC = utc
log.SetOutput(lw)
}

func init() {
// Retrieve our dynamic defaults.
discoveredDroidFile = config.Droid()
// format the application logger before all other init.
setLogger(true)
}

var (
// BUILD, ADD flag sets
build = flag.NewFlagSet("build | add", flag.ExitOnError)
home = build.String("home", config.Home(), "override the default home directory")
droid = build.String("droid", discoveredDroidFile, "set name/path for DROID signature file")

Check failure on line 136 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 136 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 136 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 136 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 136 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 136 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 136 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.x)

undefined: discoveredDroidFile

Check failure on line 136 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 136 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.x)

undefined: discoveredDroidFile
mi = build.String("mi", "", "set name/path for MIMEInfo signature file")
fdd = build.String("fdd", "", "set name/path for LOC FDD signature file")
locfdd = build.Bool("loc", false, "build a LOC FDD signature file")
Expand Down Expand Up @@ -165,7 +171,7 @@
// HARVEST
harvest = flag.NewFlagSet("harvest", flag.ExitOnError)
harvestHome = harvest.String("home", config.Home(), "override the default home directory")
harvestDroid = harvest.String("droid", discoveredDroidFile, "set name/path for DROID signature file")

Check failure on line 174 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 174 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 174 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 174 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 174 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 174 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 174 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.x)

undefined: discoveredDroidFile

Check failure on line 174 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 174 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.x)

undefined: discoveredDroidFile
harvestChanges = harvest.Bool("changes", false, "harvest the latest PRONOM release-notes.xml file")
_, htimeout, _, _ = config.HarvestOptions()
timeout = harvest.Duration("timeout", htimeout, "set duration before timing-out harvesting requests e.g. 120s")
Expand All @@ -179,7 +185,7 @@
// INSPECT (roy inspect | roy inspect fmt/121 | roy inspect usr/local/mysig.sig | roy inspect 10)
inspect = flag.NewFlagSet("inspect", flag.ExitOnError)
inspectHome = inspect.String("home", config.Home(), "override the default home directory")
inspectDroid = inspect.String("droid", discoveredDroidFile, "set name/path for DROID signature file")

Check failure on line 188 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 188 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 188 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 188 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 188 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 188 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 188 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.x)

undefined: discoveredDroidFile

Check failure on line 188 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 188 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.x)

undefined: discoveredDroidFile
inspectReports = inspect.Bool("reports", false, "build signatures from PRONOM reports (rather than DROID xml)")
inspectExtend = inspect.String("extend", "", "comma separated list of additional signatures")
inspectExtendc = inspect.String("extendc", "", "comma separated list of additional container signatures")
Expand All @@ -196,7 +202,7 @@
// SETS
setsf = flag.NewFlagSet("sets", flag.ExitOnError)
setsHome = setsf.String("home", config.Home(), "override the default home directory")
setsDroid = setsf.String("droid", discoveredDroidFile, "set name/path for DROID signature file")

Check failure on line 205 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 205 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 205 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 205 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 205 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 205 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 205 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.x)

undefined: discoveredDroidFile

Check failure on line 205 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 205 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.x)

undefined: discoveredDroidFile
setsChanges = setsf.Bool("changes", false, "create a pronom-changes.json sets file")
setsList = setsf.String("list", "", "expand comma separated list of format sets")

Expand Down Expand Up @@ -340,7 +346,7 @@
func getOptions() []config.Option {
opts := []config.Option{}
// build options
if *droid != discoveredDroidFile {

Check failure on line 349 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 349 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 349 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 349 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 349 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 349 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 349 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.x)

undefined: discoveredDroidFile

Check failure on line 349 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 349 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.x)

undefined: discoveredDroidFile
opts = append(opts, config.SetDroid(*droid))
}
if *container != config.Container() {
Expand Down Expand Up @@ -450,7 +456,7 @@
opts = append(opts, config.SetVerbose(!*quiet)) // do the opposite, because the flag is quiet and the setting is verbose!
}
// inspect options
if *inspectDroid != discoveredDroidFile {

Check failure on line 459 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 459 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 459 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 459 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 459 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 459 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 459 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.x)

undefined: discoveredDroidFile

Check failure on line 459 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 459 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.x)

undefined: discoveredDroidFile
opts = append(opts, config.SetDroid(*inspectDroid))
}
if *inspectMI != "" {
Expand Down Expand Up @@ -493,7 +499,7 @@
}

func setHarvestOptions() {
if *harvestDroid != discoveredDroidFile {

Check failure on line 502 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 502 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 502 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 502 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 502 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 502 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 502 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.x)

undefined: discoveredDroidFile

Check failure on line 502 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 502 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.x)

undefined: discoveredDroidFile
config.SetDroid(*harvestDroid)()
}
if *harvestHome != config.Home() {
Expand Down Expand Up @@ -521,7 +527,7 @@
}

func setSetsOptions() {
if *setsDroid != discoveredDroidFile {

Check failure on line 530 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 530 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.18)

undefined: discoveredDroidFile

Check failure on line 530 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 530 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 530 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.19)

undefined: discoveredDroidFile

Check failure on line 530 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 530 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.x)

undefined: discoveredDroidFile

Check failure on line 530 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.20)

undefined: discoveredDroidFile

Check failure on line 530 in cmd/roy/roy.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.x)

undefined: discoveredDroidFile
config.SetDroid(*setsDroid)()
}
if *setsHome != config.Home() {
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))
}
Loading