Skip to content

Commit

Permalink
feat: improve exported defaults (#17)
Browse files Browse the repository at this point in the history
Signed-off-by: Tronje Krop <[email protected]>
  • Loading branch information
Tronje Krop committed Nov 7, 2024
1 parent a8ca715 commit 1312a5d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 66 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.9
0.0.10
48 changes: 28 additions & 20 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@ import (
"golang.org/x/term"
)

// Default values for the log configuration.
const (
// DefaultLevel is the default log level.
DefaultLevel = LevelInfo
// Default report caller flag in configuration.
DefaultCaller = false
// TImeFormat is defining default time format.
DefaultTimeFormat = "2006-01-02 15:04:05.999999"
)

// Default values for the log formatter.
var (
// DefaultLevelColors is the default color mapping for the log levels.
DefaultLevelColors = []string{
ColorPanic, ColorFatal, ColorError,
ColorWarn, ColorInfo, ColorDebug, ColorTrace, ColorField,
}

// DefaultLevelNames is the default name mapping for the log levels.
DefaultLevelNames = []string{
"PANIC", "FATAL", "ERROR", "WARN",
"INFO", "DEBUG", "TRACE", "-",
}

// DefaultErrorName is the default name used for marking errors.
DefaultErrorName = "error"
)

// Log levels.
const (
// LevelPanic is the panic log level.
Expand Down Expand Up @@ -120,26 +148,6 @@ const (
ColorTrace = ColorMagenta
// ColorField is the color code for fields.
ColorField = ColorGray

// TImeFormat is defining default time format.
DefaultTimeFormat = "2006-01-02 15:04:05.999999"
)

var (
// DefaultLevelColors is the default color mapping for the log levels.
DefaultLevelColors = []string{
ColorPanic, ColorFatal, ColorError,
ColorWarn, ColorInfo, ColorDebug, ColorTrace, ColorField,
}

// DefaultLevelNames is the default name mapping for the log levels.
DefaultLevelNames = []string{
"PANIC", "FATAL", "ERROR", "WARN",
"INFO", "DEBUG", "TRACE", "-",
}

// DefaultErrorName is the default name used for marking errors.
DefaultErrorName = "error"
)

// ColorModeString is the color mode used for logging.
Expand Down
83 changes: 38 additions & 45 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ import (
"github.com/tkrop/go-config/log"
)

const (
// Default log level in configuration.
defaultLogLevel = "info"
// Default report caller flag in configuration.
defaultLogCaller = false
)

var (
// otime is a fixed output time string for testing.
otime = "2024-10-01 23:07:13.891012345Z"
Expand Down Expand Up @@ -102,9 +95,9 @@ type setupParams struct {
var testSetupParams = map[string]setupParams{
"read default config": {
config: &log.Config{},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -115,7 +108,7 @@ var testSetupParams = map[string]setupParams{
},
expectLogLevel: log.LevelPanic,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -126,7 +119,7 @@ var testSetupParams = map[string]setupParams{
},
expectLogLevel: log.LevelFatal,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -137,7 +130,7 @@ var testSetupParams = map[string]setupParams{
},
expectLogLevel: log.LevelError,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -148,7 +141,7 @@ var testSetupParams = map[string]setupParams{
},
expectLogLevel: log.LevelWarn,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -159,7 +152,7 @@ var testSetupParams = map[string]setupParams{
},
expectLogLevel: log.LevelWarning,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -170,7 +163,7 @@ var testSetupParams = map[string]setupParams{
},
expectLogLevel: log.LevelInfo,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -181,7 +174,7 @@ var testSetupParams = map[string]setupParams{
},
expectLogLevel: log.LevelDebug,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -192,18 +185,18 @@ var testSetupParams = map[string]setupParams{
},
expectLogLevel: log.LevelTrace,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},

"log level invalid": {
config: &log.Config{
Level: "detail",
Level: "invalid",
},
expectLogLevel: "info",
expectLogLevel: log.LevelInfo,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -212,9 +205,9 @@ var testSetupParams = map[string]setupParams{
config: &log.Config{
TimeFormat: "2024-12-31",
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: "2024-12-31",
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -223,7 +216,7 @@ var testSetupParams = map[string]setupParams{
config: &log.Config{
Caller: true,
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: true,
expectColorMode: log.ColorOff,
Expand All @@ -234,9 +227,9 @@ var testSetupParams = map[string]setupParams{
config: &log.Config{
Formatter: log.FormatterText,
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -245,9 +238,9 @@ var testSetupParams = map[string]setupParams{
config: &log.Config{
Formatter: log.FormatterJSON,
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -256,9 +249,9 @@ var testSetupParams = map[string]setupParams{
config: &log.Config{
Formatter: log.FormatterPretty,
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
},
Expand All @@ -268,94 +261,94 @@ var testSetupParams = map[string]setupParams{
Formatter: log.FormatterPretty,
ColorMode: log.ColorModeOn,
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectColorMode: log.ColorOn,
expectOrderMode: log.OrderOn,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
},

"formatter pretty color-off": {
config: &log.Config{
Formatter: log.FormatterPretty,
ColorMode: log.ColorModeOff,
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
},

"formatter pretty color-levels": {
config: &log.Config{
Formatter: log.FormatterPretty,
ColorMode: log.ColorModeLevels,
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectColorMode: log.ColorLevels,
expectOrderMode: log.OrderOn,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
},

"formatter pretty color-fields": {
config: &log.Config{
Formatter: log.FormatterPretty,
ColorMode: log.ColorModeFields,
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectColorMode: log.ColorFields,
expectOrderMode: log.OrderOn,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
},

"formatter pretty color-any": {
config: &log.Config{
Formatter: log.FormatterPretty,
ColorMode: "any",
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
},

"formatter pretty order-on": {
config: &log.Config{
Formatter: log.FormatterPretty,
OrderMode: log.OrderModeOn,
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOn,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
},

"formatter pretty order-off": {
config: &log.Config{
Formatter: log.FormatterPretty,
OrderMode: log.OrderModeOff,
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOff,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
},

"formatter pretty order-any": {
config: &log.Config{
Formatter: log.FormatterPretty,
OrderMode: "any",
},
expectLogLevel: defaultLogLevel,
expectLogLevel: log.DefaultLevel,
expectTimeFormat: log.DefaultTimeFormat,
expectColorMode: log.ColorOff,
expectOrderMode: log.OrderOff,
expectLogCaller: defaultLogCaller,
expectLogCaller: log.DefaultCaller,
},
}

0 comments on commit 1312a5d

Please sign in to comment.