zaputils

package
v0.0.0-...-401e8d7 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package zaputils provides utility funcs to work with Uber's zap (https://github.com/uber-go/zap).

A lot of the documentation here is a copy from zap's documentation.

Zap is licensed under the MIT LICENSE, and so is this package.

Zap's license: Copyright (c) 2016-2017 Uber Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(c Config) (*zap.Logger, func(), error)

NewLogger creates a zap Logger that duplicates log entries into 3 or 2 underlying cores.

The func returned by NewLogger calls the underlying Cores's Sync method and closes all opened writers. Applications should take care to call this func before exiting. This func is never nil and it is safe to be called even if there was an error. This func should not be called if the logger is still going to be used.

On windows OS: the logger will have 2 underlying cores. It will log to stderr and to a file named "logs.log".

On non windows OS: the logger will have 1 additional core that logs to the syslog.

All logger errors will be logged to a logger_errors.log file and to stderr.

If no level is provided for the ConsoleLevel, it will default to zap.InfoLevel. If no level is provided for the FileLevel and/or the SysLevel, they will default to zap.WarnLevel.

func NewProdLogger

func NewProdLogger(name, facility string, priority gsyslog.Priority) (*zap.Logger, func(), error)

NewProdLogger is a helper function that calls NewLogger using NewProdConfig.

See NewProdConfig and NewLogger for details.

func NewRotatingProdLogger

func NewRotatingProdLogger(name, facility string, priority gsyslog.Priority) (*zap.Logger, func(), error)

NewRotatingProdLogger is a helper function that calls NewLogger using NewRotatingProdConfig.

See NewRotatingProdConfig and NewLogger for details.

Types

type Config

type Config struct {
	// ConsoleLevel is the Level of the core
	// that logs to the console.
	// If no level is provided, it will
	// default to zap.InfoLevel.
	ConsoleLevel zap.AtomicLevel

	// FileLevel is the Level of the core
	// that logs to the file.
	// If no level is provided, it will
	// default to zap.WarnLevel.
	FileLevel zap.AtomicLevel

	// SysLevel is the Level of the core
	// that logs to the syslog.
	// If no level is provided, it will
	// default to zap.WarnLevel.
	SysLevel zap.AtomicLevel

	// InitialFields is a collection of
	// fields to add to the logger.
	InitialFields map[string]any

	// Sampling sets a sampling policy.
	// A nil SamplingConfig disables sampling.
	Sampling *zap.SamplingConfig

	// LumberjackLogger implements a rotating
	// system to the file logger.
	LumberjackLogger *lumberjack.Logger

	// ConsoleEncoderConfig sets options for
	// the console core encoder.
	// See zapcore.EncoderConfig for details.
	//
	// This will always be console encoded.
	ConsoleEncoderConfig zapcore.EncoderConfig

	// FileEncoderConfig sets options for
	// the file core encoder.
	// See zapcore.EncoderConfig for details.
	//
	// This will always be JSON encoded.
	FileEncoderConfig zapcore.EncoderConfig

	// SysEncoderConfig sets options for
	// the syslog core encoder.
	// See zapcore.EncoderConfig for details.
	//
	// This will always be JSON encoded.
	SysEncoderConfig zapcore.EncoderConfig

	// SysLogFacility represents te syslog
	// facility.
	SysLogFacility string

	// Name is the name of the logger.
	Name string

	// FileOutputPaths is a list of URLs
	// or file paths to write logging
	// output to.
	// It will be used by the file core.
	// See zap.Open for details.
	FileOutputPaths []string

	// FileOutputPaths is a list of URLs
	// or file paths to write logging
	// output to.
	// It will be used by the console core.
	// See zap.Open for details.
	ConsoleOutputPaths []string

	// 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.
	ErrorOutputPaths []string

	// SysLogPriority maps to the syslog
	// priority levels.
	SysLogPriority gsyslog.Priority

	// Development puts the logger in
	// development mode, which changes
	// the behavior of DPanicLevel and
	// takes stacktraces more liberally.
	Development bool

	// DisableCaller stops annotating logs
	// with the calling function's file name
	// and line number. By default, all logs
	// are annotated.
	DisableCaller bool

	// 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

	// ExtraCores will be appended to the core tee.
	ExtraCores []zapcore.Core
}

Config is used to create a zap Logger.

func NewProdConfig

func NewProdConfig(name, facility string, priority gsyslog.Priority) Config

NewProdConfig is a reasonable production logging configuration. Console log is enabled at InfoLevel and above, while the file log and the sys log are enabled at WarnLevel and above.

It enables sampling and stacktraces are included on logs of ErrorLevel and above.

func NewRotatingProdConfig

func NewRotatingProdConfig(name, facility string, priority gsyslog.Priority) Config

NewRotatingProdConfig returns NewProdConfig but with a lumberjackLogger to implement rotation.

NewRotationProdConfig implements a rotating logger for the file logger. The maximum size for each file is 100MB, the max age is 14 days, the max amount of old log files to keep is 5 and the old files are compressed with gzip.

See NewProdConfig and NewLogger for details about the rest of the configuration.

Jump to

Keyboard shortcuts

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