logger

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: MIT Imports: 4 Imported by: 13

Documentation

Index

Constants

View Source
const (
	FIELD_TRACE_ID = "trace_id"
	FIELD_SPAN_ID  = "span_id"
)
View Source
const (
	DefaultLogLevel       = InfoLevel
	DefaultLogLevelString = "info"
)

Variables

This section is empty.

Functions

func NewContext

func NewContext(ctx context.Context, l Logger) context.Context

Types

type Level

type Level int8
const (
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel Level = iota - 2
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
	// InfoLevel is the default logging priority.
	// General operational entries about what's going on inside the application.
	InfoLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	ErrorLevel
	// FatalLevel level. Logs and then calls `logger.Exit(1)`. highest level of severity.
	FatalLevel
)

func GetLevel

func GetLevel(levelStr string) (Level, error)

GetLevel converts a level string into a logger Level value. returns an error if the input string does not match known values.

func (Level) Enabled

func (l Level) Enabled(lvl Level) bool

Enabled returns true if the given level is at or above this level.

func (Level) String

func (l Level) String() string

type Logger

type Logger interface {
	// Init initializes options
	Init(options ...Option) error
	// The Logger options
	Options() Options
	// Fields set fields to always be logged
	Fields(fields map[string]interface{}) Logger

	// Log writes a log entry
	Log(ctx context.Context, level Level, args ...interface{})
	// Logf writes a formatted log entry
	Logf(ctx context.Context, level Level, format string, args ...interface{})
	// Log Trace
	Trace(ctx context.Context, args ...interface{})
	// Logf Trace
	Tracef(ctx context.Context, format string, args ...interface{})
	// Log Debug
	Debug(ctx context.Context, args ...interface{})
	// Logf Debug
	Debugf(ctx context.Context, format string, args ...interface{})
	// Log Info
	Info(ctx context.Context, args ...interface{})
	// Logf Info
	Infof(ctx context.Context, format string, args ...interface{})
	// Log Warn
	Warn(ctx context.Context, args ...interface{})
	// Logf Warn
	Warnf(ctx context.Context, format string, args ...interface{})
	// Log Error
	Error(ctx context.Context, args ...interface{})
	// Logf Error
	Errorf(ctx context.Context, format string, args ...interface{})
	// Log Fatal
	Fatal(ctx context.Context, args ...interface{})
	// Logf Fatal
	Fatalf(ctx context.Context, format string, args ...interface{})

	// String returns the name of logger
	String() string
}

func FromContext

func FromContext(ctx context.Context) (Logger, bool)

type Option

type Option func(*Options)

func SetOption

func SetOption(k, v interface{}) Option

func WithCallerSkipCount

func WithCallerSkipCount(c int) Option

WithCallerSkipCount set frame count to skip.

func WithFields

func WithFields(fields map[string]interface{}) Option

WithFields set default fields for the logger.

func WithLevel

func WithLevel(level Level) Option

WithLevel set default level for the logger.

func WithOutput

func WithOutput(out io.Writer) Option

WithOutput set default output writer for the logger.

func WithTracer

func WithTracer(t trace.Tracer) Option

type Options

type Options struct {
	// It's common to set this to a file, or leave it default which is `os.Stderr`
	Out io.Writer
	// fields to always be logged
	Fields map[string]interface{}
	// Caller skip frame count for file:line info
	CallerSkipCount int
	// The logging level the logger should log at. default is `InfoLevel`
	Level Level
	// Alternative options
	Context context.Context
	//tracer for extract Trace ID, Span ID
	Tracer trace.Tracer
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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