• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hamba / cmd / 14325811295

08 Apr 2025 05:26AM UTC coverage: 70.195% (-3.3%) from 73.529%
14325811295

push

github

web-flow
feat: migrate to github.com/urfave/cli/v3 (#287)

44 of 57 new or added lines in 5 files covered. (77.19%)

7 existing lines in 3 files now uncovered.

252 of 359 relevant lines covered (70.19%)

2.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/log.go
1
package cmd
2

3
import (
4
        "io"
5
        "os"
6

7
        "github.com/ettle/strcase"
8
        "github.com/hamba/logger/v2"
9
        "github.com/hamba/logger/v2/ctx"
10
        "github.com/urfave/cli/v3"
11
)
12

13
// Log flag constants declared for CLI use.
14
const (
15
        FlagLogFormat = "log.format"
16
        FlagLogLevel  = "log.level"
17
        FlagLogCtx    = "log.ctx"
18
)
19

20
// CategoryLog is the log flag category.
21
const CategoryLog = "Logging"
22

23
// LogFlags are flags that configure logging.
24
var LogFlags = Flags{
25
        &cli.StringFlag{
26
                Name:     FlagLogFormat,
27
                Category: CategoryLog,
28
                Usage:    "Specify the format of logs. Supported formats: 'logfmt', 'json', 'console'",
29
                Sources:  cli.EnvVars(strcase.ToSNAKE(FlagLogFormat)),
30
        },
31
        &cli.StringFlag{
32
                Name:     FlagLogLevel,
33
                Category: CategoryLog,
34
                Value:    "info",
35
                Usage:    "Specify the log level. e.g. 'debug', 'info', 'error'.",
36
                Sources:  cli.EnvVars(strcase.ToSNAKE(FlagLogLevel)),
37
        },
38
        &cli.StringMapFlag{
39
                Name:     FlagLogCtx,
40
                Category: CategoryLog,
41
                Usage:    "A list of context field appended to every log. Format: key=value.",
42
                Sources:  cli.EnvVars(strcase.ToSNAKE(FlagLogCtx)),
43
        },
44
}
45

46
// LoggerOptions are options for creating a logger.
47
type LoggerOptions struct {
48
        Writer io.Writer
49
}
50

51
// NewLogger returns a logger configured from the cli.
52
func NewLogger(cmd *cli.Command) (*logger.Logger, error) {
8✔
53
        return NewLoggerWithOptions(cmd, &LoggerOptions{})
8✔
54
}
8✔
55

56
// NewLoggerWithOptions returns a logger configured from the cli.
57
func NewLoggerWithOptions(cmd *cli.Command, opts *LoggerOptions) (*logger.Logger, error) {
8✔
58
        str := cmd.String(FlagLogLevel)
8✔
59
        if str == "" {
8✔
UNCOV
60
                str = "info"
×
UNCOV
61
        }
×
62

63
        lvl, err := logger.LevelFromString(str)
8✔
64
        if err != nil {
9✔
65
                return nil, err
1✔
66
        }
1✔
67

68
        fmtr := newLogFormatter(cmd)
7✔
69

7✔
70
        tags := cmd.StringMap(FlagLogCtx)
7✔
71

7✔
72
        fields := make([]logger.Field, 0, len(tags))
7✔
73
        for k, v := range tags {
8✔
74
                fields = append(fields, ctx.Str(k, v))
1✔
75
        }
1✔
76

77
        w := opts.Writer
7✔
78
        if w == nil {
14✔
79
                w = os.Stdout
7✔
80
        }
7✔
81

82
        return logger.New(w, fmtr, lvl).With(fields...), nil
7✔
83
}
84

85
func newLogFormatter(cmd *cli.Command) logger.Formatter {
7✔
86
        format := cmd.String(FlagLogFormat)
7✔
87
        switch format {
7✔
88
        case "json":
2✔
89
                return logger.JSONFormat()
2✔
90
        case "console":
1✔
91
                return logger.ConsoleFormat()
1✔
92
        default:
4✔
93
                return logger.LogfmtFormat()
4✔
94
        }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc