Skip to content

Commit

Permalink
Add config option for JSON log output.
Browse files Browse the repository at this point in the history
  • Loading branch information
brocaar committed Jul 13, 2023
1 parent a9bb4ed commit b5e6125
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/chirpstack-gateway-bridge/cmd/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const configTemplate = `[general]
# debug=5, info=4, warning=3, error=2, fatal=1, panic=0
log_level={{ .General.LogLevel }}
# Log in JSON format.
log_json={{ .General.LogJSON }}
# Log to syslog.
#
# When set to true, log messages are being written to syslog.
Expand Down
8 changes: 8 additions & 0 deletions cmd/chirpstack-gateway-bridge/cmd/root_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
func run(cmd *cobra.Command, args []string) error {

tasks := []func() error{
setLogJSON,
setLogLevel,
setSyslog,
printStartMessage,
Expand Down Expand Up @@ -52,6 +53,13 @@ func run(cmd *cobra.Command, args []string) error {
return nil
}

func setLogJSON() error {
if config.C.General.LogJSON {
log.SetFormatter(&log.JSONFormatter{})
}
return nil
}

func setLogLevel() error {
log.SetLevel(log.Level(uint8(config.C.General.LogLevel)))
return nil
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
// Config defines the configuration structure.
type Config struct {
General struct {
LogJSON bool `mapstructure:"log_json"`
LogLevel int `mapstructure:"log_level"`
LogToSyslog bool `mapstructure:"log_to_syslog"`
} `mapstructure:"general"`
Expand Down

0 comments on commit b5e6125

Please sign in to comment.