Documentation ¶
Overview ¶
Package clog is a convenience package for passing Zap loggers through contexts.
Index ¶
- func Check(ctx context.Context, lvl zapcore.Level, msg string) *zapcore.CheckedEntry
- func Config(level zap.AtomicLevel, isDev bool) (conf zap.Config)
- func DPanic(ctx context.Context, msg string, fields ...zapcore.Field)
- func Debug(ctx context.Context, msg string, fields ...zapcore.Field)
- func Error(ctx context.Context, msg string, fields ...zapcore.Field)
- func Fatal(ctx context.Context, msg string, fields ...zapcore.Field)
- func GetEnvConfig(procname string) (level zapcore.Level, isDev bool)
- func Info(ctx context.Context, msg string, fields ...zapcore.Field)
- func Logger(ctx context.Context) (logger *zap.Logger)
- func New(level zap.AtomicLevel, isDev bool) (*zap.Logger, error)
- func NewFromEnv(procname string, level zap.AtomicLevel) (*zap.Logger, error)
- func Panic(ctx context.Context, msg string, fields ...zapcore.Field)
- func Sugar(ctx context.Context) *zap.SugaredLogger
- func Warn(ctx context.Context, msg string, fields ...zapcore.Field)
- func With(ctx context.Context, fields ...zapcore.Field) context.Context
- func WithFieldGenerators(ctx context.Context, generators ...FieldGenerator) context.Context
- func WithLogger(ctx context.Context, logger *zap.Logger) context.Context
- func WithOptions(ctx context.Context, opts ...zap.Option) context.Context
- type FieldGenerator
- type StdLogger
- func (l *StdLogger) Fatal(args ...interface{})
- func (l *StdLogger) Fatalf(format string, args ...interface{})
- func (l *StdLogger) Fatalln(args ...interface{})
- func (l *StdLogger) Panic(args ...interface{})
- func (l *StdLogger) Panicf(format string, args ...interface{})
- func (l *StdLogger) Panicln(args ...interface{})
- func (l *StdLogger) Print(args ...interface{})
- func (l *StdLogger) Printf(format string, args ...interface{})
- func (l *StdLogger) Println(args ...interface{})
- func (l *StdLogger) With(fields ...zapcore.Field) *StdLogger
- func (l *StdLogger) WithOptions(opts ...zap.Option) *StdLogger
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Config ¶
func Config(level zap.AtomicLevel, isDev bool) (conf zap.Config)
Config returns a zap.Config based on the level given and the json and debug parameters. If json is true, the config uses a JSON encoder. If debug is true, production limits on logging are removed and the development flag is set to true.
func GetEnvConfig ¶
GetEnvConfig returns the environment-configured logging level and whether to use JSON and debug logging for procname. If procname is the empty string, os.Args[0] is used instead.
If PROCNAME_LOG_MODE is set to "dev" (case-insensitive) then log output will be formatted for reading on a console. Otherwise, logging defaults to a production configuration.
If PROCNAME_LOG_LEVEL is set to a valid Zap logging level (info, warn, etc.), then that logging level will be returned. Otherwise, the logging level defaults to zap.InfoLevel for production and zap.DebugLevel for development.
func New ¶
New allocates a new zap.Logger using configuration based on the level given and the json and debug parameters, as interpreted by Config.
Example ¶
package main import ( "context" "github.com/Kochava/clog" "go.uber.org/zap" ) func main() { // Create a logger at info level with a production configuration. level := zap.NewAtomicLevelAt(zap.InfoLevel) l, err := clog.New(level, false) if err != nil { panic(err) } l.Info("Ready") // Attach the logger, l, to a context: ctx := clog.WithLogger(context.Background(), l) // Attach fields to the logger: ctx = clog.With(ctx, zap.Int("field", 1234)) // Log at info level: clog.Info(ctx, "Log message") }
Output:
func NewFromEnv ¶
NewFromEnv allocates a new zap.Logger using configuration from the environment. This looks for PROCNAME_LOG_MODE and PROCNAME_LOG_LEVEL to configure the logger. If LOG_MODE is not "dev", the development configuration of Zap is used. Otherwise, logging is configured for production.
func Sugar ¶
func Sugar(ctx context.Context) *zap.SugaredLogger
Sugar is a convenience function for calling Logger(ctx).Sugar().
func With ¶
With returns a context parented to ctx with a logger that has the given fields appended to it. The logger of ctx is the one returned by Logger.
func WithFieldGenerators ¶ added in v0.2.2
func WithFieldGenerators(ctx context.Context, generators ...FieldGenerator) context.Context
WithFieldGenerators adds the supplied FieldGenerator to those found on the supplied context and surfaces a new context
func WithLogger ¶
WithLogger returns a context parented to ctx with the given logger attached as a value.
Types ¶
type FieldGenerator ¶ added in v0.2.2
FieldGenerator describes a function that generates zap fields for a given context
func FieldGenerators ¶ added in v0.2.2
func FieldGenerators(ctx context.Context) (fieldGenerators []FieldGenerator)
FieldGenerators retrieves the FieldGenerators stored on a context
type StdLogger ¶
type StdLogger struct {
// contains filtered or unexported fields
}
StdLogger is a logger that implements most methods used by "log" stdlib users. Print-ed logs are recorded at Info level.
func NewStdLogger ¶
NewStdLogger creats a new StdLogger attached to the given zap.Logger. If logger is nil, NewStdLogger returns nil as well.
func (*StdLogger) Fatal ¶
func (l *StdLogger) Fatal(args ...interface{})
Fatal writes a fatal-level fmt.Sprint-formatted log message.
func (*StdLogger) Fatalln ¶
func (l *StdLogger) Fatalln(args ...interface{})
Fatalln writes a fatal-level fmt.Sprintln-formatted log message.
func (*StdLogger) Panic ¶
func (l *StdLogger) Panic(args ...interface{})
Panic writes a panic-level fmt.Sprint-formatted log message.
func (*StdLogger) Panicln ¶
func (l *StdLogger) Panicln(args ...interface{})
Panicln writes a panic-level fmt.Sprintln-formatted log message.
func (*StdLogger) Print ¶
func (l *StdLogger) Print(args ...interface{})
Print writes an info-level fmt.Sprint-formatted log message.
func (*StdLogger) Println ¶
func (l *StdLogger) Println(args ...interface{})
Println writes an info-level fmt.Sprintln-formatted log message.