mirror of
https://github.com/go-acme/lego
synced 2026-03-14 14:35:48 +01:00
feat: add logger flags
This commit is contained in:
parent
b770f7dc5b
commit
dd3a65ec04
5 changed files with 96 additions and 6 deletions
64
cmd/cmd.go
64
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
cmd/flags.go
26
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(),
|
||||
|
|
|
|||
5
go.mod
5
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
|
||||
|
|
|
|||
6
go.sum
6
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=
|
||||
|
|
|
|||
|
|
@ -93,6 +93,7 @@ func createStubApp() *cli.Command {
|
|||
Name: "lego",
|
||||
Usage: "ACME client written in Go",
|
||||
Commands: cmd.CreateCommands(),
|
||||
Flags: cmd.CreateLogFlags(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue