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

Allow disabling stack traces, add test. #192

Merged
merged 5 commits into from
Apr 2, 2024
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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
lint:
golangci-lint run -v

test:
@echo Running unit tests...
@go test ./...
7 changes: 5 additions & 2 deletions loggingx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (

// Config handles reading in all the config values available for setting up a logger
type Config struct {
Debug bool `mapstructure:"debug"`
Pretty bool `mapstructure:"pretty"`
Debug bool `mapstructure:"debug"`
Pretty bool `mapstructure:"pretty"`
DisableStacktrace bool `mapstructure:"disable_stacktrace"`
}

// MustViperFlags returns the cobra flags and viper config to prevent code duplication
Expand All @@ -34,4 +35,6 @@ func MustViperFlags(v *viper.Viper, flags *pflag.FlagSet) {
viperx.MustBindFlag(v, "logging.debug", flags.Lookup("debug"))
flags.Bool("pretty", false, "enable pretty (human readable) logging output")
viperx.MustBindFlag(v, "logging.pretty", flags.Lookup("pretty"))
flags.Bool("disable-stacktrace", false, "logging errorf/fatalf will not include stacktraces")
viperx.MustBindFlag(v, "logging.disable_stacktrace", flags.Lookup("disable-stacktrace"))
}
4 changes: 4 additions & 0 deletions loggingx/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func InitLogger(appName string, cfg Config) *zap.SugaredLogger {
lgrCfg.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}

if cfg.DisableStacktrace {
lgrCfg.DisableStacktrace = true
}

l, err := lgrCfg.Build()
if err != nil {
panic(err)
Expand Down
65 changes: 65 additions & 0 deletions loggingx/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package loggingx_test

import (
"testing"

"go.infratographer.com/x/loggingx"

"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)

var expectedMsg = "ohno"

func TestInitLogger(t *testing.T) {
tests := []struct {
name string
cfg loggingx.Config
expectedST bool
}{
{
name: "With all disabled",
cfg: loggingx.Config{Debug: false, Pretty: false, DisableStacktrace: true},
expectedST: false,
},
{
name: "With stacktrace enabled",
cfg: loggingx.Config{Debug: false, Pretty: false, DisableStacktrace: false},
expectedST: true,
},
{
name: "With all enabled",
cfg: loggingx.Config{Debug: true, Pretty: true, DisableStacktrace: false},
expectedST: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger := loggingx.InitLogger("test", tt.cfg)

// Replace the logger's core with the observer core
core, recorded := observer.New(zapcore.DebugLevel)
logger = logger.Desugar().WithOptions(zap.WrapCore(func(_ zapcore.Core) zapcore.Core {
return core
})).Sugar()

// Log an error message
logger.Error(expectedMsg)

// Assert that the message was logged
require.Equal(t, 1, recorded.Len(), "Expected one log entry")
allLogs := recorded.All()
require.Equal(t, expectedMsg, allLogs[0].Message, "Logged message does not match expected")

// Assert that the stacktrace was logged as expected
if tt.expectedST {
require.NotEmpty(t, allLogs[0].Stack, "Expected stacktrace to be logged")
} else {
require.Empty(t, allLogs[0].Stack, "Expected no stacktrace to be logged")
}
})
}
}
Loading