Documentation ¶
Index ¶
- func NewObserverWriter(lvl Level, out Output) (Writer, *ObservedLog)
- func WithCtx(ctx context.Context, w Logger) context.Context
- type Config
- type ConfigOpt
- type FileConfig
- type Level
- type Log
- type Logger
- type NRConfig
- type ObservedLog
- func (o *ObservedLog) All() []loggedLog
- func (o *ObservedLog) Flush(_ time.Duration)
- func (o *ObservedLog) Len() int
- func (o *ObservedLog) Level() Level
- func (o *ObservedLog) Output() Output
- func (o *ObservedLog) TakeAll() []loggedLog
- func (o *ObservedLog) Wait(_ time.Duration)
- func (o *ObservedLog) Write(p []byte) (n int, err error)
- func (o *ObservedLog) Writer() io.Writer
- type Output
- type Type
- type Writer
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).
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config required object that holds any necessary data used by each log output implementation
type ConfigOpt ¶
type ConfigOpt func(*Config)
ConfigOpt options for Config. Not recommended to be used directly.
func WithFileAge ¶
WithFileAge set maximum number of days to retain old log files based on the timestamp encoded in their filename
func WithFileMaxBackup ¶
WithFileMaxBackup set maximum number of old log files to retain before got removed.
func WithFilePath ¶
WithFilePath set target readable directory + local file which the log data will be written.
func WithFileSize ¶
WithFileSize set maximum file log size in megabytes before got rotated.
func WithNRAppName ¶
WithNRAppName set new relic application name.
type FileConfig ¶
type FileConfig struct {
// contains filtered or unexported fields
}
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 ¶
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 Error ¶
Error constructs a Log with the given err value and 'error' as the key. This set the type to ErrorType.
type Logger ¶
type Logger interface { // Init do initialization and should be called at very first before // any other functions. Init(dur time.Duration) // Flush do clean up task that make sure any leftover logs written properly // to the destination by the Log therefor this should be called at very // last after other functions e.g. when gracefully shutting down server. 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 ¶
FromCtx return the singleton Logger associated with given ctx. If no logger is associated, the default logger is returned, otherwise if no logger exist in the context, no-op logger will be returned instead.
func NewNop ¶
func NewNop() Logger
NewNop returns a no-op Logger. Do nothing and never writes out any logs.
func NewSlogLogger ¶
NewSlogLogger return Logger implementer that use stdlib slog as the backend.
func NewZapLogger ¶
NewZapLogger return Logger implementer that use zap as the backend.
type NRConfig ¶
type NRConfig struct {
// contains filtered or unexported fields
}
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) Writer ¶
func (o *ObservedLog) Writer() io.Writer
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 ¶
NewConsoleWriter return Writer implementer that write logs to os.Stdout and set given lvl as the log Level.
func NewFileWriter ¶
NewFileWriter return Writer implementer that write logs to designated file based on the given Config and set given lvl as the log Level.
func NewNewrelicWriter ¶
NewNewrelicWriter return Writer implementer that ingest logs directly to newrelic server by given Config and set given Level as the log level.