logger

package
v0.0.0-...-1569ab3 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	DebugLevel = Level(zapcore.DebugLevel)
	// InfoLevel is the default logging priority.
	InfoLevel = Level(zapcore.InfoLevel)
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel = Level(zapcore.WarnLevel)
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel = Level(zapcore.ErrorLevel)
	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	DPanicLevel = Level(zapcore.DPanicLevel)
	// PanicLevel logs a message, then panics.
	PanicLevel = Level(zapcore.PanicLevel)
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel = Level(zapcore.FatalLevel)
)
View Source
const DefaultLogLevel = ""

DefaultLogLevel is the *number* that control the level of Logger with highest priority.

NOTE: ***KEEP EMPTY VALUE HERE*** This value is expected to modify by: GO run -ldflags "-X 'github.com/quanxiang-cloud/cabin/logger.DefaultLogLevel=0'"

View Source
const EnvLogLevel = "CABIN_LOG_LEVEL"

EnvLogLevel is the environment variable to control logger.LogLevel eg: "set CABIN_LOG_LEVEL=0"

Variables

This section is empty.

Functions

func GetLogLevelFromEnv

func GetLogLevelFromEnv() (int, error)

GetLogLevelFromEnv get cabin log level from os.Getenv

func ZapField

func ZapField(field string, val interface{}) zap.Field

ZapField return a zap field key-value pair

Types

type AdaptedLogger

type AdaptedLogger interface {
	// WithValues returns a new Logger with additional key/value pairs.
	WithValues(keysAndValues ...interface{}) AdaptedLogger

	// WithName returns a new Logger with the specified name appended.
	WithName(name string) AdaptedLogger

	// WithLevel returns a new Logger with the specified level filter.
	WithLevel(level Level) AdaptedLogger

	// WithOptions clones the current Logger, applies the supplied Options, and
	// returns the resulting Logger. It's safe to use concurrently.
	WithOptions(opts ...Option) AdaptedLogger

	// PutError write log with error
	PutError(err error, msg string, keysAndValues ...interface{})

	// Debug uses fmt.Sprint to construct and log a message.
	Debug(args ...interface{})

	// Info uses fmt.Sprint to construct and log a message.
	Info(args ...interface{})

	// Warn uses fmt.Sprint to construct and log a message.
	Warn(args ...interface{})

	// Error uses fmt.Sprint to construct and log a message.
	Error(args ...interface{})

	// DPanic uses fmt.Sprint to construct and log a message. In development, the
	// logger then panics. (See DPanicLevel for details.)
	DPanic(args ...interface{})

	// Panic uses fmt.Sprint to construct and log a message, then panicl.
	Panic(args ...interface{})

	// Fatal uses fmt.Sprint to construct and log a message, then calls ol.Exit.
	Fatal(args ...interface{})

	// Debugf uses fmt.Sprintf to log a templated message.
	Debugf(template string, args ...interface{})

	// Infof uses fmt.Sprintf to log a templated message.
	Infof(template string, args ...interface{})

	// Warnf uses fmt.Sprintf to log a templated message.
	Warnf(template string, args ...interface{})

	// Errorf uses fmt.Sprintf to log a templated message.
	Errorf(template string, args ...interface{})

	// DPanicf uses fmt.Sprintf to log a templated message. In development, the
	// logger then panics. (See DPanicLevel for details.)
	DPanicf(template string, args ...interface{})

	// Panicf uses fmt.Sprintf to log a templated message, then panicl.
	Panicf(template string, args ...interface{})

	// Fatalf uses fmt.Sprintf to log a templated message, then calls ol.Exit.
	Fatalf(template string, args ...interface{})

	// Debugw logs a message with some additional context. The variadic key-value
	// pairs are treated as they are in With.
	//
	// When debug-level logging is disabled, this is much faster than
	//  l.With(keysAndValues).Debug(msg)
	Debugw(msg string, keysAndValues ...interface{})

	// Infow logs a message with some additional context. The variadic key-value
	// pairs are treated as they are in With.
	Infow(msg string, keysAndValues ...interface{})

	// Warnw logs a message with some additional context. The variadic key-value
	// pairs are treated as they are in With.
	Warnw(msg string, keysAndValues ...interface{})

	// Errorw logs a message with some additional context. The variadic key-value
	// pairs are treated as they are in With.
	Errorw(msg string, keysAndValues ...interface{})

	// DPanicw logs a message with some additional context. In development, the
	// logger then panics. (See DPanicLevel for details.) The variadic key-value
	// pairs are treated as they are in With.
	DPanicw(msg string, keysAndValues ...interface{})

	// Panicw logs a message with some additional context, then panicl. The
	// variadic key-value pairs are treated as they are in With.
	Panicw(msg string, keysAndValues ...interface{})

	// Fatalw logs a message with some additional context, then calls ol.Exit. The
	// variadic key-value pairs are treated as they are in With.
	Fatalw(msg string, keysAndValues ...interface{})

	// Sync flushes any buffered log entries.
	Sync() error
}

AdaptedLogger is the interface that adapt zap.logger

var Logger AdaptedLogger = NewDefault()

Logger is the default logger object.

func New

func New(cfg *LogConfig, options ...Option) AdaptedLogger

New create a new AdaptedLogger

The cfg is optional, we use cfg.Level only. NOTE: there are 4 ways to control logger.Level(with priority decend): 1. value of "DefaultLogLevel" 2. value of "os.Getenv(CABIN_LOG_LEVEL)" eg: "set CABIN_LOG_LEVEL=0" 3. value of Config.Level 4. default value DebugLevel(-1)

func NewDefault

func NewDefault() AdaptedLogger

NewDefault create a defult Logger

func NewFromLogr

func NewFromLogr(log logr.Logger) AdaptedLogger

NewFromLogr new Logger from logr.Logger

type Clock

type Clock = zapcore.Clock

Clock is a source of time for logged entries.

type Field

type Field = zap.Field

Field is an alias for Field. Aliasing this type dramatically improves the navigability of this package's API documentation.

type Level

type Level zapcore.Level

A Level is a logging priority. Higher levels are more important.

func (Level) Int

func (l Level) Int() int

Int convert level to int

func (Level) ZapLevel

func (l Level) ZapLevel() zapcore.Level

ZapLevel return zap level

type LevelEnabler

type LevelEnabler = zapcore.LevelEnabler

LevelEnabler decides whether a given logging level is enabled when logging a message.

Enablers are intended to be used to implement deterministic filters; concerns like sampling are better implemented as a Core.

Each concrete Level value implements a static LevelEnabler which returns true for itself and all higher logging levels. For example WarnLevel.Enabled() will return true for WarnLevel, ErrorLevel, DPanicLevel, PanicLevel, and FatalLevel, but return false for InfoLevel and DebugLevel.

type LogConfig

type LogConfig struct {
	Level           int      `yaml:"level"`
	Development     bool     `yaml:"development"`
	Sampling        Sampling `yaml:"sampling"`
	OutputPath      []string `yaml:"outputPath"`
	ErrorOutputPath []string `yaml:"errorOutputPath"`
}

Config config

type Option

type Option = zap.Option

An Option configures a Logger.

func AddCallerSkip

func AddCallerSkip(skip int) Option

AddCallerSkip increases the number of callers skipped by caller annotation (as enabled by the AddCaller option). When building wrappers around the Logger and SugaredLogger, supplying this Option prevents zap from always reporting the wrapper code as the caller.

func AddStacktrace

func AddStacktrace(lvl LevelEnabler) Option

AddStacktrace configures the Logger to record a stack trace for all messages at or above a given level.

func Fields

func Fields(fs ...Field) Option

Fields adds fields to the Logger.

func WithCaller

func WithCaller(enabled bool) Option

WithCaller configures the Logger to annotate each message with the filename, line number, and function name of zap's caller, or not, depending on the value of enabled. This is a generalized form of AddCaller.

func WithClock

func WithClock(clock Clock) Option

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

type Sampling

type Sampling struct {
	Initial    int `yaml:"initial"`
	Thereafter int `yaml:"thereafter"`
}

Sampling Sampling

Jump to

Keyboard shortcuts

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