Skip to content

Commit

Permalink
refactor(log): centralise logging via logutil package
Browse files Browse the repository at this point in the history
- Add a new `logutil` package for centralised logging using `JLog`.
- Migrated all packages to use `logutil.Log` instead of individual `jLog` variables.
- Removed now-unnecessary `LogInit` functions and per-package `jLog` variables.
- Ensures consistent logging configuration across all packages.
  • Loading branch information
JosephKav committed Jan 26, 2025
1 parent 8698634 commit ef1bc58
Show file tree
Hide file tree
Showing 135 changed files with 823 additions and 917 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ linters:
- errorlint
- gci
- gocritic
- goimports
- misspell
- typecheck
- wrapcheck

linters-settings:
goimports:
local-prefixes: github.com/release-argus/Argus
26 changes: 13 additions & 13 deletions cmd/argus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import (
"flag"
"fmt"

_ "modernc.org/sqlite"

cfg "github.com/release-argus/Argus/config"
"github.com/release-argus/Argus/db"
"github.com/release-argus/Argus/testing"
"github.com/release-argus/Argus/util"
logutil "github.com/release-argus/Argus/util/log"
"github.com/release-argus/Argus/web"
_ "modernc.org/sqlite"
)

var (
jLog util.JLog
configFile = flag.String("config.file", "config.yml", "Argus configuration file path.")
configCheckFlag = flag.Bool("config.check", false, "Print the fully-parsed config.")
testCommandsFlag = flag.String("test.commands", "", "Put the name of the Service to test the `commands` of.")
Expand All @@ -48,19 +48,19 @@ func main() {
flag.Visit(func(f *flag.Flag) { flagset[f.Name] = true })

var config cfg.Config
config.Load(*configFile, &flagset, &jLog)
jLog.SetTimestamps(*config.Settings.LogTimestamps())
jLog.SetLevel(config.Settings.LogLevel())
config.Load(*configFile, &flagset)
logutil.Log.SetTimestamps(*config.Settings.LogTimestamps())
logutil.Log.SetLevel(config.Settings.LogLevel())

// config.check
config.Print(configCheckFlag)
// test.*
testing.CommandTest(testCommandsFlag, &config, &jLog)
testing.NotifyTest(testNotifyFlag, &config, &jLog)
testing.ServiceTest(testServiceFlag, &config, &jLog)
testing.CommandTest(testCommandsFlag, &config)
testing.NotifyTest(testNotifyFlag, &config)
testing.ServiceTest(testServiceFlag, &config)

// Count of active services to monitor (if log level INFO or above).
if jLog.Level > 1 {
if logutil.Log.Level > 1 {
// Count active services.
serviceCount := len(config.Order)
for _, key := range config.Order {
Expand All @@ -71,7 +71,7 @@ func main() {

// Log active count.
msg := fmt.Sprintf("Found %d services to monitor:", serviceCount)
jLog.Info(msg, util.LogFrom{}, true)
logutil.Log.Info(msg, logutil.LogFrom{}, true)

// Log names of active services.
for _, key := range config.Order {
Expand All @@ -82,11 +82,11 @@ func main() {
}

// Setup DB and last known service versions.
db.Run(&config, &jLog)
db.Run(&config)

// Track all targets for changes in version and act on any found changes.
go config.Service.Track(&config.Order, &config.OrderMutex)

// Web server.
web.Run(&config, &jLog)
web.Run(&config)
}
8 changes: 4 additions & 4 deletions cmd/argus/main_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright [2024] [Argus]
// Copyright [2025] [Argus]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,7 @@ import (
"time"

"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
logutil "github.com/release-argus/Argus/util/log"
)

func resetFlags() {
Expand All @@ -37,8 +37,8 @@ func resetFlags() {

func TestTheMain(t *testing.T) {
// GIVEN different Configs to test
jLog = *util.NewJLog("WARN", false)
jLog.Testing = true
logutil.Init("WARN", false)
logutil.Log.Testing = true
tests := map[string]struct {
file func(path string, t *testing.T)
outputContains *[]string
Expand Down
13 changes: 7 additions & 6 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import (

"github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/util"
logutil "github.com/release-argus/Argus/util/log"
"github.com/release-argus/Argus/web/metric"
)

// Exec will execute every `Command` for the controller.
func (c *Controller) Exec(logFrom util.LogFrom) error {
func (c *Controller) Exec(logFrom logutil.LogFrom) error {
if c == nil || c.Command == nil || len(*c.Command) == 0 {
return nil
}
Expand Down Expand Up @@ -59,7 +60,7 @@ func (c *Controller) Exec(logFrom util.LogFrom) error {
}

// ExecIndex will execute the `Command` at the given index.
func (c *Controller) ExecIndex(logFrom util.LogFrom, index int) error {
func (c *Controller) ExecIndex(logFrom logutil.LogFrom, index int) error {
if index >= len(*c.Command) {
return nil
}
Expand Down Expand Up @@ -100,17 +101,17 @@ func (c *Controller) ExecIndex(logFrom util.LogFrom, index int) error {
}

// Exec this Command and return any errors encountered.
func (c *Command) Exec(logFrom util.LogFrom) error {
jLog.Info(
func (c *Command) Exec(logFrom logutil.LogFrom) error {
logutil.Log.Info(
fmt.Sprintf("Executing '%s'", c),
logFrom, true)
//#nosec G204 -- Command is user defined.
out, err := exec.Command((*c)[0], (*c)[1:]...).Output()

if err != nil {
jLog.Error(err, logFrom, true)
logutil.Log.Error(err, logFrom, true)
} else {
jLog.Info(string(out), logFrom, string(out) != "")
logutil.Log.Info(string(out), logFrom, string(out) != "")
}

//nolint:wrapcheck
Expand Down
9 changes: 5 additions & 4 deletions command/command_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright [2024] [Argus]
// Copyright [2025] [Argus]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@ import (
"github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
logutil "github.com/release-argus/Argus/util/log"
)

func TestCommand_ApplyTemplate(t *testing.T) {
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestCommand_Exec(t *testing.T) {
releaseStdout := test.CaptureStdout()

// WHEN Exec is called on it
err := tc.cmd.Exec(util.LogFrom{})
err := tc.cmd.Exec(logutil.LogFrom{})

// THEN the stdout is expected
if util.ErrorToString(err) != util.ErrorToString(tc.err) {
Expand Down Expand Up @@ -152,7 +153,7 @@ func TestController_ExecIndex(t *testing.T) {
releaseStdout := test.CaptureStdout()

// WHEN the Command @index is executed
err := controller.ExecIndex(util.LogFrom{}, tc.index)
err := controller.ExecIndex(logutil.LogFrom{}, tc.index)

// THEN the stdout is expected
// err
Expand Down Expand Up @@ -219,7 +220,7 @@ func TestController_Exec(t *testing.T) {
if tc.nilController {
controller = nil
}
err := controller.Exec(util.LogFrom{})
err := controller.Exec(logutil.LogFrom{})

// THEN the stdout is expected
// err
Expand Down
9 changes: 4 additions & 5 deletions command/help_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright [2024] [Argus]
// Copyright [2025] [Argus]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -22,14 +22,13 @@ import (

"github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
logutil "github.com/release-argus/Argus/util/log"
)

func TestMain(m *testing.M) {
// initialise jLog
mainJLog := util.NewJLog("DEBUG", false)
mainJLog.Testing = true
LogInit(mainJLog)
logutil.Init("DEBUG", false)
logutil.Log.Testing = true

// run other tests
exitCode := m.Run()
Expand Down
5 changes: 0 additions & 5 deletions command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ import (
"github.com/release-argus/Argus/web/metric"
)

// LogInit for this package.
func LogInit(log *util.JLog) {
jLog = log
}

// Init the Command Controller.
func (c *Controller) Init(
serviceStatus *status.Status,
Expand Down
1 change: 1 addition & 0 deletions command/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus/testutil"

"github.com/release-argus/Argus/notify/shoutrrr"
shoutrrr_test "github.com/release-argus/Argus/notify/shoutrrr/test"
"github.com/release-argus/Argus/service/status"
Expand Down
7 changes: 1 addition & 6 deletions command/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright [2024] [Argus]
// Copyright [2025] [Argus]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -22,11 +22,6 @@ import (

"github.com/release-argus/Argus/notify/shoutrrr"
"github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/util"
)

var (
jLog *util.JLog
)

// Slice mapping of WebHook.
Expand Down
7 changes: 4 additions & 3 deletions config/defaults.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright [2024] [Argus]
// Copyright [2025] [Argus]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@ import (
"github.com/release-argus/Argus/notify/shoutrrr"
"github.com/release-argus/Argus/service"
"github.com/release-argus/Argus/util"
logutil "github.com/release-argus/Argus/util/log"
"github.com/release-argus/Argus/webhook"
)

Expand Down Expand Up @@ -70,9 +71,9 @@ func (d *Defaults) MapEnvToStruct() {
}

if err != nil {
jLog.Fatal(
logutil.Log.Fatal(
"One or more 'ARGUS_' environment variables are invalid:\n"+err.Error(),
util.LogFrom{}, true)
logutil.LogFrom{}, true)
}
}

Expand Down
17 changes: 9 additions & 8 deletions config/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ import (
dbtype "github.com/release-argus/Argus/db/types"
"github.com/release-argus/Argus/service"
"github.com/release-argus/Argus/util"
logutil "github.com/release-argus/Argus/util/log"
)

// AddService to the config (or replace/rename an existing service).
func (c *Config) AddService(oldServiceID string, newService *service.Service) error {
c.OrderMutex.Lock()
defer c.OrderMutex.Unlock()
logFrom := util.LogFrom{Primary: "AddService"}
logFrom := logutil.LogFrom{Primary: "AddService"}

// Check a service does not already exist with the new id/name, if the name is changing.
if oldServiceID != newService.ID &&
(c.Service[newService.ID] != nil || c.ServiceWithNameExists(newService.ID, oldServiceID)) {
err := fmt.Errorf("service %q already exists", newService.ID)
jLog.Error(err, logFrom, true)
logutil.Log.Error(err, logFrom, true)
return err
}

Expand All @@ -46,7 +47,7 @@ func (c *Config) AddService(oldServiceID string, newService *service.Service) er
!c.Service[oldServiceID].Status.SameVersions(&newService.Status)
// New service.
if oldServiceID == "" || c.Service[oldServiceID] == nil {
jLog.Info("Adding service", logFrom, true)
logutil.Log.Info("Adding service", logFrom, true)
c.Order = append(c.Order, newService.ID)
// Create the service map if it doesn't exist.
//nolint:typecheck
Expand All @@ -58,7 +59,7 @@ func (c *Config) AddService(oldServiceID string, newService *service.Service) er
} else {
// Keeping the same ID.
if oldServiceID == newService.ID {
jLog.Info("Replacing service", logFrom, true)
logutil.Log.Info("Replacing service", logFrom, true)
// Delete the old service.
c.Service[oldServiceID].PrepDelete(false)

Expand Down Expand Up @@ -120,9 +121,9 @@ func (c *Config) RenameService(oldService string, newService *service.Service) {
return
}

jLog.Info(
logutil.Log.Info(
fmt.Sprintf("%q", newService.ID),
util.LogFrom{Primary: "RenameService", Secondary: oldService},
logutil.LogFrom{Primary: "RenameService", Secondary: oldService},
true)
// Replace the service in the order/config.
c.Order = util.ReplaceElement(c.Order, oldService, newService.ID)
Expand All @@ -147,9 +148,9 @@ func (c *Config) DeleteService(serviceID string) {
return
}

jLog.Info(
logutil.Log.Info(
"Deleting service",
util.LogFrom{Primary: "DeleteService", Secondary: serviceID},
logutil.LogFrom{Primary: "DeleteService", Secondary: serviceID},
true)
// Remove the service from the Order.
c.Order = util.RemoveElement(c.Order, serviceID)
Expand Down
Loading

0 comments on commit ef1bc58

Please sign in to comment.