Documentation ¶
Overview ¶
Package zapflag implements functions to use go.uber.org/zap with github.com/urfave/cli.
Example ¶
zf := zapflag.New(clix.FlagPrefix("EXAMPLE_")) app := cli.NewApp() app.Name = "example" app.HelpName = "example" app.Flags = zf.Flags() app.Before = zf.InitGlobal app.After = func(c *cli.Context) error { _ = zap.L().Sync() return nil } app.Action = func(c *cli.Context) error { zap.L().Info("hello world") return nil } _ = app.Run([]string{"example", "--help"})
Output: NAME: example - A new cli application USAGE: example [global options] command [command options] [arguments...] COMMANDS: help, h Shows a list of commands or help for one command GLOBAL OPTIONS: --log-development, --ld enable development mode (default: false) [$EXAMPLE_LOG_DEVELOPMENT, $EXAMPLE_LD] --log-level level, --ll level level [debug|info|warn|error|dpanic|panic|fatal] (default: auto) [$EXAMPLE_LOG_LEVEL, $EXAMPLE_LL] --log-with-caller, --lwc whether including caller (default: auto) [$EXAMPLE_LOG_WITH_CALLER, $EXAMPLE_LWC] --log-stack-trace, --lst whether including stack trace (default: auto) [$EXAMPLE_LOG_STACK_TRACE, $EXAMPLE_LST] --log-stack-trace-level level, --lstl level level [debug|info|warn|error|dpanic|panic|fatal] (default: auto) [$EXAMPLE_LOG_STACK_TRACE_LEVEL, $EXAMPLE_LSTL] --log-date-format value, --ldf value see https://pkg.go.dev/time#Time.Format (default: auto) [$EXAMPLE_LOG_DATE_FORMAT, $EXAMPLE_LDF] --log-field key=value, --lf key=value key=value added to the logger [$EXAMPLE_LOG_FIELD, $EXAMPLE_LF] --log-path value, --lp value output path (default: auto) [$EXAMPLE_LOG_PATH, $EXAMPLE_LP] --log-err-path value, --lep value error output path (default: auto) [$EXAMPLE_LOG_ERR_PATH, $EXAMPLE_LEP] --log-sampling-initial N, --lsi N sampling initial count N (default: auto) [$EXAMPLE_LOG_SAMPLING_INITIAL, $EXAMPLE_LSI] --log-sampling-thereafter N, --lsth N sampling thereafter count N (default: auto) [$EXAMPLE_LOG_SAMPLING_THEREAFTER, $EXAMPLE_LSTH] --help, -h show help (default: false)
Index ¶
- func IgnoreError(error) error
- func Named(logger *zap.Logger, c *cli.Context) *zap.Logger
- func ParseZapFields(ss []string) []zap.Field
- func Sync(p **zap.Logger, fn ...ErrorFunc) func(*cli.Context) error
- func SyncGlobal(fn ...ErrorFunc) func(*cli.Context) error
- func ZapFields(v url.Values) []zap.Field
- func ZapTimeEncoder(v string) zapcore.TimeEncoder
- type ErrorFunc
- type Flags
- func (f *Flags) Config() zap.Config
- func (f *Flags) DateFormat() string
- func (f *Flags) Development() bool
- func (f *Flags) ErrPaths() []string
- func (f *Flags) Fields() []string
- func (f *Flags) Flags() []cli.Flag
- func (f *Flags) Init(c *cli.Context) error
- func (f *Flags) InitGlobal(c *cli.Context) error
- func (f *Flags) InitVar(p **zap.Logger) func(*cli.Context) error
- func (f *Flags) Level() zapcore.Level
- func (f *Flags) Logger() (*zap.Logger, error)
- func (f *Flags) LookupDateFormat() (string, bool)
- func (f *Flags) LookupErrPaths() ([]string, bool)
- func (f *Flags) LookupFields() ([]string, bool)
- func (f *Flags) LookupLevel() (zapcore.Level, bool)
- func (f *Flags) LookupPaths() ([]string, bool)
- func (f *Flags) LookupSamplingInitial() (int, bool)
- func (f *Flags) LookupSamplingThereafter() (int, bool)
- func (f *Flags) LookupStackTrace() (bool, bool)
- func (f *Flags) LookupStackTraceLv() (zapcore.Level, bool)
- func (f *Flags) LookupWithCaller() (bool, bool)
- func (f *Flags) Options() []zap.Option
- func (f *Flags) Paths() []string
- func (f *Flags) RootLogger() (*zap.Logger, error)
- func (f *Flags) SamplingConfig() *zap.SamplingConfig
- func (f *Flags) SamplingInitial() int
- func (f *Flags) SamplingThereafter() int
- func (f *Flags) StackTrace() bool
- func (f *Flags) StackTraceLv() zapcore.Level
- func (f *Flags) WithCaller() bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Named ¶
Named adds a new path segment of command/subcommand name to the logger's name.
Example ¶
log := zap.NewExample() app := cli.NewApp() app.Name = "example" app.Action = func(c *cli.Context) error { return nil } app.Before = func(c *cli.Context) error { log := zapflag.Named(log, c) log.Info("named logger for root") return nil } app.Commands = []*cli.Command{ { Name: "hello", Action: func(c *cli.Context) error { return nil }, Before: func(c *cli.Context) error { log := zapflag.Named(log, c) log.Info("named logger for hello") return nil }, Subcommands: []*cli.Command{ { Name: "world", Action: func(c *cli.Context) error { return nil }, Before: func(c *cli.Context) error { log := zapflag.Named(log, c) log.Info("named logger for world") return nil }, Subcommands: []*cli.Command{ { Name: "melon", Action: func(c *cli.Context) error { return nil }, Before: func(c *cli.Context) error { log := zapflag.Named(log, c) log.Info("named logger for melon") return nil }, }, }, }, }, }, } _ = app.Run([]string{"example", "hello", "world", "melon"})
Output: {"level":"info","logger":"example","msg":"named logger for root"} {"level":"info","logger":"example.hello","msg":"named logger for hello"} {"level":"info","logger":"example.hello.world","msg":"named logger for world"} {"level":"info","logger":"example.hello.world.melon","msg":"named logger for melon"}
func ParseZapFields ¶
ParseZapFields converts ss into []zap.Field. Each string of ss must have format 'key=value'. The key can contain '.'.
func Sync ¶
Sync calls Sync method of *zap.Logger of *p, returns its result. If fn is specified, the result is converted by them before returned.
This is intended to be used as cli.AfterFunc. see https://pkg.go.dev/github.com/urfave/cli/v2#AfterFunc
func SyncGlobal ¶
SyncGlobal calls zap.L().Sync(), returns its result. If fn is specified, the result is converted by them before returned.
This is intended to be used as cli.AfterFunc. see https://pkg.go.dev/github.com/urfave/cli/v2#AfterFunc
func ZapFields ¶
ZapFields converts v into []zap.Field.
For each key in v, if len(v[key]) is less than 2 the element is a result of zap.String. Otherwise the element is a result of zap.Strings.
func ZapTimeEncoder ¶
func ZapTimeEncoder(v string) zapcore.TimeEncoder
ZapTimeEncoder converts v into zapcore.TimeEncoder.
If v is equal to "rfc3339nano" or "RFC3339Nano", it returns zapcore.RFC3339NanoTimeEncoder. If v is equal to "rfc3339" or "RFC3339", it returns zapcore.RFC3339TimeEncoder. If v is equal to "iso8601" or "ISO8601", it returns zapcore.ISO8601TimeEncoder. If v is equal to "millis", it returns zapcore.EpochMillisTimeEncoder. If v is equal to "nanos", it returns zapcore.EpochNanosTimeEncoder. If v is equal to "unix", "epoch" or "", it returns zapcore.EpochTimeEncoder. Otherwise it returns zapcore.TimeEncoderOfLayout(v).
Types ¶
type Flags ¶
type Flags struct { Name string FlagDevelopment *cli.BoolFlag FlagLevel *cli.GenericFlag FlagWithCaller *cli.BoolFlag FlagStackTrace *cli.BoolFlag FlagStackTraceLv *cli.GenericFlag FlagDateFormat *cli.StringFlag FlagFields *cli.StringSliceFlag FlagPaths *cli.StringSliceFlag FlagErrPaths *cli.StringSliceFlag FlagSamplingInitial *cli.IntFlag FlagSamplingThereafter *cli.IntFlag FlagSet clix.FlagSet }
Flags represents flags to build a zap.Logger.
func (*Flags) Config ¶
Config returns zap.Config initialized by f's values.
Following values affects the result.
f.Development() f.LookupLevel() f.LookupStackTrace() f.LookupDateFormat() f.LookupPaths() f.LookupErrPaths() f.SamplingConfig()
func (*Flags) DateFormat ¶
DateFormat returns the value of f.FlagDateFormat.
func (*Flags) Development ¶
Development returns the value of f.FlagDevelopment.
func (*Flags) Flags ¶
func (f *Flags) Flags() []cli.Flag
Flags returns []cli.Flag.
Elements are following.
f.FlagDevelopment f.FlagLevel f.FlagWithCaller f.FlagStackTrace f.FlagStackTraceLv f.FlagDateFormat f.FlagFields f.FlagPaths f.FlagErrPaths f.FlagSamplingInitial f.FlagSamplingThereafter
func (*Flags) Init ¶
Init returns f.FlagSet.Init(c).
This is intended to be used as cli.BeforeFunc. see https://pkg.go.dev/github.com/urfave/cli/v2#BeforeFunc
func (*Flags) InitGlobal ¶
InitGlobal calls f.Init(c), then replaces the global logger with f.Logger().
This is intended to be used as cli.BeforeFunc. see https://pkg.go.dev/github.com/urfave/cli/v2#BeforeFunc
func (*Flags) InitVar ¶
InitVar returns the function that can be used as cli.BeforeFunc. The function calls f.Init and f.Logger to create a new logger. Finaly new logger is named with f.Name, saved into p.
func (*Flags) Logger ¶
Logger calls f.RootLogger() and returns the logger named with f.Name if f.Name is not empty, otherwise returns the result of f.RootLogger().
func (*Flags) LookupDateFormat ¶
LookupDateFormat returns the value of f.FlagDateFormat. If the flag is set by the environment variable or the command line argument the value is returned and the boolean is true. Otherwise the returned value is undefined and boolean will be false.
func (*Flags) LookupErrPaths ¶
LookupErrPaths returns the value of f.FlagErrPaths. If the flag is set by the environment variable or the command line argument the value is returned and the boolean is true. Otherwise the returned value is undefined and boolean will be false.
func (*Flags) LookupFields ¶
LookupFields returns the value of f.FlagFields. If the flag is set by the environment variable or the command line argument the value is returned and the boolean is true. Otherwise the returned value is undefined and boolean will be false.
func (*Flags) LookupLevel ¶
LookupLevel returns the value of f.FlagLevel. If the flag is set by the environment variable or the command line argument the value is returned and the boolean is true. Otherwise the returned value is undefined and boolean will be false.
func (*Flags) LookupPaths ¶
LookupPaths returns the value of f.FlagPaths. If the flag is set by the environment variable or the command line argument the value is returned and the boolean is true. Otherwise the returned value is undefined and boolean will be false.
func (*Flags) LookupSamplingInitial ¶
LookupSamplingInitial returns the value of f.FlagSamplingInitial. If the flag is set by the environment variable or the command line argument the value is returned and the boolean is true. Otherwise the returned value is undefined and boolean will be false.
func (*Flags) LookupSamplingThereafter ¶
LookupSamplingThereafter returns the value of f.SamplingThereafter. If the flag is set by the environment variable or the command line argument the value is returned and the boolean is true. Otherwise the returned value is undefined and boolean will be false.
func (*Flags) LookupStackTrace ¶
LookupStackTrace returns the value of f.FlagStackTrace. If the flag is set by the environment variable or the command line argument the value is returned and the boolean is true. Otherwise the returned value is undefined and boolean will be false.
func (*Flags) LookupStackTraceLv ¶
LookupStackTraceLv returns the value of f.FlagStackTraceLv. If the flag is set by the environment variable or the command line argument the value is returned and the boolean is true. Otherwise the returned value is undefined and boolean will be false.
func (*Flags) LookupWithCaller ¶
LookupWithCaller returns the value of f.FlagWithCaller. If the flag is set by the environment variable or the command line argument the value is returned and the boolean is true. Otherwise the returned value is undefined and boolean will be false.
func (*Flags) Options ¶
Options returns []zap.Option.
Following values affects the result.
- f.LookupStackTraceLv()
- f.LookupWithCaller()
- f.LookupFields()
func (*Flags) RootLogger ¶
RootLogger returns f.Config().Build(f.Options()...).
func (*Flags) SamplingConfig ¶
func (f *Flags) SamplingConfig() *zap.SamplingConfig
SamplingConfig returns *zap.SamplingConfig. If at least one of f.FlagSamplingInitial or f.FlagSamplingThereafter is set, it returns non-nil value. Otherwise returns nil. The value that is not specified is same as the other value. The value of thereafter will not be less than 1.
func (*Flags) SamplingInitial ¶
SamplingInitial returns the value of f.FlagSamplingInitial.
func (*Flags) SamplingThereafter ¶
SamplingThereafter returns the value of f.SamplingThereafter.
func (*Flags) StackTrace ¶
StackTrace returns the value of f.FlagStackTrace.
func (*Flags) StackTraceLv ¶
StackTraceLv returns the value of f.FlagStackTraceLv.
func (*Flags) WithCaller ¶
WithCaller returns the value of f.FlagWithCaller.