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

feat: improve exported defaults (#17) #17

Merged
merged 1 commit into from
Nov 7, 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
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,
},
}
Loading