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 ¶
- 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 FromContext(ctx context.Context) *zap.Logger
- func ID() zap.Field
- func Info(ctx context.Context, msg string, fields ...zapcore.Field)
- func Log(ctx context.Context, lvl zapcore.Level, msg string, fields ...zapcore.Field)
- func LogHandler(lvl zapcore.Level, next http.Handler) http.Handler
- func Logger() *zap.Logger
- func NYI()
- func Panic(ctx context.Context, msg string, fields ...zapcore.Field)
- func SetLogger(logger *zap.Logger) *zap.Logger
- func Sugar(ctx context.Context) *zap.SugaredLogger
- func Warn(ctx context.Context, msg string, fields ...zapcore.Field)
- func With(ctx context.Context, fields ...zap.Field) context.Context
- func WithLogger(ctx context.Context, log *zap.Logger) context.Context
- func WithOptions(ctx context.Context, opts ...zap.Option) context.Context
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DPanic ¶
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 Fatal ¶
Fatal logs a message at FatalLevel.
The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.
func FromContext ¶
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 ¶
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 Log ¶
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 ¶
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 ¶
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 ¶
Panic logs a message at PanicLevel.
The logger then panics, even if logging at PanicLevel is disabled.
func SetLogger ¶
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 With ¶
With creates a child logger with the given fields, then returns a child context associated with that child logger.
func WithLogger ¶
WithLogger returns a child context of ctx, which will use the given logger.
Types ¶
This section is empty.