log

package module
v0.0.0-...-eb33cdb Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: Apache-2.0 Imports: 6 Imported by: 5

README

Log

Contains abstraction for logging driver. The driver implementation should conform Logger interface.

Documentation

Overview

This package provide logger abstraction and default implementation. This package also provide global logger which use the default implementation by defaults. Libraries might use the global logger to log their internal event or debuging purpose, libraries that use global logger should use Debug level to avoid cluter the user logs. SimpleLogger which is the default implementation should be replaced by more reliable logging library implementation such as `zaplog`, you can set global logger by calling: log.SetLogger(logger)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, optfields ...LogField)

Debug logs a message at DebugLevel using global logger. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.

func Error

func Error(msg string, err error, optfields ...LogField)

Error logs a message at ErrorLevel using global logger, put the passed error in "error" field. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.

func Fatal

func Fatal(msg string, err error, optfields ...LogField)

Fatal logs a message at FatalLevel using global logger, then calls os.Exit(1). This should only be use with extra care, ideally Fatal should only be used in main where the apps encountered an error and have noway to continue. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.

func Info

func Info(msg string, optfields ...LogField)

Info logs a message at InfoLevel using global logger. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.

func SetLogger

func SetLogger(logger Logger)

SetLogger set global logger.

func Warn

func Warn(msg string, optfields ...LogField)

Warn logs a message at WarnLevel using global logger. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.

Types

type LogField

type LogField struct {
	Key   string
	Value any
}

LogField is an additional structured field

func Field

func Field(k string, v any) LogField

type LogLevel

type LogLevel int8
const (
	FatalLogLevel LogLevel = 3
	ErrorLogLevel LogLevel = 2
	WarnLogLevel  LogLevel = 1
	InfoLogLevel  LogLevel = 0
	DebugLogLevel LogLevel = -1
)

type Logger

type Logger interface {
	// Fatal logs a message at FatalLevel, then calls os.Exit(1). This should only be use with extra care, ideally Fatal
	// should only be used in main where the apps encountered an error and have noway to continue.
	// optfields is optional, when supplied it will be added as new field using
	// Key as field name, and Value as it's value.
	Fatal(msg string, err error, optfields ...LogField)
	// Error logs a message at ErrorLevel, put the passed error in "error" field.
	// optfields is optional, when supplied it will be added as new field using
	// Key as field name, and Value as it's value.
	Error(msg string, err error, optfields ...LogField)
	// Warn logs a message at WarnLevel.
	// optfields is optional, when supplied it will be added as new field using
	// Key as field name, and Value as it's value.
	Warn(msg string, optfields ...LogField)
	// Info logs a message at InfoLevel.
	// optfields is optional, when supplied it will be added as new field using
	// Key as field name, and Value as it's value.
	Info(msg string, optfields ...LogField)
	// Debug logs a message at DebugLevel.
	// optfields is optional, when supplied it will be added as new field using
	// Key as field name, and Value as it's value.
	Debug(msg string, optfields ...LogField)
	// WithContext return Logger instance that will use passed context to log additional info,
	// such as opentelemetry's SpanID and TraceID if applicable.
	WithContext(ctx context.Context) Logger
}

Logger log message to underlying logger. Any logging driver should implements Logger interface. The optfield should be optional, and when it's not nil/empty it should be logged as structured key/value.

func GetLogger

func GetLogger() Logger

GetLogger get global logger, by default global logger use SimpleLogger and use standard log default writer (log.Default().Writer()) as it's writer. Use SetLogger to set global logger.

func WithContext

func WithContext(ctx context.Context) Logger

WithContext return Logger instance that will use passed context to log additional info, such as opentelemetry's SpanID and TraceID if applicable.

type NoOpLogger

type NoOpLogger struct{}

NoOpLogger will not writes out logs to any output. All NoOpLogger method basically doesn't do anything except for Fatal, it will simply call os.Exit(1).

func (*NoOpLogger) Debug

func (l *NoOpLogger) Debug(msg string, optfields ...LogField)

func (*NoOpLogger) Error

func (l *NoOpLogger) Error(msg string, err error, optfields ...LogField)

func (*NoOpLogger) Fatal

func (l *NoOpLogger) Fatal(msg string, err error, optfields ...LogField)

Fatal call os.Exit(1)

func (*NoOpLogger) Info

func (l *NoOpLogger) Info(msg string, optfields ...LogField)

func (*NoOpLogger) Warn

func (l *NoOpLogger) Warn(msg string, optfields ...LogField)

func (*NoOpLogger) WithContext

func (l *NoOpLogger) WithContext(ctx context.Context) Logger

type SimpleLogger

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

func NewSimpleLogger

func NewSimpleLogger(w io.Writer, lv LogLevel) *SimpleLogger

func (*SimpleLogger) Debug

func (l *SimpleLogger) Debug(msg string, optfields ...LogField)

func (*SimpleLogger) Error

func (l *SimpleLogger) Error(msg string, err error, optfields ...LogField)

func (*SimpleLogger) Fatal

func (l *SimpleLogger) Fatal(msg string, err error, optfields ...LogField)

Fatal call os.Exit(1)

func (*SimpleLogger) Info

func (l *SimpleLogger) Info(msg string, optfields ...LogField)

func (*SimpleLogger) Warn

func (l *SimpleLogger) Warn(msg string, optfields ...LogField)

func (*SimpleLogger) WithContext

func (l *SimpleLogger) WithContext(ctx context.Context) Logger

type WriterFunc

type WriterFunc func(msg string, optfields ...LogField)

WriterFunc takes Logger's log method signature to implement io.Writer, this is useful when you want to use Logger as standard log's output. ie. stdlog.SetOutput(log.WriterFunc(logger.Debug))

func (WriterFunc) Write

func (w WriterFunc) Write(m []byte) (n int, err error)

Directories

Path Synopsis
drivers
zaplog Module

Jump to

Keyboard shortcuts

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