qlog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2021 License: GPL-3.0, MIT Imports: 21 Imported by: 0

README

qlog

logs

Documentation

Overview

package qlog provides fast, structured, leveled logging.

For applications that log in the hot path, reflection-based serialization and string formatting are prohibitively expensive - they're CPU-intensive and make many small allocations. Put differently, using json.Marshal and fmt.Fprintf to log tons of interface{} makes your application slow.

Qlog takes a different approach. It includes a reflection-free, zero-allocation JSON encoder, and the base Logger strives to avoid serialization overhead and allocations wherever possible. By building the high-level SugaredLogger on that foundation, qlog lets users choose when they need to count every allocation and when they'd prefer a more familiar, loosely typed API.

Choosing a Logger

In contexts where performance is nice, but not critical, use the SugaredLogger. It's 4-10x faster than other structured logging packages and supports both structured and printf-style logging. Like log15 and go-kit, the SugaredLogger's structured logging APIs are loosely typed and accept a variadic number of key-value pairs. (For more advanced use cases, they also accept strongly typed fields - see the SugaredLogger.With documentation for details.)

sugar := qlog.NewExample().Sugar()
defer sugar.Sync()
sugar.Infow("failed to fetch URL",
  "url", "http://example.com",
  "attempt", 3,
  "backoff", time.Second,
)
sugar.Infof("failed to fetch URL: %s", "http://example.com")

By default, loggers are unbuffered. However, since qlog's low-level APIs allow buffering, calling Sync before letting your process exit is a good habit.

In the rare contexts where every microsecond and every allocation matter, use the Logger. It's even faster than the SugaredLogger and allocates far less, but it only supports strongly-typed, structured logging.

logger := qlog.NewExample()
defer logger.Sync()
logger.Info("failed to fetch URL",
  qlog.String("url", "http://example.com"),
  qlog.Int("attempt", 3),
  qlog.Duration("backoff", time.Second),
)

Choosing between the Logger and SugaredLogger doesn't need to be an application-wide decision: converting between the two is simple and inexpensive.

logger := qlog.NewExample()
defer logger.Sync()
sugar := logger.Sugar()
plain := sugar.Desugar()

Configuring Qlog

The simplest way to build a Logger is to use qlog's opinionated presets: NewExample, NewProduction, and NewDevelopment. These presets build a logger with a single function call:

logger, err := qlog.NewProduction()
if err != nil {
  log.Fatalf("can't initialize qlog logger: %v", err)
}
defer logger.Sync()

Presets are fine for small projects, but larger projects and organizations naturally require a bit more customization. For most users, qlog's Config struct strikes the right balance between flexibility and convenience. See the package-level BasicConfiguration example for sample code.

More unusual configurations (splitting output between files, sending logs to a message queue, etc.) are possible, but require direct use of gitee.com/qrpc/qlog/qlogcore. See the package-level AdvancedConfiguration example for sample code.

Extending Qlog

The qlog package itself is a relatively thin wrapper around the interfaces in gitee.com/qrpc/qlog/qlogcore. Extending qlog to support a new encoding (e.g., BSON), a new log sink (e.g., Kafka), or something more exotic (perhaps an exception aggregation service, like Sentry or Rollbar) typically requires implementing the qlogcore.Encoder, qlogcore.WriteSyncer, or qlogcore.Core interfaces. See the qlogcore documentation for details.

Similarly, package authors can use the high-performance Encoder and Core implementations in the qlogcore package to build their own loggers.

Frequently Asked Questions

An FAQ covering everything from installation errors to design decisions is available at https://github.com/uber-go/qlog/blob/master/FAQ.md.

Example (AdvancedConfiguration)
package main

import (
	"io/ioutil"
	"os"

	"gitee.com/qrpc/qlog"
	"gitee.com/qrpc/qlog/qlogcore"
)

func main() {
	// The bundled Config struct only supports the most common configuration
	// options. More complex needs, like splitting logs between multiple files
	// or writing to non-file outputs, require use of the qlogcore package.
	//
	// In this example, imagine we're both sending our logs to Kafka and writing
	// them to the console. We'd like to encode the console output and the Kafka
	// topics differently, and we'd also like special treatment for
	// high-priority logs.

	// First, define our level-handling logic.
	highPriority := qlog.LevelEnablerFunc(func(lvl qlogcore.Level) bool {
		return lvl >= qlogcore.ErrorLevel
	})
	lowPriority := qlog.LevelEnablerFunc(func(lvl qlogcore.Level) bool {
		return lvl < qlogcore.ErrorLevel
	})

	// Assume that we have clients for two Kafka topics. The clients implement
	// qlogcore.WriteSyncer and are safe for concurrent use. (If they only
	// implement io.Writer, we can use qlogcore.AddSync to add a no-op Sync
	// method. If they're not safe for concurrent use, we can add a protecting
	// mutex with qlogcore.Lock.)
	topicDebugging := qlogcore.AddSync(ioutil.Discard)
	topicErrors := qlogcore.AddSync(ioutil.Discard)

	// High-priority output should also go to standard error, and low-priority
	// output should also go to standard out.
	consoleDebugging := qlogcore.Lock(os.Stdout)
	consoleErrors := qlogcore.Lock(os.Stderr)

	// Optimize the Kafka output for machine consumption and the console output
	// for human operators.
	kafkaEncoder := qlogcore.NewJSONEncoder(qlog.NewProductionEncoderConfig())
	consoleEncoder := qlogcore.NewConsoleEncoder(qlog.NewDevelopmentEncoderConfig())

	// Join the outputs, encoders, and level-handling functions into
	// qlogcore.Cores, then tee the four cores together.
	core := qlogcore.NewTee(
		qlogcore.NewCore(kafkaEncoder, topicErrors, highPriority),
		qlogcore.NewCore(consoleEncoder, consoleErrors, highPriority),
		qlogcore.NewCore(kafkaEncoder, topicDebugging, lowPriority),
		qlogcore.NewCore(consoleEncoder, consoleDebugging, lowPriority),
	)

	// From a qlogcore.Core, it's easy to construct a Logger.
	logger := qlog.New(core)
	defer logger.Sync()
	logger.Info("constructed a logger")
}
Output:

Example (BasicConfiguration)
package main

import (
	"encoding/json"

	"gitee.com/qrpc/qlog"
)

func main() {
	// For some users, the presets offered by the NewProduction, NewDevelopment,
	// and NewExample constructors won't be appropriate. For most of those
	// users, the bundled Config struct offers the right balance of flexibility
	// and convenience. (For more complex needs, see the AdvancedConfiguration
	// example.)
	//
	// See the documentation for Config and qlogcore.EncoderConfig for all the
	// available options.
	rawJSON := []byte(`{
	  "level": "debug",
	  "encoding": "json",
	  "outputPaths": ["stdout", "/tmp/logs"],
	  "errorOutputPaths": ["stderr"],
	  "initialFields": {"foo": "bar"},
	  "encoderConfig": {
	    "messageKey": "message",
	    "levelKey": "level",
	    "levelEncoder": "lowercase"
	  }
	}`)

	var cfg qlog.Config
	if err := json.Unmarshal(rawJSON, &cfg); err != nil {
		panic(err)
	}
	logger, err := cfg.Build()
	if err != nil {
		panic(err)
	}
	defer logger.Sync()

	logger.Info("logger construction succeeded")
}
Output:

{"level":"info","message":"logger construction succeeded","foo":"bar"}
Example (Presets)
package main

import (
	"time"

	"gitee.com/qrpc/qlog"
)

func main() {
	// Using qlog's preset constructors is the simplest way to get a feel for the
	// package, but they don't allow much customization.
	logger := qlog.NewExample() // or NewProduction, or NewDevelopment
	defer logger.Sync()

	const url = "http://example.com"

	// In most circumstances, use the SugaredLogger. It's 4-10x faster than most
	// other structured logging packages and has a familiar, loosely-typed API.
	sugar := logger.Sugar()
	sugar.Infow("Failed to fetch URL.",
		// Structured context as loosely typed key-value pairs.
		"url", url,
		"attempt", 3,
		"backoff", time.Second,
	)
	sugar.Infof("Failed to fetch URL: %s", url)

	// In the unusual situations where every microsecond matters, use the
	// Logger. It's even faster than the SugaredLogger, but only supports
	// structured logging.
	logger.Info("Failed to fetch URL.",
		// Structured context as strongly typed fields.
		qlog.String("url", url),
		qlog.Int("attempt", 3),
		qlog.Duration("backoff", time.Second),
	)
}
Output:

{"level":"info","msg":"Failed to fetch URL.","url":"http://example.com","attempt":3,"backoff":"1s"}
{"level":"info","msg":"Failed to fetch URL: http://example.com"}
{"level":"info","msg":"Failed to fetch URL.","url":"http://example.com","attempt":3,"backoff":"1s"}

Index

Examples

Constants

View Source
const (
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	DebugLevel = qlogcore.DebugLevel
	// InfoLevel is the default logging priority.
	InfoLevel = qlogcore.InfoLevel
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel = qlogcore.WarnLevel
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel = qlogcore.ErrorLevel
	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	DPanicLevel = qlogcore.DPanicLevel
	// PanicLevel logs a message, then panics.
	PanicLevel = qlogcore.PanicLevel
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel = qlogcore.FatalLevel
)

Variables

This section is empty.

Functions

func CombineWriteSyncers

func CombineWriteSyncers(writers ...qlogcore.WriteSyncer) qlogcore.WriteSyncer

CombineWriteSyncers is a utility that combines multiple WriteSyncers into a single, locked WriteSyncer. If no inputs are supplied, it returns a no-op WriteSyncer.

It's provided purely as a convenience; the result is no different from using qlogcore.NewMultiWriteSyncer and qlogcore.Lock individually.

func LevelFlag

func LevelFlag(name string, defaultLevel qlogcore.Level, usage string) *qlogcore.Level

LevelFlag uses the standard library's flag.Var to declare a global flag with the specified name, default, and usage guidance. The returned value is a pointer to the value of the flag.

If you don't want to use the flag package's global state, you can use any non-nil *Level as a flag.Value with your own *flag.FlagSet.

func NewDevelopmentEncoderConfig

func NewDevelopmentEncoderConfig() qlogcore.EncoderConfig

NewDevelopmentEncoderConfig returns an opinionated EncoderConfig for development environments.

func NewProductionEncoderConfig

func NewProductionEncoderConfig() qlogcore.EncoderConfig

NewProductionEncoderConfig returns an opinionated EncoderConfig for production environments.

func NewStdLog

func NewStdLog(l *Logger) *log.Logger

NewStdLog returns a *log.Logger which writes to the supplied qlog Logger at InfoLevel. To redirect the standard library's package-global logging functions, use RedirectStdLog instead.

Example
package main

import (
	"gitee.com/qrpc/qlog"
)

func main() {
	logger := qlog.NewExample()
	defer logger.Sync()

	std := qlog.NewStdLog(logger)
	std.Print("standard logger wrapper")
}
Output:

{"level":"info","msg":"standard logger wrapper"}

func NewStdLogAt

func NewStdLogAt(l *Logger, level qlogcore.Level) (*log.Logger, error)

NewStdLogAt returns *log.Logger which writes to supplied qlog logger at required level.

func Open

func Open(paths ...string) (qlogcore.WriteSyncer, func(), error)

Open is a high-level wrapper that takes a variadic number of URLs, opens or creates each of the specified resources, and combines them into a locked WriteSyncer. It also returns any error encountered and a function to close any opened files.

Passing no URLs returns a no-op WriteSyncer. Qlog handles URLs without a scheme and URLs with the "file" scheme. Third-party code may register factories for other schemes using RegisterSink.

URLs with the "file" scheme must use absolute paths on the local filesystem. No user, password, port, fragments, or query parameters are allowed, and the hostname must be empty or "localhost".

Since it's common to write logs to the local filesystem, URLs without a scheme (e.g., "/var/log/foo.log") are treated as local file paths. Without a scheme, the special paths "stdout" and "stderr" are interpreted as os.Stdout and os.Stderr. When specified without a scheme, relative file paths also work.

func RedirectStdLog

func RedirectStdLog(l *Logger) func()

RedirectStdLog redirects output from the standard library's package-global logger to the supplied logger at InfoLevel. Since qlog already handles caller annotations, timestamps, etc., it automatically disables the standard library's annotations and prefixing.

It returns a function to restore the original prefix and flags and reset the standard library's output to os.Stderr.

Example
package main

import (
	"log"

	"gitee.com/qrpc/qlog"
)

func main() {
	logger := qlog.NewExample()
	defer logger.Sync()

	undo := qlog.RedirectStdLog(logger)
	defer undo()

	log.Print("redirected standard library")
}
Output:

{"level":"info","msg":"redirected standard library"}

func RedirectStdLogAt

func RedirectStdLogAt(l *Logger, level qlogcore.Level) (func(), error)

RedirectStdLogAt redirects output from the standard library's package-global logger to the supplied logger at the specified level. Since qlog already handles caller annotations, timestamps, etc., it automatically disables the standard library's annotations and prefixing.

It returns a function to restore the original prefix and flags and reset the standard library's output to os.Stderr.

func RegisterEncoder

func RegisterEncoder(name string, constructor func(qlogcore.EncoderConfig) (qlogcore.Encoder, error)) error

RegisterEncoder registers an encoder constructor, which the Config struct can then reference. By default, the "json" and "console" encoders are registered.

Attempting to register an encoder whose name is already taken returns an error.

func RegisterSink

func RegisterSink(scheme string, factory func(*url.URL) (Sink, error)) error

RegisterSink registers a user-supplied factory for all sinks with a particular scheme.

All schemes must be ASCII, valid under section 3.1 of RFC 3986 (https://tools.ietf.org/html/rfc3986#section-3.1), and must not already have a factory registered. Qlog automatically registers a factory for the "file" scheme.

func ReplaceGlobals

func ReplaceGlobals(logger *Logger) func()

ReplaceGlobals replaces the global Logger and SugaredLogger, and returns a function to restore the original values. It's safe for concurrent use.

Example
package main

import (
	"gitee.com/qrpc/qlog"
)

func main() {
	logger := qlog.NewExample()
	defer logger.Sync()

	undo := qlog.ReplaceGlobals(logger)
	defer undo()

	qlog.L().Info("replaced qlog's global loggers")
}
Output:

{"level":"info","msg":"replaced qlog's global loggers"}

Types

type AtomicLevel

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

An AtomicLevel is an atomically changeable, dynamic logging level. It lets you safely change the log level of a tree of loggers (the root logger and any children created by adding context) at runtime.

The AtomicLevel itself is an http.Handler that serves a JSON endpoint to alter its level.

AtomicLevels must be created with the NewAtomicLevel constructor to allocate their internal atomic pointer.

Example
package main

import (
	"os"

	"gitee.com/qrpc/qlog"
	"gitee.com/qrpc/qlog/qlogcore"
)

func main() {
	atom := qlog.NewAtomicLevel()

	// To keep the example deterministic, disable timestamps in the output.
	encoderCfg := qlog.NewProductionEncoderConfig()
	encoderCfg.TimeKey = ""

	logger := qlog.New(qlogcore.NewCore(
		qlogcore.NewJSONEncoder(encoderCfg),
		qlogcore.Lock(os.Stdout),
		atom,
	))
	defer logger.Sync()

	logger.Info("info logging enabled")

	atom.SetLevel(qlog.ErrorLevel)
	logger.Info("info logging disabled")
}
Output:

{"level":"info","msg":"info logging enabled"}
Example (Config)
package main

import (
	"encoding/json"

	"gitee.com/qrpc/qlog"
)

func main() {
	// The qlog.Config struct includes an AtomicLevel. To use it, keep a
	// reference to the Config.
	rawJSON := []byte(`{
		"level": "info",
		"outputPaths": ["stdout"],
		"errorOutputPaths": ["stderr"],
		"encoding": "json",
		"encoderConfig": {
			"messageKey": "message",
			"levelKey": "level",
			"levelEncoder": "lowercase"
		}
	}`)
	var cfg qlog.Config
	if err := json.Unmarshal(rawJSON, &cfg); err != nil {
		panic(err)
	}
	logger, err := cfg.Build()
	if err != nil {
		panic(err)
	}
	defer logger.Sync()

	logger.Info("info logging enabled")

	cfg.Level.SetLevel(qlog.ErrorLevel)
	logger.Info("info logging disabled")
}
Output:

{"level":"info","message":"info logging enabled"}

func NewAtomicLevel

func NewAtomicLevel() AtomicLevel

NewAtomicLevel creates an AtomicLevel with InfoLevel and above logging enabled.

func NewAtomicLevelAt

func NewAtomicLevelAt(l qlogcore.Level) AtomicLevel

NewAtomicLevelAt is a convenience function that creates an AtomicLevel and then calls SetLevel with the given level.

func (AtomicLevel) Enabled

func (lvl AtomicLevel) Enabled(l qlogcore.Level) bool

Enabled implements the qlogcore.LevelEnabler interface, which allows the AtomicLevel to be used in place of traditional static levels.

func (AtomicLevel) Level

func (lvl AtomicLevel) Level() qlogcore.Level

Level returns the minimum enabled log level.

func (AtomicLevel) MarshalText

func (lvl AtomicLevel) MarshalText() (text []byte, err error)

MarshalText marshals the AtomicLevel to a byte slice. It uses the same text representation as the static qlogcore.Levels ("debug", "info", "warn", "error", "dpanic", "panic", and "fatal").

func (AtomicLevel) ServeHTTP

func (lvl AtomicLevel) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is a simple JSON endpoint that can report on or change the current logging level.

GET

The GET request returns a JSON description of the current logging level like:

{"level":"info"}

PUT

The PUT request changes the logging level. It is perfectly safe to change the logging level while a program is running. Two content types are supported:

Content-Type: application/x-www-form-urlencoded

With this content type, the level can be provided through the request body or a query parameter. The log level is URL encoded like:

level=debug

The request body takes precedence over the query parameter, if both are specified.

This content type is the default for a curl PUT request. Following are two example curl requests that both set the logging level to debug.

curl -X PUT localhost:8080/log/level?level=debug
curl -X PUT localhost:8080/log/level -d level=debug

For any other content type, the payload is expected to be JSON encoded and look like:

{"level":"info"}

An example curl request could look like this:

curl -X PUT localhost:8080/log/level -H "Content-Type: application/json" -d '{"level":"debug"}'

func (AtomicLevel) SetLevel

func (lvl AtomicLevel) SetLevel(l qlogcore.Level)

SetLevel alters the logging level.

func (AtomicLevel) String

func (lvl AtomicLevel) String() string

String returns the string representation of the underlying Level.

func (*AtomicLevel) UnmarshalText

func (lvl *AtomicLevel) UnmarshalText(text []byte) error

UnmarshalText unmarshals the text to an AtomicLevel. It uses the same text representations as the static qlogcore.Levels ("debug", "info", "warn", "error", "dpanic", "panic", and "fatal").

type Clock

type Clock interface {
	// Now returns the current local time.
	Now() time.Time
}

Clock is a source of time for logged entries.

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 AtomicLevel `json:"level" yaml:"level"`
	// Development puts the logger in development mode, which changes the
	// behavior of DPanicLevel and takes stacktraces more liberally.
	Development bool `json:"development" yaml:"development"`
	// DisableCaller stops annotating logs with the calling function's file
	// name and line number. By default, all logs are annotated.
	DisableCaller bool `json:"disableCaller" yaml:"disableCaller"`
	// 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 `json:"disableStacktrace" yaml:"disableStacktrace"`
	// Sampling sets a sampling policy. A nil SamplingConfig disables sampling.
	Sampling *SamplingConfig `json:"sampling" yaml:"sampling"`
	// Encoding sets the logger's encoding. Valid values are "json" and
	// "console", as well as any third-party encodings registered via
	// RegisterEncoder.
	Encoding string `json:"encoding" yaml:"encoding"`
	// EncoderConfig sets options for the chosen encoder. See
	// qlogcore.EncoderConfig for details.
	EncoderConfig qlogcore.EncoderConfig `json:"encoderConfig" yaml:"encoderConfig"`
	// OutputPaths is a list of URLs or file paths to write logging output to.
	// See Open for details.
	OutputPaths []string `json:"outputPaths" yaml:"outputPaths"`
	// 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 `json:"errorOutputPaths" yaml:"errorOutputPaths"`
	// InitialFields is a collection of fields to add to the root logger.
	InitialFields map[string]interface{} `json:"initialFields" yaml:"initialFields"`
}

Config offers a declarative way to construct a logger. It doesn't do anything that can't be done with New, Options, and the various qlogcore.WriteSyncer and qlogcore.Core wrappers, but it's a simpler way to toggle common options.

Note that Config intentionally supports only the most common options. More unusual logging setups (logging to network connections or message queues, splitting output between multiple files, etc.) are possible, but require direct use of the qlogcore package. For sample code, see the package-level BasicConfiguration and AdvancedConfiguration examples.

For an example showing runtime log level changes, see the documentation for AtomicLevel.

func NewDevelopmentConfig

func NewDevelopmentConfig() Config

NewDevelopmentConfig is a reasonable development logging configuration. Logging is enabled at DebugLevel and above.

It enables development mode (which makes DPanicLevel logs panic), uses a console encoder, writes to standard error, and disables sampling. Stacktraces are automatically included on logs of WarnLevel and above.

func NewProductionConfig

func NewProductionConfig() Config

NewProductionConfig is a reasonable production logging configuration. Logging is enabled at InfoLevel and above.

It uses a JSON encoder, writes to standard error, and enables sampling. Stacktraces are automatically included on logs of ErrorLevel and above.

func (Config) Build

func (cfg Config) Build(opts ...Option) (*Logger, error)

Build constructs a logger from the Config and Options.

type Field

type Field = qlogcore.Field

Field is an alias for Field. Aliasing this type dramatically improves the navigability of this package's API documentation.

func Any

func Any(key string, value interface{}) Field

Any takes a key and an arbitrary value and chooses the best way to represent them as a field, falling back to a reflection-based approach only if necessary.

Since byte/uint8 and rune/int32 are aliases, Any can't differentiate between them. To minimize surprises, []byte values are treated as binary blobs, byte values are treated as uint8, and runes are always treated as integers.

func Array

func Array(key string, val qlogcore.ArrayMarshaler) Field

Array constructs a field with the given key and ArrayMarshaler. It provides a flexible, but still type-safe and efficient, way to add array-like types to the logging context. The struct's MarshalLogArray method is called lazily.

func Binary

func Binary(key string, val []byte) Field

Binary constructs a field that carries an opaque binary blob.

Binary data is serialized in an encoding-appropriate format. For example, qlog's JSON encoder base64-encodes binary blobs. To log UTF-8 encoded text, use ByteString.

func Bool

func Bool(key string, val bool) Field

Bool constructs a field that carries a bool.

func Boolp

func Boolp(key string, val *bool) Field

Boolp constructs a field that carries a *bool. The returned Field will safely and explicitly represent `nil` when appropriate.

func Bools

func Bools(key string, bs []bool) Field

Bools constructs a field that carries a slice of bools.

func ByteString

func ByteString(key string, val []byte) Field

ByteString constructs a field that carries UTF-8 encoded text as a []byte. To log opaque binary blobs (which aren't necessarily valid UTF-8), use Binary.

func ByteStrings

func ByteStrings(key string, bss [][]byte) Field

ByteStrings constructs a field that carries a slice of []byte, each of which must be UTF-8 encoded text.

func Complex128

func Complex128(key string, val complex128) Field

Complex128 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex128 to interface{}).

func Complex128p

func Complex128p(key string, val *complex128) Field

Complex128p constructs a field that carries a *complex128. The returned Field will safely and explicitly represent `nil` when appropriate.

func Complex128s

func Complex128s(key string, nums []complex128) Field

Complex128s constructs a field that carries a slice of complex numbers.

func Complex64

func Complex64(key string, val complex64) Field

Complex64 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex64 to interface{}).

func Complex64p

func Complex64p(key string, val *complex64) Field

Complex64p constructs a field that carries a *complex64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Complex64s

func Complex64s(key string, nums []complex64) Field

Complex64s constructs a field that carries a slice of complex numbers.

func Duration

func Duration(key string, val time.Duration) Field

Duration constructs a field with the given key and value. The encoder controls how the duration is serialized.

func Durationp

func Durationp(key string, val *time.Duration) Field

Durationp constructs a field that carries a *time.Duration. The returned Field will safely and explicitly represent `nil` when appropriate.

func Durations

func Durations(key string, ds []time.Duration) Field

Durations constructs a field that carries a slice of time.Durations.

func Error

func Error(err error) Field

Error is shorthand for the common idiom NamedError("error", err).

func Errors

func Errors(key string, errs []error) Field

Errors constructs a field that carries a slice of errors.

func Float32

func Float32(key string, val float32) Field

Float32 constructs a field that carries a float32. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.

func Float32p

func Float32p(key string, val *float32) Field

Float32p constructs a field that carries a *float32. The returned Field will safely and explicitly represent `nil` when appropriate.

func Float32s

func Float32s(key string, nums []float32) Field

Float32s constructs a field that carries a slice of floats.

func Float64

func Float64(key string, val float64) Field

Float64 constructs a field that carries a float64. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.

func Float64p

func Float64p(key string, val *float64) Field

Float64p constructs a field that carries a *float64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Float64s

func Float64s(key string, nums []float64) Field

Float64s constructs a field that carries a slice of floats.

func Inline

func Inline(val qlogcore.ObjectMarshaler) Field

Inline constructs a Field that is similar to Object, but it will add the elements of the provided ObjectMarshaler to the current namespace.

func Int

func Int(key string, val int) Field

Int constructs a field with the given key and value.

func Int16

func Int16(key string, val int16) Field

Int16 constructs a field with the given key and value.

func Int16p

func Int16p(key string, val *int16) Field

Int16p constructs a field that carries a *int16. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int16s

func Int16s(key string, nums []int16) Field

Int16s constructs a field that carries a slice of integers.

func Int32

func Int32(key string, val int32) Field

Int32 constructs a field with the given key and value.

func Int32p

func Int32p(key string, val *int32) Field

Int32p constructs a field that carries a *int32. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int32s

func Int32s(key string, nums []int32) Field

Int32s constructs a field that carries a slice of integers.

func Int64

func Int64(key string, val int64) Field

Int64 constructs a field with the given key and value.

func Int64p

func Int64p(key string, val *int64) Field

Int64p constructs a field that carries a *int64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int64s

func Int64s(key string, nums []int64) Field

Int64s constructs a field that carries a slice of integers.

func Int8

func Int8(key string, val int8) Field

Int8 constructs a field with the given key and value.

func Int8p

func Int8p(key string, val *int8) Field

Int8p constructs a field that carries a *int8. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int8s

func Int8s(key string, nums []int8) Field

Int8s constructs a field that carries a slice of integers.

func Intp

func Intp(key string, val *int) Field

Intp constructs a field that carries a *int. The returned Field will safely and explicitly represent `nil` when appropriate.

func Ints

func Ints(key string, nums []int) Field

Ints constructs a field that carries a slice of integers.

func NamedError

func NamedError(key string, err error) Field

NamedError constructs a field that lazily stores err.Error() under the provided key. Errors which also implement fmt.Formatter (like those produced by github.com/pkg/errors) will also have their verbose representation stored under key+"Verbose". If passed a nil error, the field is a no-op.

For the common case in which the key is simply "error", the Error function is shorter and less repetitive.

func Namespace

func Namespace(key string) Field

Namespace creates a named, isolated scope within the logger's context. All subsequent fields will be added to the new namespace.

This helps prevent key collisions when injecting loggers into sub-components or third-party libraries.

Example
package main

import (
	"gitee.com/qrpc/qlog"
)

func main() {
	logger := qlog.NewExample()
	defer logger.Sync()

	logger.With(
		qlog.Namespace("metrics"),
		qlog.Int("counter", 1),
	).Info("tracked some metrics")
}
Output:

{"level":"info","msg":"tracked some metrics","metrics":{"counter":1}}

func Object

func Object(key string, val qlogcore.ObjectMarshaler) Field

Object constructs a field with the given key and ObjectMarshaler. It provides a flexible, but still type-safe and efficient, way to add map- or struct-like user-defined types to the logging context. The struct's MarshalLogObject method is called lazily.

Example
package main

import (
	"gitee.com/qrpc/qlog"
	"gitee.com/qrpc/qlog/qlogcore"
)

type addr struct {
	IP   string
	Port int
}

type request struct {
	URL    string
	Listen addr
	Remote addr
}

func (a addr) MarshalLogObject(enc qlogcore.ObjectEncoder) error {
	enc.AddString("ip", a.IP)
	enc.AddInt("port", a.Port)
	return nil
}

func (r request) MarshalLogObject(enc qlogcore.ObjectEncoder) error {
	enc.AddString("url", r.URL)
	qlog.Inline(r.Listen).AddTo(enc)
	return enc.AddObject("remote", r.Remote)
}

func main() {
	logger := qlog.NewExample()
	defer logger.Sync()

	req := &request{
		URL:    "/test",
		Listen: addr{"127.0.0.1", 8080},
		Remote: addr{"127.0.0.1", 31200},
	}
	logger.Info("new request, in nested object", qlog.Object("req", req))
	logger.Info("new request, inline", qlog.Inline(req))
}
Output:

{"level":"info","msg":"new request, in nested object","req":{"url":"/test","ip":"127.0.0.1","port":8080,"remote":{"ip":"127.0.0.1","port":31200}}}
{"level":"info","msg":"new request, inline","url":"/test","ip":"127.0.0.1","port":8080,"remote":{"ip":"127.0.0.1","port":31200}}

func Reflect

func Reflect(key string, val interface{}) Field

Reflect constructs a field with the given key and an arbitrary object. It uses an encoding-appropriate, reflection-based function to lazily serialize nearly any object into the logging context, but it's relatively slow and allocation-heavy. Outside tests, Any is always a better choice.

If encoding fails (e.g., trying to serialize a map[int]string to JSON), Reflect includes the error message in the final log output.

func Skip

func Skip() Field

Skip constructs a no-op field, which is often useful when handling invalid inputs in other Field constructors.

func Stack

func Stack(key string) Field

Stack constructs a field that stores a stacktrace of the current goroutine under provided key. Keep in mind that taking a stacktrace is eager and expensive (relatively speaking); this function both makes an allocation and takes about two microseconds.

func StackSkip

func StackSkip(key string, skip int) Field

StackSkip constructs a field similarly to Stack, but also skips the given number of frames from the top of the stacktrace.

func String

func String(key string, val string) Field

String constructs a field with the given key and value.

func Stringer

func Stringer(key string, val fmt.Stringer) Field

Stringer constructs a field with the given key and the output of the value's String method. The Stringer's String method is called lazily.

func Stringp

func Stringp(key string, val *string) Field

Stringp constructs a field that carries a *string. The returned Field will safely and explicitly represent `nil` when appropriate.

func Strings

func Strings(key string, ss []string) Field

Strings constructs a field that carries a slice of strings.

func Time

func Time(key string, val time.Time) Field

Time constructs a Field with the given key and value. The encoder controls how the time is serialized.

func Timep

func Timep(key string, val *time.Time) Field

Timep constructs a field that carries a *time.Time. The returned Field will safely and explicitly represent `nil` when appropriate.

func Times

func Times(key string, ts []time.Time) Field

Times constructs a field that carries a slice of time.Times.

func Uint

func Uint(key string, val uint) Field

Uint constructs a field with the given key and value.

func Uint16

func Uint16(key string, val uint16) Field

Uint16 constructs a field with the given key and value.

func Uint16p

func Uint16p(key string, val *uint16) Field

Uint16p constructs a field that carries a *uint16. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint16s

func Uint16s(key string, nums []uint16) Field

Uint16s constructs a field that carries a slice of unsigned integers.

func Uint32

func Uint32(key string, val uint32) Field

Uint32 constructs a field with the given key and value.

func Uint32p

func Uint32p(key string, val *uint32) Field

Uint32p constructs a field that carries a *uint32. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint32s

func Uint32s(key string, nums []uint32) Field

Uint32s constructs a field that carries a slice of unsigned integers.

func Uint64

func Uint64(key string, val uint64) Field

Uint64 constructs a field with the given key and value.

func Uint64p

func Uint64p(key string, val *uint64) Field

Uint64p constructs a field that carries a *uint64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint64s

func Uint64s(key string, nums []uint64) Field

Uint64s constructs a field that carries a slice of unsigned integers.

func Uint8

func Uint8(key string, val uint8) Field

Uint8 constructs a field with the given key and value.

func Uint8p

func Uint8p(key string, val *uint8) Field

Uint8p constructs a field that carries a *uint8. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint8s

func Uint8s(key string, nums []uint8) Field

Uint8s constructs a field that carries a slice of unsigned integers.

func Uintp

func Uintp(key string, val *uint) Field

Uintp constructs a field that carries a *uint. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uintptr

func Uintptr(key string, val uintptr) Field

Uintptr constructs a field with the given key and value.

func Uintptrp

func Uintptrp(key string, val *uintptr) Field

Uintptrp constructs a field that carries a *uintptr. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uintptrs

func Uintptrs(key string, us []uintptr) Field

Uintptrs constructs a field that carries a slice of pointer addresses.

func Uints

func Uints(key string, nums []uint) Field

Uints constructs a field that carries a slice of unsigned integers.

type LevelEnablerFunc

type LevelEnablerFunc func(qlogcore.Level) bool

LevelEnablerFunc is a convenient way to implement qlogcore.LevelEnabler with an anonymous function.

It's particularly useful when splitting log output between different outputs (e.g., standard error and standard out). For sample code, see the package-level AdvancedConfiguration example.

func (LevelEnablerFunc) Enabled

func (f LevelEnablerFunc) Enabled(lvl qlogcore.Level) bool

Enabled calls the wrapped function.

type Logger

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

A Logger provides fast, leveled, structured logging. All methods are safe for concurrent use.

The Logger is designed for contexts in which every microsecond and every allocation matters, so its API intentionally favors performance and type safety over brevity. For most applications, the SugaredLogger strikes a better balance between performance and ergonomics.

func L

func L() *Logger

L returns the global Logger, which can be reconfigured with ReplaceGlobals. It's safe for concurrent use.

func New

func New(core qlogcore.Core, options ...Option) *Logger

New constructs a new Logger from the provided qlogcore.Core and Options. If the passed qlogcore.Core is nil, it falls back to using a no-op implementation.

This is the most flexible way to construct a Logger, but also the most verbose. For typical use cases, the highly-opinionated presets (NewProduction, NewDevelopment, and NewExample) or the Config struct are more convenient.

For sample code, see the package-level AdvancedConfiguration example.

func NewDevelopment

func NewDevelopment(options ...Option) (*Logger, error)

NewDevelopment builds a development Logger that writes DebugLevel and above logs to standard error in a human-friendly format.

It's a shortcut for NewDevelopmentConfig().Build(...Option).

func NewExample

func NewExample(options ...Option) *Logger

NewExample builds a Logger that's designed for use in qlog's testable examples. It writes DebugLevel and above logs to standard out as JSON, but omits the timestamp and calling function to keep example output short and deterministic.

func NewNop

func NewNop() *Logger

NewNop returns a no-op Logger. It never writes out logs or internal errors, and it never runs user-defined hooks.

Using WithOptions to replace the Core or error output of a no-op Logger can re-enable logging.

func NewProduction

func NewProduction(options ...Option) (*Logger, error)

NewProduction builds a sensible production Logger that writes InfoLevel and above logs to standard error as JSON.

It's a shortcut for NewProductionConfig().Build(...Option).

func (*Logger) Check

func (log *Logger) Check(lvl qlogcore.Level, msg string) *qlogcore.CheckedEntry

Check returns a CheckedEntry if logging a message at the specified level is enabled. It's a completely optional optimization; in high-performance applications, Check can help avoid allocating a slice to hold fields.

Example
package main

import (
	"gitee.com/qrpc/qlog"
)

func main() {
	logger := qlog.NewExample()
	defer logger.Sync()

	if ce := logger.Check(qlog.DebugLevel, "debugging"); ce != nil {
		// If debug-level log output isn't enabled or if qlog's sampling would have
		// dropped this log entry, we don't allocate the slice that holds these
		// fields.
		ce.Write(
			qlog.String("foo", "bar"),
			qlog.String("baz", "quux"),
		)
	}

}
Output:

{"level":"debug","msg":"debugging","foo":"bar","baz":"quux"}

func (*Logger) Core

func (log *Logger) Core() qlogcore.Core

Core returns the Logger's underlying qlogcore.Core.

func (*Logger) DPanic

func (log *Logger) DPanic(msg string, fields ...Field)

DPanic logs a message at DPanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

If the logger is in development mode, it then panics (DPanic means "development panic"). This is useful for catching errors that are recoverable, but shouldn't ever happen.

func (*Logger) Debug

func (log *Logger) Debug(msg string, fields ...Field)

Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) Error

func (log *Logger) Error(msg string, fields ...Field)

Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) Fatal

func (log *Logger) Fatal(msg string, fields ...Field)

Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.

func (*Logger) Info

func (log *Logger) Info(msg string, fields ...Field)

Info logs a message at InfoLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) Named

func (log *Logger) Named(s string) *Logger

Named adds a new path segment to the logger's name. Segments are joined by periods. By default, Loggers are unnamed.

Example
package main

import (
	"gitee.com/qrpc/qlog"
)

func main() {
	logger := qlog.NewExample()
	defer logger.Sync()

	// By default, Loggers are unnamed.
	logger.Info("no name")

	// The first call to Named sets the Logger name.
	main := logger.Named("main")
	main.Info("main logger")

	// Additional calls to Named create a period-separated path.
	main.Named("subpackage").Info("sub-logger")
}
Output:

{"level":"info","msg":"no name"}
{"level":"info","logger":"main","msg":"main logger"}
{"level":"info","logger":"main.subpackage","msg":"sub-logger"}

func (*Logger) Panic

func (log *Logger) Panic(msg string, fields ...Field)

Panic logs a message at PanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

The logger then panics, even if logging at PanicLevel is disabled.

func (*Logger) Sugar

func (log *Logger) Sugar() *SugaredLogger

Sugar wraps the Logger to provide a more ergonomic, but slightly slower, API. Sugaring a Logger is quite inexpensive, so it's reasonable for a single application to use both Loggers and SugaredLoggers, converting between them on the boundaries of performance-sensitive code.

func (*Logger) Sync

func (log *Logger) Sync() error

Sync calls the underlying Core's Sync method, flushing any buffered log entries. Applications should take care to call Sync before exiting.

func (*Logger) Warn

func (log *Logger) Warn(msg string, fields ...Field)

Warn logs a message at WarnLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) With

func (log *Logger) With(fields ...Field) *Logger

With creates a child logger and adds structured context to it. Fields added to the child don't affect the parent, and vice versa.

func (*Logger) WithOptions

func (log *Logger) WithOptions(opts ...Option) *Logger

WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently.

type Option

type Option interface {
	// contains filtered or unexported methods
}

An Option configures a Logger.

func AddCaller

func AddCaller() Option

AddCaller configures the Logger to annotate each message with the filename, line number, and function name of qlog's caller. See also WithCaller.

func AddCallerSkip

func AddCallerSkip(skip int) Option

AddCallerSkip increases the number of callers skipped by caller annotation (as enabled by the AddCaller option). When building wrappers around the Logger and SugaredLogger, supplying this Option prevents qlog from always reporting the wrapper code as the caller.

func AddStacktrace

func AddStacktrace(lvl qlogcore.LevelEnabler) Option

AddStacktrace configures the Logger to record a stack trace for all messages at or above a given level.

func Development

func Development() Option

Development puts the logger in development mode, which makes DPanic-level logs panic instead of simply logging an error.

func ErrorOutput

func ErrorOutput(w qlogcore.WriteSyncer) Option

ErrorOutput sets the destination for errors generated by the Logger. Note that this option 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.

The supplied WriteSyncer must be safe for concurrent use. The Open and qlogcore.Lock functions are the simplest ways to protect files with a mutex.

func Fields

func Fields(fs ...Field) Option

Fields adds fields to the Logger.

func Hooks

func Hooks(hooks ...func(qlogcore.Entry) error) Option

Hooks registers functions which will be called each time the Logger writes out an Entry. Repeated use of Hooks is additive.

Hooks are useful for simple side effects, like capturing metrics for the number of emitted logs. More complex side effects, including anything that requires access to the Entry's structured fields, should be implemented as a qlogcore.Core instead. See qlogcore.RegisterHooks for details.

func IncreaseLevel

func IncreaseLevel(lvl qlogcore.LevelEnabler) Option

IncreaseLevel increase the level of the logger. It has no effect if the passed in level tries to decrease the level of the logger.

func OnFatal

func OnFatal(action qlogcore.CheckWriteAction) Option

OnFatal sets the action to take on fatal logs.

func WithCaller

func WithCaller(enabled bool) Option

WithCaller configures the Logger to annotate each message with the filename, line number, and function name of qlog's caller, or not, depending on the value of enabled. This is a generalized form of AddCaller.

func WithClock

func WithClock(clock Clock) Option

WithClock specifies the clock used by the logger to determine the current time for logged entries. Defaults to the system clock with time.Now.

func WrapCore

func WrapCore(f func(qlogcore.Core) qlogcore.Core) Option

WrapCore wraps or replaces the Logger's underlying qlogcore.Core.

Example (Replace)
package main

import (
	"gitee.com/qrpc/qlog"
	"gitee.com/qrpc/qlog/qlogcore"
)

func main() {
	// Replacing a Logger's core can alter fundamental behaviors.
	// For example, it can convert a Logger to a no-op.
	nop := qlog.WrapCore(func(qlogcore.Core) qlogcore.Core {
		return qlogcore.NewNopCore()
	})

	logger := qlog.NewExample()
	defer logger.Sync()

	logger.Info("working")
	logger.WithOptions(nop).Info("no-op")
	logger.Info("original logger still works")
}
Output:

{"level":"info","msg":"working"}
{"level":"info","msg":"original logger still works"}
Example (Wrap)
package main

import (
	"gitee.com/qrpc/qlog"
	"gitee.com/qrpc/qlog/qlogcore"
)

func main() {
	// Wrapping a Logger's core can extend its functionality. As a trivial
	// example, it can double-write all logs.
	doubled := qlog.WrapCore(func(c qlogcore.Core) qlogcore.Core {
		return qlogcore.NewTee(c, c)
	})

	logger := qlog.NewExample()
	defer logger.Sync()

	logger.Info("single")
	logger.WithOptions(doubled).Info("doubled")
}
Output:

{"level":"info","msg":"single"}
{"level":"info","msg":"doubled"}
{"level":"info","msg":"doubled"}

type SamplingConfig

type SamplingConfig struct {
	Initial    int                                             `json:"initial" yaml:"initial"`
	Thereafter int                                             `json:"thereafter" yaml:"thereafter"`
	Hook       func(qlogcore.Entry, qlogcore.SamplingDecision) `json:"-" yaml:"-"`
}

SamplingConfig sets a sampling strategy for the logger. Sampling caps the global CPU and I/O load that logging puts on your process while attempting to preserve a representative subset of your logs.

If specified, the Sampler will invoke the Hook after each decision.

Values configured here are per-second. See qlogcore.NewSamplerWithOptions for details.

type Sink

type Sink interface {
	qlogcore.WriteSyncer
	io.Closer
}

Sink defines the interface to write to and close logger destinations.

type SugaredLogger

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

A SugaredLogger wraps the base Logger functionality in a slower, but less verbose, API. Any Logger can be converted to a SugaredLogger with its Sugar method.

Unlike the Logger, the SugaredLogger doesn't insist on structured logging. For each log level, it exposes three methods: one for loosely-typed structured logging, one for println-style formatting, and one for printf-style formatting. For example, SugaredLoggers can produce InfoLevel output with Infow ("info with" structured context), Info, or Infof.

func S

func S() *SugaredLogger

S returns the global SugaredLogger, which can be reconfigured with ReplaceGlobals. It's safe for concurrent use.

func (*SugaredLogger) DPanic

func (s *SugaredLogger) DPanic(args ...interface{})

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

func (*SugaredLogger) DPanicf

func (s *SugaredLogger) 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 (*SugaredLogger) DPanicw

func (s *SugaredLogger) 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 (*SugaredLogger) Debug

func (s *SugaredLogger) Debug(args ...interface{})

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

func (*SugaredLogger) Debugf

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

Debugf uses fmt.Sprintf to log a templated message.

func (*SugaredLogger) Debugw

func (s *SugaredLogger) 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 (*SugaredLogger) Desugar

func (s *SugaredLogger) Desugar() *Logger

Desugar unwraps a SugaredLogger, exposing the original Logger. Desugaring is quite inexpensive, so it's reasonable for a single application to use both Loggers and SugaredLoggers, converting between them on the boundaries of performance-sensitive code.

func (*SugaredLogger) Error

func (s *SugaredLogger) Error(args ...interface{})

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

func (*SugaredLogger) Errorf

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

Errorf uses fmt.Sprintf to log a templated message.

func (*SugaredLogger) Errorw

func (s *SugaredLogger) 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 (*SugaredLogger) Fatal

func (s *SugaredLogger) Fatal(args ...interface{})

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

func (*SugaredLogger) Fatalf

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

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

func (*SugaredLogger) Fatalw

func (s *SugaredLogger) 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 (*SugaredLogger) Info

func (s *SugaredLogger) Info(args ...interface{})

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

func (*SugaredLogger) Infof

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

Infof uses fmt.Sprintf to log a templated message.

func (*SugaredLogger) Infow

func (s *SugaredLogger) 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 (*SugaredLogger) Named

func (s *SugaredLogger) Named(name string) *SugaredLogger

Named adds a sub-scope to the logger's name. See Logger.Named for details.

func (*SugaredLogger) Panic

func (s *SugaredLogger) Panic(args ...interface{})

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

func (*SugaredLogger) Panicf

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

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

func (*SugaredLogger) Panicw

func (s *SugaredLogger) 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 (*SugaredLogger) Sync

func (s *SugaredLogger) Sync() error

Sync flushes any buffered log entries.

func (*SugaredLogger) Warn

func (s *SugaredLogger) Warn(args ...interface{})

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

func (*SugaredLogger) Warnf

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

Warnf uses fmt.Sprintf to log a templated message.

func (*SugaredLogger) Warnw

func (s *SugaredLogger) 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 (*SugaredLogger) With

func (s *SugaredLogger) With(args ...interface{}) *SugaredLogger

With adds a variadic number of fields to the logging context. It accepts a mix of strongly-typed Field objects and loosely-typed key-value pairs. When processing pairs, the first element of the pair is used as the field key and the second as the field value.

For example,

 sugaredLogger.With(
   "hello", "world",
   "failure", errors.New("oh no"),
   Stack(),
   "count", 42,
   "user", User{Name: "alice"},
)

is the equivalent of

unsugared.With(
  String("hello", "world"),
  String("failure", "oh no"),
  Stack(),
  Int("count", 42),
  Object("user", User{Name: "alice"}),
)

Note that the keys in key-value pairs should be strings. In development, passing a non-string key panics. In production, the logger is more forgiving: a separate error is logged, but the key-value pair is skipped and execution continues. Passing an orphaned key triggers similar behavior: panics in development and errors in production.

Directories

Path Synopsis
Package buffer provides a thin wrapper around a byte slice.
Package buffer provides a thin wrapper around a byte slice.
internal
bufferpool
Package bufferpool houses qlog's shared internal buffer pool.
Package bufferpool houses qlog's shared internal buffer pool.
color
Package color adds coloring functionality for TTY output.
Package color adds coloring functionality for TTY output.
exit
Package exit provides stubs so that unit tests can exercise code that calls os.Exit(1).
Package exit provides stubs so that unit tests can exercise code that calls os.Exit(1).
ztest
Package ztest provides low-level helpers for testing log output.
Package ztest provides low-level helpers for testing log output.
package qlogcore defines and implements the low-level interfaces upon which qlog is built.
package qlogcore defines and implements the low-level interfaces upon which qlog is built.
package qloggrpc provides a logger that is compatible with grpclog.
package qloggrpc provides a logger that is compatible with grpclog.
package qlogtest provides a variety of helpers for testing log output.
package qlogtest provides a variety of helpers for testing log output.
observer
Package observer provides a qlogcore.Core that keeps an in-memory, encoding-agnostic repesentation of log entries.
Package observer provides a qlogcore.Core that keeps an in-memory, encoding-agnostic repesentation of log entries.

Jump to

Keyboard shortcuts

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