tog

package
v0.0.0-...-81b864c Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2023 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package tog implements a context-based logging API. The ctx argument to logging functions in this package will have a value that determines the destination Logger.

This package is powered by go.uber.org/zap. See that package for more usage details.

This package configures a default "top-level" logger (which is used for contexts that do not have a logger set), which has different behaviours depending on build tags. If the tag `togProduction` is used at build time, the top-level logger is set to log at InfoLevel, and outputs to stderr in JSON format.

Otherwise, if this tag is not specified, a logger more suitable for debugging is set up. This logger still outputs to stderr, but uses a more human-readable format, and defaults to logging at DebugLevel. This logger can also be tweaked with the environment variables LOG_LEVEL (which sets the logging level), and LOG_FILE (which adds a file path to output logs to, in addition to stderr).

You can also manually set the top-level logger, using SetLogger.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DPanic

func DPanic(ctx context.Context, msg string, fields ...zapcore.Field)

DPanic logs a message at DPanicLevel.

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 Debug

func Debug(ctx context.Context, msg string, fields ...zapcore.Field)

Debug logs a message at DebugLevel.

func Error

func Error(ctx context.Context, msg string, fields ...zapcore.Field)

Error logs a message at ErrorLevel.

func Fatal

func Fatal(ctx context.Context, msg string, fields ...zapcore.Field)

Fatal logs a message at FatalLevel.

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

func FromContext

func FromContext(ctx context.Context) *zap.Logger

FromContext gets the logger associated with ctx. If ctx is nil or has no associated logger, then this returns the top-level logger instead.

func ID

func ID() zap.Field

ID returns a log field with a unique-ish value, to aid in distinguishing multiple log entries from one conceptual task.

This value is NOT SECURELY GENERATED, may not be entirely unique, and may be generated completely deterministically. DO NOT rely on it for anything more than a general visual aid.

func Info

func Info(ctx context.Context, msg string, fields ...zapcore.Field)

Info logs a message at InfoLevel.

func Log

func Log(ctx context.Context, lvl zapcore.Level, msg string, fields ...zapcore.Field)

Log logs a message at the specified level. The destination logger is the same as what would be returned by a call to FromContext(ctx).

See also zap.Logger.Log.

func LogHandler

func LogHandler(lvl zapcore.Level, next http.Handler) http.Handler

LogHandler provides middleware for net/http that logs incoming HTTP requests. The log entry is written when WriteHeader is called (even if the underlying WriteHeader call would be erroneous).

The entry is written at the log level given by lvl. It includes the method and full URL of the request - note that modifications to the URL or Request field provided to next will not be visible in the log entry. The entry also includes the "id" field, as returned by ID(), and modifies the request context with that ID to easier facilitate identifying the logs of a single request.

This middleware supports http.Hijacker, if and only if the underlying http.ResponseWriter also supports Hijacker. Attempting to convert the ResponseWriter to a Hijacker remains appropriate to determine whether or not the feature is supported.

func Logger

func Logger() *zap.Logger

Logger returns the top-level logger for the application. See the package documentation for details on the initial value of this function.

func NYI

func NYI()

NYI causes a panic, with the log message "not implemented". It is used to mark a function body that needs to be revisited.

func Panic

func Panic(ctx context.Context, msg string, fields ...zapcore.Field)

Panic logs a message at PanicLevel.

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

func SetLogger

func SetLogger(logger *zap.Logger) *zap.Logger

SetLogger sets logger as the new top-level logger. Note that this will not affect anything already derived from the old top-level logger, so you will likely want to call this function either from some init(), or very early on in main().

This package makes some minor adjustments to logger before setting it as the top-level, so the actual logger returned by Logger will not be exactly the same as the argument to this function (although it will be derived from the passed logger). The return value of this function is the logger that would be returned by Logger.

func Sugar

func Sugar(ctx context.Context) *zap.SugaredLogger

Sugar returns a logger with a more ergonomic API, derived from the logger returned by FromContext(ctx).

func Warn

func Warn(ctx context.Context, msg string, fields ...zapcore.Field)

Warn logs a message at WarnLevel.

func With

func With(ctx context.Context, fields ...zap.Field) context.Context

With creates a child logger with the given fields, then returns a child context associated with that child logger.

func WithLogger

func WithLogger(ctx context.Context, log *zap.Logger) context.Context

WithLogger returns a child context of ctx, which will use the given logger.

func WithOptions

func WithOptions(ctx context.Context, opts ...zap.Option) context.Context

WithOptions clones the logger that is returned by FromContext(ctx), applies the supplied options, then returns a child context associated with that logger.

Types

This section is empty.

Jump to

Keyboard shortcuts

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