log

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewObserverWriter

func NewObserverWriter(lvl Level, out Output) (Writer, *ObservedLog)

NewObserverWriter return new Writer implementer that write logs to memory and also return ObservedLog to help assert and check logged Log(s).

func WithCtx

func WithCtx(ctx context.Context, w Logger) context.Context

WithCtx return a copy of ctx with given logger attached.

Types

type Config

type Config struct {
	NR   NRConfig
	File FileConfig
}

Config required object that holds any necessary data used by each log output implementation

func NewConfig

func NewConfig(opts ...ConfigOpt) *Config

NewConfig return new Config after applying given options.

type ConfigOpt

type ConfigOpt func(*Config)

func WithFileAge

func WithFileAge(age int) ConfigOpt

WithFileAge set maximum number of days to retain old log files based on the timestamp encoded in their filename

func WithFileMaxBackup

func WithFileMaxBackup(max int) ConfigOpt

WithFileMaxBackup set maximum number of old log files to retain before got removed.

func WithFilePath

func WithFilePath(p string) ConfigOpt

WithFilePath set target readable directory + local file which the log data will be written.

func WithFileSize

func WithFileSize(size int) ConfigOpt

WithFileSize set maximum file log size in megabytes before got rotated.

func WithNRAppName

func WithNRAppName(name string) ConfigOpt

WithNRAppName set new relic application name.

func WithNRLicense

func WithNRLicense(lic string) ConfigOpt

WithNRLicense set new relic license.

type FileConfig

type FileConfig struct {
	Path string
	Size int
	Age  int
	Num  int
}

FileConfig specific config for file as the log output

type Level

type Level int8

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

const (
	// DebugLevel most verbose logs, and are usually disabled in production.
	DebugLevel Level = iota
	// InfoLevel is the default logging priority.
	InfoLevel
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel
)

func ParseLevel

func ParseLevel(lvl string) Level

ParseLevel parses a level based on the lower-case representation of the log level.

type Log

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

Log object that holds data for each field inserted to each log message. How Logger implementer is treating this object should read the field typ and follow the guideline from Type and each of the supported types.

func Any

func Any(k string, any interface{}) Log

Any constructs a Log with the given key and value. This set the type to AnyType.

func Bool

func Bool(k string, b bool) Log

Bool constructs a Log with the given key and value. This set the type to BoolType.

func Error

func Error(err error) Log

Error constructs a Log with the given err value and 'error' as the key. This set the type to ErrorType.

func Float

func Float(k string, f float64) Log

Float constructs a Log with the given key and value. This set the type to FloatType.

func Num

func Num(k string, num int) Log

Num constructs a Log with the given key and value. This set the type to NumType.

func String

func String(k, v string) Log

String constructs a Log with the given key and value. This set the type to StringType.

type Logger

type Logger interface {
	// Init do initialization and should be called at very first before
	// other functions.
	Init(dur time.Duration)
	// Flush do clean up task that make sure any leftover logs written properly
	// to the destination by Log therefor should be called at very last
	// after other functions.
	Flush(dur time.Duration)
	// With add given Log(s) as structured context.
	With(pr ...Log) Logger
	// Group create new group or namespace with given key and Log(s) as the
	// content. In JSON, this is like creating new object using given key and
	// Log(s) as the fields.
	//
	// Ref(s):
	//  - https://pkg.go.dev/go.uber.org/zap#Namespace
	//  - https://pkg.go.dev/golang.org/x/exp/slog#Group
	Group(key string, pr ...Log) Logger
	// Dbg logs a message at DebugLevel.
	Dbg(msg string, pr ...Log)
	// Inf logs a message at InfoLevel.
	Inf(msg string, pr ...Log)
	// Wrn logs a message at WarnLevel.
	Wrn(msg string, pr ...Log)
	// Err logs a message at ErrorLevel.
	Err(msg string, pr ...Log)
}

Logger unified front-end to log.

func FromCtx

func FromCtx(ctx context.Context) Logger

FromCtx return the singleton Logger associated with given ctx. If no logger is associated, the default logger is returned, unless it is nil in which case a no-op logger is returned.

func NewNop

func NewNop() Logger

NewNop returns a no-op Logger. Do nothing and never writes out any logs.

func NewSlogLogger

func NewSlogLogger(wr ...Writer) Logger

NewSlogLogger return Logger implementer that use stdlib slog as the backend.

func NewZapLogger

func NewZapLogger(wr ...Writer) Logger

NewZapLogger return Logger implementer that use zap as the backend.

type NRConfig

type NRConfig struct {
	Name    string
	License string
}

NRConfig specific config for new relic as the log output

type ObservedLog

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

ObservedLog is a concurrency-safe, ordered collection of observed Log(s).

func (*ObservedLog) All

func (o *ObservedLog) All() []loggedLog

All returns a copy of all the observed logs.

func (*ObservedLog) Flush

func (o *ObservedLog) Flush(_ time.Duration)

func (*ObservedLog) Len

func (o *ObservedLog) Len() int

Len returns the number of items in the collection.

func (*ObservedLog) Level

func (o *ObservedLog) Level() Level

func (*ObservedLog) Output

func (o *ObservedLog) Output() Output

func (*ObservedLog) TakeAll

func (o *ObservedLog) TakeAll() []loggedLog

TakeAll returns a copy of all the observed logs, and truncates the observed slice.

func (*ObservedLog) Wait

func (o *ObservedLog) Wait(_ time.Duration)

func (*ObservedLog) Write

func (o *ObservedLog) Write(p []byte) (n int, err error)

func (*ObservedLog) Writer

func (o *ObservedLog) Writer() io.Writer

type Output

type Output int8

Output define currently supported target output for logging.

const (
	CONSOLE  Output = iota // CONSOLE target log output to console/terminal
	NEWRELIC               // NEWRELIC target log output directly to new relic via their client sdk
	FILE                   // FILE target log output to local file
)

type Type

type Type uint8

Type indicates how the Logger implementer should treat each Log.

const (
	// StringType use field str string of Log as the value.
	StringType Type = iota
	// NumType use field num int of Log as the value.
	NumType
	// FloatType use field flt float64 of Log as the value.
	FloatType
	// BoolType use field b bool of Log as the value.
	BoolType
	// AnyType use field any interface of Log as the value.
	AnyType
	// ErrorType use field err from error interface of Log as the value.
	ErrorType
)

type Writer

type Writer interface {
	// Writer return where and how the implementer should write the logs.
	Writer() io.Writer
	// Output define the Output type.
	Output() Output
	// Level define the logs level.
	Level() Level
	// Wait in case the implementer need some delay or preparation before can write any logs.
	Wait(dur time.Duration)
	// Flush any necessary clean up task that will be run by Producer at the last order.
	Flush(dur time.Duration)
}

Writer unified log writer that's responsible where the log from Logger should be written to.

func NewConsoleWriter

func NewConsoleWriter(lvl Level) Writer

NewConsoleWriter return Writer implementer that write logs to os.Stdout and set given lvl as the log Level.

func NewFileWriter

func NewFileWriter(lvl Level, cnf *Config) Writer

NewFileWriter return Writer implementer that write logs to designated file based on the given Config.File and set given lvl as the log Level.

func NewNewrelicWriter

func NewNewrelicWriter(lvl Level, cnf *Config) Writer

NewNewrelicWriter return Writer implementer that ingest logs directly to newrelic server by given Config.NR and set given Level as the log level.

Jump to

Keyboard shortcuts

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