From dd3a65ec04f3b063bc3e9451db2124cbf343fa4f Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Thu, 29 Jan 2026 16:32:54 +0100 Subject: [PATCH] feat: add logger flags --- cmd/cmd.go | 64 +++++++++++++++++++++++- cmd/flags.go | 26 ++++++++++ go.mod | 5 +- go.sum | 6 ++- internal/generators/clihelp/generator.go | 1 + 5 files changed, 96 insertions(+), 6 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 6f6b1a80a..04de9a8f4 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -1,6 +1,17 @@ package cmd -import "github.com/urfave/cli/v3" +import ( + "context" + "log/slog" + "os" + "strings" + + "github.com/go-acme/lego/v5/log" + "github.com/urfave/cli/v3" + "gitlab.com/greyxor/slogor" +) + +const rfc3339NanoNatural = "2006-01-02T15:04:05.000000000Z07:00" // CreateRootCommand Creates the root CLI command. func CreateRootCommand() *cli.Command { @@ -8,7 +19,13 @@ func CreateRootCommand() *cli.Command { Name: "lego", Usage: "ACME client written in Go", EnableShellCompletion: true, - Commands: CreateCommands(), + Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) { + setUpLogger(cmd) + + return ctx, nil + }, + Flags: CreateLogFlags(), + Commands: CreateCommands(), } } @@ -23,3 +40,46 @@ func CreateCommands() []*cli.Command { createList(), } } + +func setUpLogger(cmd *cli.Command) { + var logger *slog.Logger + + switch cmd.String(flgLogFormat) { + case "json": + opts := &slog.HandlerOptions{ + Level: getLogLeveler(cmd.String(flgLogLevel)), + } + + logger = slog.New(slog.NewJSONHandler(os.Stdout, opts)) + + case "text": + opts := &slog.HandlerOptions{ + Level: getLogLeveler(cmd.String(flgLogLevel)), + } + + logger = slog.New(slog.NewTextHandler(os.Stdout, opts)) + + default: + logger = slog.New(slogor.NewHandler(os.Stderr, + slogor.SetLevel(getLogLeveler(cmd.String(flgLogLevel))), + slogor.SetTimeFormat(rfc3339NanoNatural)), + ) + } + + log.SetDefault(logger) +} + +func getLogLeveler(lvl string) slog.Leveler { + switch strings.ToUpper(lvl) { + case "debug": + return slog.LevelDebug + case "info": + return slog.LevelInfo + case "warn": + return slog.LevelWarn + case "error": + return slog.LevelError + default: + return slog.LevelInfo + } +} diff --git a/cmd/flags.go b/cmd/flags.go index 469a835f7..8db291a56 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -30,6 +30,7 @@ const ( categoryACMEClient = "Flags related to the ACME client:" categoryAdvanced = "Flags related to advanced options:" categoryARI = "Flags related to ACME Renewal Information (ARI) Extension:" + categoryLogs = "Flags related to logs:" ) // Flag names related to the account. @@ -120,6 +121,12 @@ const ( flgDeployHookTimeout = "deploy-hook-timeout" ) +// Flag names related to logs. +const ( + flgLogLevel = "log.level" + flgLogFormat = "log.format" +) + // Flag names related to the specific run command. const ( flgPrivateKey = "private-key" @@ -531,6 +538,25 @@ func createHookFlags() []cli.Flag { } } +func CreateLogFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Category: categoryLogs, + Name: flgLogLevel, + Sources: cli.EnvVars(toEnvName(flgLogLevel)), + Usage: "Set the logging level. Supported values: 'debug', 'info', 'warn', 'error'.", + Value: "info", + }, + &cli.StringFlag{ + Category: categoryLogs, + Name: flgLogFormat, + Sources: cli.EnvVars(toEnvName(flgLogFormat)), + Usage: "Set the logging format. Supported values: 'colored', 'text', 'json'.", + Value: "colored", + }, + } +} + func createRunFlags() []cli.Flag { flags := []cli.Flag{ createDomainFlag(), diff --git a/go.mod b/go.mod index e127d43fc..f8cda9115 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/go-acme/lego/v5 -go 1.24.0 +go 1.25.0 require ( cloud.google.com/go/compute/metadata v0.9.0 @@ -91,6 +91,7 @@ require ( github.com/yandex-cloud/go-genproto v0.43.0 github.com/yandex-cloud/go-sdk/services/dns v0.0.25 github.com/yandex-cloud/go-sdk/v2 v2.37.0 + gitlab.com/greyxor/slogor v1.6.6 golang.org/x/crypto v0.46.0 golang.org/x/net v0.48.0 golang.org/x/oauth2 v0.34.0 @@ -211,7 +212,7 @@ require ( golang.org/x/exp v0.0.0-20241210194714-1829a127f884 // indirect golang.org/x/mod v0.30.0 // indirect golang.org/x/sync v0.19.0 // indirect - golang.org/x/sys v0.39.0 // indirect + golang.org/x/sys v0.40.0 // indirect golang.org/x/tools v0.39.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20251202230838-ff82c1b0f217 // indirect diff --git a/go.sum b/go.sum index 304dff62f..f78a8913e 100644 --- a/go.sum +++ b/go.sum @@ -944,6 +944,8 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +gitlab.com/greyxor/slogor v1.6.6 h1:SE/RbrEhe0dQ3W94wiaPhoQgbAyKme7X7kvcEeDrGOM= +gitlab.com/greyxor/slogor v1.6.6/go.mod h1:aj17VCg12qGr1oqZwc/IqJGe9z0vfKLySCGSkLjyN8s= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g= @@ -1242,8 +1244,8 @@ golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk= -golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ= +golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/internal/generators/clihelp/generator.go b/internal/generators/clihelp/generator.go index 9bb5a71c6..70a4bdb35 100644 --- a/internal/generators/clihelp/generator.go +++ b/internal/generators/clihelp/generator.go @@ -93,6 +93,7 @@ func createStubApp() *cli.Command { Name: "lego", Usage: "ACME client written in Go", Commands: cmd.CreateCommands(), + Flags: cmd.CreateLogFlags(), } }