-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: add flag to specify log format (console, json)
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
- Loading branch information
1 parent
2fbf0da
commit 6f98739
Showing
6 changed files
with
90 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) VirtualTam | ||
// SPDX-License-Identifier: MIT | ||
|
||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
const ( | ||
// Format log messages as pretty-printed key-value pairs. | ||
LogFormatConsole = "console" | ||
|
||
// Format log messages as JSON documents. | ||
LogFormatJSON = "json" | ||
) | ||
|
||
var ( | ||
// Available logger levels. | ||
LogLevelValues = []string{ | ||
zerolog.LevelTraceValue, | ||
zerolog.LevelDebugValue, | ||
zerolog.LevelInfoValue, | ||
zerolog.LevelWarnValue, | ||
zerolog.LevelErrorValue, | ||
zerolog.LevelFatalValue, | ||
zerolog.LevelPanicValue, | ||
} | ||
) | ||
|
||
// SetupGlobalLogger configures the global zerolog.Logger. | ||
func SetupGlobalLogger(logFormat string, logLevelValue string) error { | ||
zerolog.SetGlobalLevel(zerolog.InfoLevel) | ||
|
||
switch logFormat { | ||
case LogFormatConsole: | ||
log.Logger = log.Output(zerolog.ConsoleWriter{ | ||
Out: os.Stderr, | ||
TimeFormat: time.RFC3339, | ||
}) | ||
|
||
case LogFormatJSON: | ||
log.Logger = log.Output(os.Stderr) | ||
|
||
default: | ||
log.Error().Str("format", logFormat).Msg("log: invalid format") | ||
return fmt.Errorf("log: invalid format %q", logFormat) | ||
} | ||
|
||
var logLevel zerolog.Level | ||
|
||
if err := logLevel.UnmarshalText([]byte(logLevelValue)); err != nil { | ||
log.Error().Err(err).Msg("invalid log level") | ||
return err | ||
} | ||
|
||
zerolog.SetGlobalLevel(logLevel) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters