Skip to content

Commit

Permalink
Refactored logging configuration setup
Browse files Browse the repository at this point in the history
The default logging configuration has been refactored to support both console and file logging. The logic for generating the default config has been moved from main.go to a new file, server/logging.go. This change allows the application to dynamically choose between console or file logging based on the 'EnableFileLog' flag in the configuration.
  • Loading branch information
willypuzzle committed Dec 3, 2024
1 parent 8e38a57 commit bd90d26
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 29 deletions.
30 changes: 1 addition & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func main() {
cfgJSON := cfg.LoggingCfgJSON
if cfg.LoggingCfgFile == "" && cfgJSON == "" {
// if no logging defined, use default config (console output)
cfgJSON = defaultLoggingConfig()
cfgJSON = server.DefaultLoggingConfig(cfg)
}
err = logger.Configure(cfg.LoggingCfgFile, cfgJSON, nil)
if err != nil {
Expand All @@ -71,31 +71,3 @@ func main() {

srv.Stop()
}

func defaultLoggingConfig() string {
return `
{
"def": {
"type": "console",
"options": {
"out": "stdout"
},
"format": "plain",
"format_options": {
"delim": " ",
"min_level_len": 5,
"min_msg_len": 40,
"enable_color": true,
"enable_caller": true
},
"levels": [
{"id": 5, "name": "debug"},
{"id": 4, "name": "info", "color": 36},
{"id": 3, "name": "warn"},
{"id": 2, "name": "error", "color": 31},
{"id": 1, "name": "fatal", "stacktrace": true},
{"id": 0, "name": "panic", "stacktrace": true}
]
}
}`
}
74 changes: 74 additions & 0 deletions server/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package server

import (
"fmt"
"strings"
)

func DefaultLoggingConfig(cfg *ConfigPushProxy) string {
if cfg.EnableFileLog == false {
return defaultLoggingConsoleLogConfig()
} else {
return defaultLoggingFileLogConfig(cfg.LogFileLocation)
}
}

func defaultLoggingFileLogConfig(filename string) string {
return fmt.Sprintf(`
{
"def": {
"type": "file",
"options": {
"filename": "%s"
},
"format": "plain",
"format_options": {
"delim": " ",
"min_level_len": 5,
"min_msg_len": 40,
"enable_color": true,
"enable_caller": true
},
"levels": [
{"id": 5, "name": "debug"},
{"id": 4, "name": "info", "color": 36},
{"id": 3, "name": "warn"},
{"id": 2, "name": "error", "color": 31},
{"id": 1, "name": "fatal", "stacktrace": true},
{"id": 0, "name": "panic", "stacktrace": true}
]
}
}`, escapeDoubleQuotes(filename))
}

func defaultLoggingConsoleLogConfig() string {
return `
{
"def": {
"type": "console",
"options": {
"out": "stdout"
},
"format": "plain",
"format_options": {
"delim": " ",
"min_level_len": 5,
"min_msg_len": 40,
"enable_color": true,
"enable_caller": true
},
"levels": [
{"id": 5, "name": "debug"},
{"id": 4, "name": "info", "color": 36},
{"id": 3, "name": "warn"},
{"id": 2, "name": "error", "color": 31},
{"id": 1, "name": "fatal", "stacktrace": true},
{"id": 0, "name": "panic", "stacktrace": true}
]
}
}`
}

func escapeDoubleQuotes(input string) string {
return strings.ReplaceAll(input, `"`, `\"`)
}

0 comments on commit bd90d26

Please sign in to comment.