log

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 9 Imported by: 6

Documentation

Overview

Package log provides simple functions to build an application logger based on the logr.Logger interface and the zap logging library.

Use New to build the logger and Sync to flush buffered logs, e.g.;

logger := logr.New(os.Stderr)
defer log.Sync(logger)
logger.Info("Hello!", "count", 10)

New accepts multiple functional options, e.g. use WithLevel to specify the verbosity of the logger:

logger := logr.New(os.Stderr, log.WithLevel(10))

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(w io.Writer, opts ...option) logr.Logger

New returns a new logger based on the logr interface and the zap logging library.

Example
package main

import (
	"os"

	"go.artefactual.dev/tools/log"
)

func main() {
	logger := log.New(os.Stderr)
	defer log.Sync(logger)

	logger.Info("Test", "count", 1)
}
Output:

func Sync

func Sync(logger logr.Logger)

Sync flushes buffered logs.

func Underlying

func Underlying(logger logr.Logger) (*zap.Logger, bool)

Underlying returns the zap logger used as a logr sink.

func WithClock added in v0.3.0

func WithClock(clock zapcore.Clock) option

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

Example
package main

import (
	"os"
	"time"

	"go.artefactual.dev/tools/log"
)

func main() {
	logger := log.New(os.Stdout,
		log.WithName("example"),
		log.WithLevel(2),
		log.WithClock(&fakeClock{}),
	)
	defer log.Sync(logger)

	logger.Info("Test.", "key", "val")
	logger.V(4).Info("This should be ignored as per the level configured.")

}

type fakeClock struct{}

func (c *fakeClock) Now() time.Time {
	return time.Unix(626572800, 0)
}

// NewTicker returns a time.Ticker that ticks at the specified frequency.
func (c *fakeClock) NewTicker(d time.Duration) *time.Ticker {
	return nil
}
Output:

{"level":"0","ts":626572800,"logger":"example","caller":"log/log_example_test.go:25","msg":"Test.","key":"val"}

func WithDebug

func WithDebug(enabled bool) option

WithDebug configures the logger for development environments.

func WithLevel

func WithLevel(level int) option

WithLevel defines the verbosity of the logger (0 is the least verbose).

func WithName

func WithName(name string) option

WithName defines the name of the logger.

func WithStacktrace added in v0.4.0

func WithStacktrace(enabled bool) option

WithStacktrace configures the logger to record error stacktraces. It is enabled by default.

func WithVerbosity

func WithVerbosity() option

WithVerbosity defines the V-level to 1.

Types

This section is empty.

Jump to

Keyboard shortcuts

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