-
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.
feat(cmd): Add global flag --log-level to change Logger level (#139)
### TL;DR Moved logger initialization into the CLI command's `Before` hook and added log level configuration via flags. ### What changed? - Relocated logger initialization from `main.go` to `cmd.cmd.go`'s `beforeFunc` - Added a new `--log-level` flag with environment variable support - Logger is now stored in the context and retrieved when needed - Removed logger parameter from `New()` and `serveCommand()` - Added validation for log level values ### How to test? 1. Run the application with different log levels: ```bash go run . --log-level debug serve go run . --log-level info serve ``` 2. Set log level via environment variable: ```bash LOG_LEVEL=warn go run . serve ``` 3. Verify logger output format: - In terminal: human-readable console output - In non-terminal: JSON output ### Why make this change? This change improves the application's logging configuration by: - Allowing runtime log level configuration - Following CLI best practices by using the context for dependency injection - Maintaining proper separation of concerns with logging initialization - Providing consistent logging format based on the execution environment
- Loading branch information
Showing
3 changed files
with
52 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,64 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/urfave/cli/v3" | ||
"golang.org/x/term" | ||
) | ||
|
||
// Version defines the version of the binary, and is meant to be set with ldflags at build time. | ||
// | ||
//nolint:gochecknoglobals | ||
var Version = "dev" | ||
|
||
func New(logger zerolog.Logger) *cli.Command { | ||
func New() *cli.Command { | ||
return &cli.Command{ | ||
Name: "ncps", | ||
Usage: "Nix Binary Cache Proxy Service", | ||
Version: Version, | ||
Before: beforeFunc, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "log-level", | ||
Usage: "Set the log level", | ||
Sources: cli.EnvVars("LOG_LEVEL"), | ||
Value: "info", | ||
Validator: func(lvl string) error { | ||
_, err := zerolog.ParseLevel(lvl) | ||
|
||
return err | ||
}, | ||
}, | ||
}, | ||
Commands: []*cli.Command{ | ||
serveCommand(logger), | ||
serveCommand(), | ||
}, | ||
} | ||
} | ||
|
||
func beforeFunc(ctx context.Context, cmd *cli.Command) (context.Context, error) { | ||
logLvl := cmd.String("log-level") | ||
|
||
lvl, err := zerolog.ParseLevel(logLvl) | ||
if err != nil { | ||
return ctx, fmt.Errorf("error parsing the log-level %q: %w", logLvl, err) | ||
} | ||
|
||
var output io.Writer = os.Stdout | ||
|
||
if term.IsTerminal(int(os.Stdout.Fd())) { | ||
output = zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339} | ||
} | ||
|
||
log := zerolog.New(output).Level(lvl) | ||
|
||
log.Info().Str("log-level", lvl.String()).Msg("logger created") | ||
|
||
return log.WithContext(ctx), 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