feat: logger - add new options (#3)

Reviewed-on: twhelp/corev3#3
This commit is contained in:
Dawid Wysokiński 2023-12-16 09:09:29 +00:00
parent e45d63e50d
commit c3d9c7fc3a
3 changed files with 123 additions and 5 deletions

View File

@ -7,17 +7,23 @@ import (
"github.com/urfave/cli/v2"
)
type appMode string
const (
appModeProduction = "production"
appModeDevelopment = "development"
appModeProduction appMode = "production"
appModeDevelopment appMode = "development"
)
func (m appMode) String() string {
return string(m)
}
var (
appFlagMode = &cli.GenericFlag{
Name: "mode",
Value: &EnumValue{
Enum: []string{appModeDevelopment, appModeProduction},
Default: appModeDevelopment,
Enum: []string{appModeDevelopment.String(), appModeProduction.String()},
Default: appModeDevelopment.String(),
},
Usage: fmt.Sprintf("%s or %s", appModeProduction, appModeDevelopment),
EnvVars: []string{"APP_MODE"},
@ -38,12 +44,14 @@ func newApp(name, version string) *appWrapper {
app.HelpName = name
app.Version = version
app.Commands = []*cli.Command{cmdDB, cmdJob}
app.Flags = appFlags
app.Flags = concatSlices(appFlags, logFlags)
app.Before = app.handleBefore
return app
}
func (a *appWrapper) handleBefore(c *cli.Context) error {
a.logger = newLoggerFromFlags(c)
c.Context = loggerToCtx(c.Context, a.logger)
a.logger.Debug("executing command", slog.Any("args", c.Args().Slice()))

View File

@ -2,9 +2,115 @@ package main
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"github.com/urfave/cli/v2"
)
type logLevel string
const (
logLevelDebug logLevel = "debug"
logLevelInfo logLevel = "info"
logLevelWarn logLevel = "warn"
logLevelError logLevel = "error"
)
func (l logLevel) slogLevel() slog.Level {
switch l {
case logLevelDebug:
return slog.LevelDebug
case logLevelInfo:
return slog.LevelInfo
case logLevelWarn:
return slog.LevelWarn
case logLevelError:
return slog.LevelError
default:
panic("unknown log level: " + l)
}
}
func (l logLevel) String() string {
return string(l)
}
type logFormat string
const (
logFormatText logFormat = "text"
logFormatJSON logFormat = "json"
)
func (f logFormat) newHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
switch f {
case logFormatText:
return slog.NewTextHandler(w, opts)
case logFormatJSON:
return slog.NewJSONHandler(w, opts)
default:
panic("unknown log format: " + f)
}
}
func (f logFormat) String() string {
return string(f)
}
var (
logFlagLevel = &cli.GenericFlag{
Name: "log.level",
Value: &EnumValue{
Enum: []string{
logLevelDebug.String(),
logLevelInfo.String(),
logLevelWarn.String(),
logLevelError.String(),
},
Default: logLevelInfo.String(),
},
Usage: fmt.Sprintf("%s, %s, %s or %s", logLevelDebug, logLevelInfo, logLevelWarn, logLevelError),
EnvVars: []string{"LOG_LEVEL"},
}
logFlagFormat = &cli.GenericFlag{
Name: "log.format",
Value: &EnumValue{
Enum: []string{
logFormatJSON.String(),
logFormatText.String(),
},
Default: logFormatText.String(),
},
Usage: fmt.Sprintf("%s or %s", logFormatText, logFormatJSON),
EnvVars: []string{"LOG_FORMAT"},
}
logFlags = []cli.Flag{
logFlagLevel,
logFlagFormat,
}
)
func newLoggerFromFlags(c *cli.Context) *slog.Logger {
return newLogger(loggerConfig{
level: logLevel(c.String(logFlagLevel.Name)),
format: logFormat(c.String(logFlagFormat.Name)),
})
}
type loggerConfig struct {
level logLevel
format logFormat
}
func newLogger(cfg loggerConfig) *slog.Logger {
return slog.New(cfg.format.newHandler(os.Stderr, &slog.HandlerOptions{
Level: cfg.level.slogLevel(),
}))
}
type loggerCtxKey struct{}
func loggerToCtx(ctx context.Context, l *slog.Logger) context.Context {

View File

@ -20,6 +20,8 @@ spec:
env:
- name: APP_MODE
value: development
- name: LOG_LEVEL
value: debug
- name: DB_CONNECTION_STRING
valueFrom:
secretKeyRef:
@ -56,6 +58,8 @@ spec:
env:
- name: APP_MODE
value: development
- name: LOG_LEVEL
value: debug
- name: DB_CONNECTION_STRING
valueFrom:
secretKeyRef: