log

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: MIT Imports: 12 Imported by: 16

Documentation

Index

Constants

View Source
const (
	OutputModeJson    = "json"
	OutputModeConsole = "console"
)

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug uses fmt.Sprint to construct and log a message.

func Debugc

func Debugc(ctx context.Context, msgFormat string, args ...interface{})

Debugc use WithCtx and fmt.Sprintf to log a template message.

func Debugf

func Debugf(format string, args ...interface{})

Debugf uses fmt.Sprintf to log a template message.

func Error

func Error(args ...interface{})

Error uses fmt.Sprint to construct and log a message.

func Errorc

func Errorc(ctx context.Context, msgFormat string, args ...interface{})

Errorc use WithCtx and fmt.Sprintf to log a template message.

func Errorf

func Errorf(msgFormat string, args ...interface{})

Errorf uses fmt.Sprintf to log a template message.

func Fatal

func Fatal(args ...interface{})

Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.

func Fatalc

func Fatalc(ctx context.Context, msgFormat string, args ...interface{})

Fatalc use WithCtx and fmt.Sprintf to log a template message.

func Fatalf

func Fatalf(msgFormat string, args ...interface{})

Fatalf uses fmt.Sprintf to log a template message, then calls os.Exit.

func Info

func Info(args ...interface{})

Info uses fmt.Sprint to construct and log a message.

func Infoc

func Infoc(ctx context.Context, msgFormat string, args ...interface{})

Infoc use WithCtx and fmt.Sprintf to log a template message.

func Infof

func Infof(msgFormat string, args ...interface{})

Infof uses fmt.Sprintf to log a template message.

func Panic

func Panic(args ...interface{})

Panic uses fmt.Sprint to construct and log a message, then panics.

func Panicc

func Panicc(ctx context.Context, msgFormat string, args ...interface{})

Panicc use WithCtx and fmt.Sprintf to log a template message.

func Panicf

func Panicf(msgFormat string, args ...interface{})

Panicf uses fmt.Sprintf to log a templated message, then panics.

func ReplaceGlobal

func ReplaceGlobal(logger *ZapLogger)

ReplaceGlobal Register a logger instance as global

func Warn

func Warn(args ...interface{})

Warn uses fmt.Sprint to construct and log a message.

func Warnc

func Warnc(ctx context.Context, msgFormat string, args ...interface{})

Warnc use WithCtx and fmt.Sprintf to log a template message.

func Warnf

func Warnf(msgFormat string, args ...interface{})

Warnf uses fmt.Sprintf to log a template message.

Types

type ContextExtractor

type ContextExtractor func(ctx context.Context) []field.Field

type ContextExtractors

type ContextExtractors []ContextExtractor

func (ContextExtractors) Extract

func (c ContextExtractors) Extract(ctx context.Context) []field.Field

func (ContextExtractors) IsExtractable

func (c ContextExtractors) IsExtractable() bool

type ContextualLogger

type ContextualLogger interface {
	// Debugc formats the message according to the format specifier
	// with some additional info in the context and logs it at debug level.
	Debugc(ctx context.Context, template string, args ...interface{})

	// Infoc formats the message according to the format specifier
	// with some additional info in the context and logs it at info level.
	Infoc(ctx context.Context, template string, args ...interface{})

	// Warnc formats the message according to the format specifier
	// with some additional info in the context and logs it at warn level.
	Warnc(ctx context.Context, template string, args ...interface{})

	// Errorc formats the message according to the format specifier
	// with some additional info in the context and logs it at error level.
	Errorc(ctx context.Context, template string, args ...interface{})

	// Fatalc formats the message according to the format specifier
	// with some additional info in the context and calls os.Exit.
	Fatalc(ctx context.Context, template string, args ...interface{})

	// Panicc formats the message according to the format specifier
	// with some additional info in the context and panics.
	Panicc(ctx context.Context, template string, args ...interface{})
}

type FiledKey

type FiledKey int
const (
	FieldKeyErr FiledKey = iota
	FieldKeyTime
	FieldKeyLevel
	FieldKeyName
	FieldKeyCaller
	FieldKeyFunc
	FieldKeyMsg
	FieldKeyStacktrace
)

type Logger

type Logger interface {
	StdLogger
	ContextualLogger

	// WithCtx adds additional info in the context and
	// additional fields to the logging context.
	WithCtx(ctx context.Context, additionalFields ...field.Field) Logger

	// WithField adds a variadic number of fields to the logging context.
	WithField(fields ...field.Field) Logger

	// WithError adds an error with FieldKeyErr field to the logging context.
	WithError(err error) Logger

	// WithErrors adds a field with FieldKeyErr field that carries a slice of errors.
	WithErrors(errs ...error) Logger

	// WithAny adds a key and an arbitrary value and chooses the best way to represent
	// them as a field.
	WithAny(key string, value interface{}) Logger
}

func GetGlobal

func GetGlobal() Logger

GetGlobal Get global logger instance

func WithAny

func WithAny(key string, value interface{}) Logger

func WithCtx

func WithCtx(ctx context.Context, additionalFields ...field.Field) Logger

func WithError

func WithError(err error) Logger

func WithErrors

func WithErrors(errs ...error) Logger

func WithField

func WithField(fields ...field.Field) Logger

type OptionFunc

type OptionFunc func(opt *Options)

func AddCallerSkip

func AddCallerSkip(skip int) OptionFunc

type Options

type Options struct {
	// Development puts the logger in development mode, which changes the
	// behavior of DPanicLevel and takes stack traces more liberally.
	Development bool

	// LogLevel is the minimum enabled logging level.
	// In Development mode, LogLevel will be set to DEBUG,
	// Opposite, LogLevel will be set to INFO mode automatically.
	LogLevel string

	// JsonOutputMode Enable or disable json output mode.
	JsonOutputMode 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, stacktrace-s are captured for WarnLevel and above logs in
	// development and ErrorLevel and above in production.
	DisableStacktrace bool

	// CallerSkip Set the number of callers
	// will be skipped before show caller
	CallerSkip int

	// FieldKeyMap Set the keys used for each log entry.
	// If any key is empty, that portion of the entry is omitted.
	FieldKeyMap map[FiledKey]string

	// ContextExtractors Define the list of extractors
	// that will be used when extract value from log context.
	ContextExtractors ContextExtractors
}

type Properties

type Properties struct {
	// Development puts the logger in development mode, which changes the
	// behavior of DPanicLevel and takes stack traces more liberally.
	Development bool `default:"false"`

	// LogLevel is the minimum enabled logging level.
	// In Development mode, LogLevel will be set to DEBUG,
	// Opposite, LogLevel will be set to INFO mode automatically.
	LogLevel string

	// JsonOutputMode Enable or disable json output mode.
	JsonOutputMode bool `default:"true"`

	// 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, stacktrace-s are captured for WarnLevel and above logs in
	// development and ErrorLevel and above in production.
	DisableStacktrace bool

	// CallerSkip Set the number of callers
	// will be skipped before show caller
	CallerSkip int `default:"1"`
}

func NewProperties

func NewProperties(loader config.Loader) (*Properties, error)

func (Properties) Prefix

func (l Properties) Prefix() string

type StdLogger

type StdLogger interface {
	// Debug logs the provided arguments at debug level.
	// Spaces are added between arguments when neither is a string.
	Debug(args ...interface{})

	// Info logs the provided arguments at debug level.
	// Spaces are added between arguments when neither is a string.
	Info(args ...interface{})

	// Warn logs the provided arguments at debug level.
	// Spaces are added between arguments when neither is a string.
	Warn(args ...interface{})

	// Error logs the provided arguments at debug level.
	// Spaces are added between arguments when neither is a string.
	Error(args ...interface{})

	// Fatal constructs a message with the provided arguments and calls os.Exit.
	// Spaces are added between arguments when neither is a string.
	Fatal(args ...interface{})

	// Panic constructs a message with the provided arguments and panics.
	// Spaces are added between arguments when neither is a string.
	Panic(args ...interface{})

	// Debugf formats the message according to the format specifier
	// and logs it at debug level.
	Debugf(template string, args ...interface{})

	// Infof formats the message according to the format specifier
	// and logs it at info level.
	Infof(template string, args ...interface{})

	// Warnf formats the message according to the format specifier
	// and logs it at warn level.
	Warnf(template string, args ...interface{})

	// Errorf formats the message according to the format specifier
	// and logs it at error level.
	Errorf(template string, args ...interface{})

	// Fatalf formats the message according to the format specifier
	// and calls os.Exit.
	Fatalf(template string, args ...interface{})

	// Panicf formats the message according to the format specifier
	// and panics.
	Panicf(template string, args ...interface{})
}

type TestingLogger

type TestingLogger struct {
	*ZapLogger
	// contains filtered or unexported fields
}

func NewTestingLogger

func NewTestingLogger(tb testing.TB, options *Options) (*TestingLogger, error)

func NewTestingLoggerFromDefault

func NewTestingLoggerFromDefault(tb testing.TB, defaultLogger *ZapLogger) *TestingLogger

type TestingWriter

type TestingWriter struct {
	// contains filtered or unexported fields
}

TestingWriter is a WriteSyncer that writes to the given testing.TB.

func NewTestingWriter

func NewTestingWriter(tb testing.TB) TestingWriter

func (TestingWriter) Sync

func (w TestingWriter) Sync() error

func (TestingWriter) Write

func (w TestingWriter) Write(p []byte) (n int, err error)

type ZapLogger

type ZapLogger struct {
	// contains filtered or unexported fields
}

func NewZapLogger

func NewZapLogger(opts *Options) (*ZapLogger, error)

func (*ZapLogger) Clone

func (l *ZapLogger) Clone(addedCallerSkip int, fields ...field.Field) *ZapLogger

func (*ZapLogger) Debug

func (l *ZapLogger) Debug(args ...interface{})

func (*ZapLogger) Debugc

func (l *ZapLogger) Debugc(ctx context.Context, template string, args ...interface{})

func (*ZapLogger) Debugf

func (l *ZapLogger) Debugf(template string, args ...interface{})

func (*ZapLogger) Error

func (l *ZapLogger) Error(args ...interface{})

func (*ZapLogger) Errorc

func (l *ZapLogger) Errorc(ctx context.Context, template string, args ...interface{})

func (*ZapLogger) Errorf

func (l *ZapLogger) Errorf(template string, args ...interface{})

func (*ZapLogger) Fatal

func (l *ZapLogger) Fatal(args ...interface{})

func (*ZapLogger) Fatalc

func (l *ZapLogger) Fatalc(ctx context.Context, template string, args ...interface{})

func (*ZapLogger) Fatalf

func (l *ZapLogger) Fatalf(template string, args ...interface{})

func (*ZapLogger) Info

func (l *ZapLogger) Info(args ...interface{})

func (*ZapLogger) Infoc

func (l *ZapLogger) Infoc(ctx context.Context, template string, args ...interface{})

func (*ZapLogger) Infof

func (l *ZapLogger) Infof(template string, args ...interface{})

func (*ZapLogger) Panic

func (l *ZapLogger) Panic(args ...interface{})

func (*ZapLogger) Panicc

func (l *ZapLogger) Panicc(ctx context.Context, template string, args ...interface{})

func (*ZapLogger) Panicf

func (l *ZapLogger) Panicf(template string, args ...interface{})

func (*ZapLogger) Warn

func (l *ZapLogger) Warn(args ...interface{})

func (*ZapLogger) Warnc

func (l *ZapLogger) Warnc(ctx context.Context, template string, args ...interface{})

func (*ZapLogger) Warnf

func (l *ZapLogger) Warnf(template string, args ...interface{})

func (*ZapLogger) WithAny

func (l *ZapLogger) WithAny(key string, value interface{}) Logger

func (*ZapLogger) WithCtx

func (l *ZapLogger) WithCtx(ctx context.Context, additionalFields ...field.Field) Logger

func (*ZapLogger) WithError

func (l *ZapLogger) WithError(err error) Logger

func (*ZapLogger) WithErrors

func (l *ZapLogger) WithErrors(errs ...error) Logger

func (*ZapLogger) WithField

func (l *ZapLogger) WithField(fields ...field.Field) Logger

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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