Skip to content

Commit

Permalink
log: group "enum" consts and touch-up docs
Browse files Browse the repository at this point in the history
Also updated the level descriptions with their documentation from
logrus.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 0b6333a)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Sep 8, 2023
1 parent 6e8f455 commit d563a41
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions log/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,42 @@ var (
L = logrus.NewEntry(logrus.StandardLogger())
)

type (
loggerKey struct{}
type loggerKey struct{}

// Fields type to pass to `WithFields`, alias from `logrus`.
Fields = logrus.Fields
// Fields type to pass to "WithFields".
type Fields = logrus.Fields

// Level is a logging level
Level = logrus.Level
)

const (
// RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to
// ensure the formatted time is always the same number of characters.
RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
// RFC3339NanoFixed is [time.RFC3339Nano] with nanoseconds padded using
// zeros to ensure the formatted time is always the same number of
// characters.
const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"

// TextFormat represents the text logging format
TextFormat = "text"
// Level is a logging level.
type Level = logrus.Level

// JSONFormat represents the JSON logging format
JSONFormat = "json"

// TraceLevel level.
TraceLevel = logrus.TraceLevel
// Supported log levels.
const (
// TraceLevel level. Designates finer-grained informational events
// than [DebugLevel].
TraceLevel Level = logrus.TraceLevel

// DebugLevel level.
DebugLevel = logrus.DebugLevel
// DebugLevel level. Usually only enabled when debugging. Very verbose
// logging.
DebugLevel Level = logrus.DebugLevel

// InfoLevel level.
InfoLevel = logrus.InfoLevel
// InfoLevel level. General operational entries about what's going on
// inside the application.
InfoLevel Level = logrus.InfoLevel
)

// SetLevel sets log level globally.
// SetLevel sets log level globally. It returns an error if the given
// level is not supported.
//
// level can be one of:
//
// - "trace" ([TraceLevel])
// - "debug" ([DebugLevel])
// - "info" ([InfoLevel])
func SetLevel(level string) error {
lvl, err := logrus.ParseLevel(level)
if err != nil {
Expand All @@ -81,7 +85,16 @@ func GetLevel() Level {
return logrus.GetLevel()
}

// SetFormat sets log output format
// Supported log output formats.
const (
// TextFormat represents the text logging format.
TextFormat = "text"

// JSONFormat represents the JSON logging format.
JSONFormat = "json"
)

// SetFormat sets the log output format ([TextFormat] or [JSONFormat]).
func SetFormat(format string) error {
switch format {
case TextFormat:
Expand Down

0 comments on commit d563a41

Please sign in to comment.