util

package
v0.0.0-...-04d6450 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package util provides logger config and config validation utilities. Basically, it wraps the well-known logging tools zap and lumberjack.

Index

Constants

This section is empty.

Variables

View Source
var (

	// Get retrieves a buffer from the pool, creating one if necessary.
	Get = _pool.Get
)

Functions

func CreateConsoleCore

func CreateConsoleCore(
	consoleConf *ConsoleConfig,
	level zapcore.Level,
	encoder zapcore.Encoder,
) zapcore.Core

CreateConsoleCore creates a new zapcore instance whose underlying file descriptor is stdout or stderr.

func CreateFileCore

func CreateFileCore(
	rootpath string,
	fileConf *FileConfig,
	level zapcore.Level,
	encoder zapcore.Encoder,
) (zapcore.Core, error)

CreateFileCore creates a new zapcore instance whose underlying file descriptor is a lumberjack wrapped regular file.

func DefaultTimeEncoder

func DefaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

DefaultTimeEncoder serializes time.Time to a human-readable formatted string

func GetDefault

func GetDefault() *zap.Logger

GetDefault returns the pre-inited default logger. Any code outside `internal/common/log` is not allowed to call this function. Because this function is intentionally designed for the offical logger APIs. They usually wrapps this default console logger as the underlying logger, so that they can output things before the real ones are initialized.

func NewTextCore

func NewTextCore(
	enc zapcore.Encoder,
	ws zapcore.WriteSyncer,
	enab zapcore.LevelEnabler,
) zapcore.Core

NewTextCore creates a Core that writes logs to a WriteSyncer.

func NewTextEncoder

func NewTextEncoder(
	encoderConfig *zapcore.EncoderConfig,
	spaced bool,
	disableErrorVerbose bool,
) zapcore.Encoder

func NewTextEncoderByConfig

func NewTextEncoderByConfig(cfg *Config) zapcore.Encoder

NewTextEncoderByConfig creates a fast, low-allocation Text encoder with config. The encoder appropriately escapes all field keys and values.

func ShortCallerEncoder

func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)

ShortCallerEncoder serializes a caller in file:line format.

Types

type Config

type Config struct {
	// Log level. Only logs with a higher level will be printed
	Level string `yaml:"level"`
	// Root path for all logs
	RootPath string `yaml:"root-path"`
	// DisableErrorVerbose stops annotating logs with the full verbose error message.
	DisableErrorVerbose bool `yaml:"disable-error-verbose"`
	// Development puts the logger in development mode, which changes the
	// behavior of DPanicLevel and takes stacktraces more liberally.
	Development bool `yaml:"development"`

	// GlobalLogger confs for all common logger
	GlobalLogger *LoggerConfig `yaml:"global-logger"`
	// contains filtered or unexported fields
}

Config contains all log related config

func (*Config) BuildOpts

func (cfg *Config) BuildOpts() []zap.Option

BuildOpts build additional options for zapcore

func (*Config) GetLevel

func (cfg *Config) GetLevel() zapcore.Level

GetLevel gets the overall level for all logging activity

func (*Config) IsVerified

func (cfg *Config) IsVerified() bool

IsVerified checks if this config struct was verified or not

func (*Config) String

func (cfg *Config) String() string

func (*Config) Verify

func (cfg *Config) Verify()

Verify wrapps doVerify with a `sync.Once`

type ConsoleConfig

type ConsoleConfig struct {
	// ConsoleFD is one of `stdout` or `stderr`
	ConsoleFD string `yaml:"console-fd"`
	// Highest level you want to output through this console FD
	LevelMax string `yaml:"level-max"`
	// Lowest level you want to output through this console FD
	LevelMin string `yaml:"level-min"`
	// contains filtered or unexported fields
}

ConsoleConfig configs output to stdout or stderr

type FileConfig

type FileConfig struct {
	// Log Filename, default to stdout
	FileName string `yaml:"file-name"`
	// Max size for a single file, in MB, default is 300MB
	MaxSize int `yaml:"max-size"`
	// Max days for keeping log files, default is never delete
	MaxDays int `yaml:"max-days"`
	// Max number of old log files to retain
	MaxBackups int `yaml:"max-backups"`
	// Highest level you want to output through this file
	LevelMax string `yaml:"level-max"`
	// Lowest level you want to output through this file
	LevelMin string `yaml:"level-min"`
	// contains filtered or unexported fields
}

LogFileConfig configs output to a single log file

type LoggerConfig

type LoggerConfig struct {
	// Configs for logs that output to files
	Files []*FileConfig `yaml:"file-confs"`
	// Configs for logs that output to `stderr` or `stdout`
	Consoles []*ConsoleConfig `yaml:"console-confs"`
}

LoggerConfig configs a single logger

Jump to

Keyboard shortcuts

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