Skip to content

Commit

Permalink
Merge pull request #45 from portainer/feat35-logging
Browse files Browse the repository at this point in the history
feat: update logging config
  • Loading branch information
deviantony authored Sep 4, 2023
2 parents 37106d5 + ca5de4f commit 49639b3
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions internal/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,75 @@ func LoggerFromContext(ctx context.Context) *zap.SugaredLogger {
return zap.S()
}

// NewLogger2 creates a new instance of a logger with the specified log level and format.
// The function takes two parameters: logLevel (string) and json (bool).
// logLevel represents the desired log level for the logger.
// json is a flag indicating whether the logger should output logs in JSON format.
// The function returns a pointer to a zap.SugaredLogger and an error if any.
// If successful, the returned logger is set as the global logger using zap.ReplaceGlobals.
// NewLogger creates and configures a new logger.
// It takes the desired log level and a flag that specifies if the logs should be in JSON format.
// The function returns a SugaredLogger and an error if the configuration fails.
func NewLogger(logLevel string, json bool) (*zap.SugaredLogger, error) {
var level = zapcore.InfoLevel
err := level.Set(logLevel)
level, err := parseLogLevel(logLevel)
if err != nil {
return nil, err
}

var config zap.Config
if json {
config = newJSONLoggerConfig(level)
} else {
config = newTextLoggerConfig(level)
}
config := createLoggerConfig(level, json)

logger, err := config.Build()
logger, err := buildLoggerFromConfig(config)
if err != nil {
return nil, err
}

zap.ReplaceGlobals(logger)
return logger.Sugar(), nil
return setGlobalLogger(logger), nil
}

// parseLogLevel converts a string level to a zapcore.Level type.
func parseLogLevel(logLevel string) (zapcore.Level, error) {
var level zapcore.Level
if err := level.Set(logLevel); err != nil {
return zapcore.InfoLevel, err // Default to InfoLevel
}
return level, nil
}

// createLoggerConfig creates a logger configuration based on the given log level and format.
func createLoggerConfig(level zapcore.Level, json bool) zap.Config {
if json {
return newJSONLoggerConfig(level)
}
return newTextLoggerConfig(level)
}

// newTextLoggerConfig returns a development logger config set at the given log level.
func newTextLoggerConfig(logLevel zapcore.Level) zap.Config {
config := zap.NewDevelopmentConfig()
config.Level = zap.NewAtomicLevelAt(logLevel)

if logLevel != zapcore.DebugLevel {
config.DisableStacktrace = true
}

return config
}

// newJSONLoggerConfig returns a production logger config set at the given log level and with JSON encoding.
func newJSONLoggerConfig(logLevel zapcore.Level) zap.Config {
config := zap.NewProductionConfig()
config.Level = zap.NewAtomicLevelAt(logLevel)
config.Encoding = "json"
config.DisableStacktrace = true
config.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.DateTime)
config.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339)
return config
}

// buildLoggerFromConfig builds and returns a logger based on the given configuration.
func buildLoggerFromConfig(config zap.Config) (*zap.Logger, error) {
logger, err := config.Build()
if err != nil {
return nil, err
}
return logger, nil
}

// setGlobalLogger sets the logger as the global logger and returns its SugaredLogger.
func setGlobalLogger(logger *zap.Logger) *zap.SugaredLogger {
zap.ReplaceGlobals(logger)
return logger.Sugar()
}

0 comments on commit 49639b3

Please sign in to comment.