log

package
v0.0.0-...-4e66fdd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

README

Configuration

  • level (optional): specify the minimum enabled logging level. Supported values (from lowest to highest) are: debug, info, warn, error. Defaults to info.
  • format (optional): specify the format of the log records (e.g. which entries appear in it). Currently supported formats:
    production: default zap production config.
    development: default zap development config.
    bare: a relatively bare format, featuring only log level (colored), logger name, and its message, suffixed with any fields.
  • development (optional): set to true to put the logger into development mode. This changes the behavior of DPanic and takes stacktraces more liberally.
  • disable-stacktrace (optional): set to true to completely disable automatic stacktrace capturing.
  • sampling (optional): set the sampling policy. There are two possible sub-settings: initial and thereafter, each is an integer value. initial specifies how many messages, of a given level, will be logged as-is every second. After than, only every thereafter'th message will be logged within that seconds. e.g.
      sampling:
        initial: 10
        thereafer: 5
    
    means, for very level (debug, info, etc) log the first 10 messages that occur in 1 second. If more messages occur within the second, log every 5th message after the first 10.
  • encoding (optional): specifies logger encoding. Supported values: console, json.
  • output-paths (optional): a list of URLs or file paths to write logging output to. By default, output is written to stdout (which may also be specified as a "path" along other locations). In case the same configuration is used by multiple services, you can insert "{{ .service }}" somewhere in paths (part from stdout/stderr) to have different services log into different files.
  • err-output-paths (optional): a list of URLs or file paths to write internal logger errors to (note: not the error-level logging output). Defaults to stderr. In case the same configuration is used by multiple services, you can insert "{{ .service }}" somewhere in paths (part from stdout/stderr) to have different services log into different files.
  • initial-fields (optional): a map of key-value fields to add to log records (in addition to those added by specific logging sites).

Documentation

Overview

Copyright 2022-2023 Contributors to the Veraison project. SPDX-License-Identifier: Apache-2.0

Copyright 2022-2023 Contributors to the Veraison project. SPDX-License-Identifier: Apache-2.0

Copyright 2022-2023 Contributors to the Veraison project. SPDX-License-Identifier: Apache-2.0

Copyright 2022-2023 Contributors to the Veraison project. SPDX-License-Identifier: Apache-2.0

Index

Constants

View Source
const (
	DebugLevel = zap.DebugLevel
	TraceLevel = zap.DebugLevel
	InfoLevel  = zap.InfoLevel
	WarnLevel  = zap.WarnLevel
	ErrorLevel = zap.ErrorLevel
)

Variables

This section is empty.

Functions

func ColorCapitalLevelEncoder

func ColorCapitalLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder)

An encoder for zap log levels that colorizes them based on their value.

func DPanic

func DPanic(args ...interface{})

DPanic uses fmt.Sprint to construct and log a message. In development, the logger then panics. (See DPanicLevel for details.)

func DPanicf

func DPanicf(template string, args ...interface{})

DPanicf uses fmt.Sprintf to log a templated message. In development, the logger then panics. (See DPanicLevel for details.)

func DPanicln

func DPanicln(args ...interface{})

DPanicln uses fmt.Sprintln to construct and log a message. In development, the logger then panics. (See DPanicLevel for details.)

func DPanicw

func DPanicw(msg string, keysAndValues ...interface{})

DPanicw logs a message with some additional context. In development, the logger then panics. (See DPanicLevel for details.) The variadic key-value pairs are treated as they are in With.

func Debug

func Debug(args ...interface{})

Debug uses fmt.Sprint to construct and log a message.

func Debugf

func Debugf(template string, args ...interface{})

Debugf uses fmt.Sprintf to log a templated message.

func Debugln

func Debugln(args ...interface{})

Debugln uses fmt.Sprintln to construct and log a message.

func Debugw

func Debugw(msg string, keysAndValues ...interface{})

Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

When debug-level logging is disabled, this is much faster than

s.With(keysAndValues).Debug(msg)

func Error

func Error(args ...interface{})

Error uses fmt.Sprint to construct and log a message.

func Errorf

func Errorf(template string, args ...interface{})

Errorf uses fmt.Sprintf to log a templated message.

func Errorln

func Errorln(args ...interface{})

Errorln uses fmt.Sprintln to construct and log a message.

func Errorw

func Errorw(msg string, keysAndValues ...interface{})

Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func Fatal

func Fatal(args ...interface{})

Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.

func Fatalf

func Fatalf(template string, args ...interface{})

Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.

func Fatalln

func Fatalln(args ...interface{})

Fatalln uses fmt.Sprintln to construct and log a message, then calls os.Exit.

func Fatalw

func Fatalw(msg string, keysAndValues ...interface{})

Fatalw logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With.

func GetLevel

func GetLevel() zapcore.Level

GetLevel returns the current global logging level.

func Info

func Info(args ...interface{})

Info uses fmt.Sprint to construct and log a message.

func Infof

func Infof(template string, args ...interface{})

Infof uses fmt.Sprintf to log a templated message.

func Infoln

func Infoln(args ...interface{})

Infoln uses fmt.Sprintln to construct and log a message.

func Infow

func Infow(msg string, keysAndValues ...interface{})

Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func Init

func Init(v *viper.Viper, classifiers map[string]interface{}) error

Init initializes services global logging. All loggers are going to be derived from a root created here. The classifiers are used to identify the executable/application that is being logged, e.g. when naming the output files, they are applied to templated paths.

func InitGinWriter

func InitGinWriter()

InitGinWriter sets up gin DefaultWriter to use Veraison logging.

func LogProblem

func LogProblem(logger *zap.SugaredLogger, prob *problems.DefaultProblem)

LogProblem logs a problems.StatusProblem reported by the api. 500 probelms are logged as errors, everything else is logged as warnings.

func Named

func Named(name string) *zap.SugaredLogger

Create a new logger (derived from the global root) with the specified name.

func NamedWriter

func NamedWriter(name string, level zapcore.Level) io.Writer

NamedWriter creates an io.Writer that utilizes a zap logger with the specified name at the specifed level.

func Panic

func Panic(args ...interface{})

Panic uses fmt.Sprint to construct and log a message, then panics.

func Panicf

func Panicf(template string, args ...interface{})

Panicf uses fmt.Sprintf to log a templated message, then panics.

func Panicln

func Panicln(args ...interface{})

Panicln uses fmt.Sprintln to construct and log a message, then panics.

func Panicw

func Panicw(msg string, keysAndValues ...interface{})

Panicw logs a message with some additional context, then panics. The variadic key-value pairs are treated as they are in With.

func SetLevel

func SetLevel(l zapcore.Level)

SetLevel sets the global logging level.

func Sync

func Sync() error

Sync loggers' io sinks.

func VerboseViper

func VerboseViper()

VerboseViper enables verbose output level for Viper loggers. This is exposed as a separate function as one might want to enable this before initializing logging.

func Warn

func Warn(args ...interface{})

Warn uses fmt.Sprint to construct and log a message.

func Warnf

func Warnf(template string, args ...interface{})

Warnf uses fmt.Sprintf to log a templated message.

func Warnln

func Warnln(args ...interface{})

Warnln uses fmt.Sprintln to construct and log a message.

func Warnw

func Warnw(msg string, keysAndValues ...interface{})

Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func WriterFromZap

func WriterFromZap(logger *zap.SugaredLogger, level zapcore.Level) io.Writer

WriterFromZap returns an io.Writer utilzing the provided zap logger at the specified level.

Types

type Config

type Config struct {
	// Level is the minimum enabled logging level. Note that this is a dynamic
	// level, so calling Config.Level.SetLevel will atomically change the log
	// level of all loggers descended from this config.
	Level string `mapstructure:"level"`
	// Development puts the logger in development mode, which changes the
	// behavior of DPanicLevel and takes stacktraces more liberally.
	Development bool `mapstructure:"development" config:"zerodefault"`
	// DisableCaller stops annotating logs with the calling function's file
	// name and line number. By default, all logs are annotated.
	DisableCaller bool `mapstructure:"disable-caller" config:"zerodefault"`
	// DisableStacktrace completely disables automatic stacktrace capturing. By
	// default, stacktraces are captured for WarnLevel and above logs in
	// development and ErrorLevel and above in production.
	DisableStacktrace bool `mapstructure:"disable-stacktrace" config:"zerodefault"`
	// Sampling sets a sampling policy. A nil SamplingConfig disables sampling.
	Sampling *zap.SamplingConfig `mapstructure:"sampling" config:"zerodefault"`
	// Encoding sets the logger's encoding. Valid values are "json" and
	// "console", as well as any third-party encodings registered via
	// RegisterEncoder.
	Encoding string `mapstructure:"encoding"`
	// OutputPaths is a list of URLs or file paths to write logging output to.
	// See Open for details.
	OutputPaths []string `mapstructure:"output-paths"`
	// ErrorOutputPaths is a list of URLs to write internal logger errors to.
	// The default is standard error.
	//
	// Note that this setting only affects internal errors; for sample code that
	// sends error-level logs to a different location from info- and debug-level
	// logs, see the package-level AdvancedConfiguration example.
	ErrorOutputPaths []string `mapstructure:"err-output-paths"`
	// InitialFields is a collection of fields to add to the root logger.
	InitialFields map[string]interface{} `mapstructure:"initial-fields" config:"zerodefault"`
	// Format is the only field that is not directly transposed into
	// zap.Config. Instead, it is used to look up a pre-defined
	// zapcore.EncoderConfig.
	Format string `mapstructure:"format"`
}

Config is, essentially, and adapter for zap.Config that defines mapstructure tags for zap.Config fields, and provides any additional Veraison-specific validation. Note: zap.Config defines field tags for JSON and YAML. Even though Veraison uses YAML config files, we do not load them directly, but use Viper, which uses mapstructure internally. Thus the need for mapstructure tags. Additionally, zap specifies camel-case for its config fields, whereas Verasion uses all-lower-case-with-hyphen-delimiter fields.

func (Config) Validate

func (o Config) Validate() error

func (Config) Zap

func (o Config) Zap() zap.Config

Zap returns a zap.Config populated with the field values of this config.

type GinColorWriter

type GinColorWriter struct {
	// contains filtered or unexported fields
}

GinColorWriter wraps zapio.Writer to provide warning and error highlighting inside gin traces.

func NewGinColorWriter

func NewGinColorWriter(writer *zapio.Writer) *GinColorWriter

func (*GinColorWriter) Write

func (o *GinColorWriter) Write(p []byte) (int, error)

type HCLogger

type HCLogger struct {
	// contains filtered or unexported fields
}

HCLogger is a wrapper around zap logger used by Veraison that implements go-hclog.Logger interface expected by go-plugin plugins.

func NewInternalLogger

func NewInternalLogger(logger *zap.SugaredLogger) *HCLogger

NewInternalLogger returns a new logger that logs all Info level messages at Debug level. This is to allow treating info-level messages form 3rd-party libararies as debug-level for our services.

func NewLogger

func NewLogger(logger *zap.SugaredLogger) *HCLogger

func (*HCLogger) Debug

func (o *HCLogger) Debug(msg string, args ...interface{})

Emit a message and key/value pairs at the DEBUG level

func (*HCLogger) Error

func (o *HCLogger) Error(msg string, args ...interface{})

Emit a message and key/value pairs at the ERROR level

func (*HCLogger) ImpliedArgs

func (o *HCLogger) ImpliedArgs() []interface{}

ImpliedArgs returns With key/value pairs

func (*HCLogger) Info

func (o *HCLogger) Info(msg string, args ...interface{})

Emit a message and key/value pairs at the INFO level

func (*HCLogger) IsDebug

func (o *HCLogger) IsDebug() bool

Indicate if DEBUG logs would be emitted. This and the other Is* guards

func (*HCLogger) IsError

func (o *HCLogger) IsError() bool

Indicate if ERROR logs would be emitted. This and the other Is* guards

func (*HCLogger) IsInfo

func (o *HCLogger) IsInfo() bool

Indicate if INFO logs would be emitted. This and the other Is* guards

func (*HCLogger) IsTrace

func (o *HCLogger) IsTrace() bool

Indicate if TRACE logs would be emitted. This and the other Is* guards are used to elide expensive logging code based on the current level.

func (*HCLogger) IsWarn

func (o *HCLogger) IsWarn() bool

Indicate if WARN logs would be emitted. This and the other Is* guards

func (*HCLogger) Log

func (o *HCLogger) Log(level hclog.Level, msg string, args ...interface{})

func (*HCLogger) Name

func (o *HCLogger) Name() string

Returns the Name of the logger

func (*HCLogger) Named

func (o *HCLogger) Named(name string) hclog.Logger

Create a logger that will prepend the name string on the front of all messages. If the logger already has a name, the new value will be appended to the current name. That way, a major subsystem can use this to decorate all it's own logs without losing context.

func (*HCLogger) ResetNamed

func (o *HCLogger) ResetNamed(name string) hclog.Logger

Create a logger that will prepend the name string on the front of all messages. This sets the name of the logger to the value directly, unlike Named which honor the current name as well.

func (*HCLogger) SetLevel

func (o *HCLogger) SetLevel(level hclog.Level)

Updates the level. This should affect all related loggers as well, unless they were created with IndependentLevels. If an implementation cannot update the level on the fly, it should no-op.

func (*HCLogger) StandardLogger

func (o *HCLogger) StandardLogger(opts *hclog.StandardLoggerOptions) *stdlog.Logger

Return a value that conforms to the stdlib log.Logger interface

func (*HCLogger) StandardWriter

func (o *HCLogger) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer

Return a value that conforms to io.Writer, which can be passed into log.SetOutput()

func (*HCLogger) Trace

func (o *HCLogger) Trace(msg string, args ...interface{})

Emit a message and key/value pairs at the TRACE level

func (*HCLogger) Warn

func (o *HCLogger) Warn(msg string, args ...interface{})

Emit a message and key/value pairs at the WARN level

func (*HCLogger) With

func (o *HCLogger) With(args ...interface{}) hclog.Logger

Creates a sublogger that will always have the given key/value pairs

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL