Documentation ¶
Index ¶
- Constants
- Variables
- func Debug(ctx context.Context, msg string)
- func Error(ctx context.Context, msg string)
- func Fatal(ctx context.Context, msg string)
- func Info(ctx context.Context, msg string)
- func NewDevelopmentLogger(lvl Level) (StdLogger, Flusher)
- func NewProductionLogger(lvl Level) (StdLogger, Flusher)
- func NewWithEncoder(lvl Level, enc Encoder) (StdLogger, Flusher)
- func SetCheckLogger(l CheckLogger)
- func SetLogger(l Logger)
- func Warning(ctx context.Context, msg string)
- type BufWriter
- type CheckLogger
- type CheckedLogger
- func CheckDebug(ctx context.Context, msg string) (CheckedLogger, bool)
- func CheckError(ctx context.Context, msg string) (CheckedLogger, bool)
- func CheckFatal(ctx context.Context, msg string) (CheckedLogger, bool)
- func CheckInfo(ctx context.Context, msg string) (CheckedLogger, bool)
- func CheckWarning(ctx context.Context, msg string) (CheckedLogger, bool)
- type Checker
- type CheckerFunc
- type Checkers
- type Decorator
- type DecoratorFunc
- type Decorators
- type DeduplicatorWriter
- type Encoder
- type Entry
- type ErrorHandler
- type Field
- func Bool(k string, v bool) Field
- func Bools(k string, v []bool) Field
- func Byte(k string, v byte) Field
- func Bytes(k string, v []byte) Field
- func Err(err error) Field
- func Errs(errs []error) Field
- func Float32(k string, v float32) Field
- func Float32s(k string, v []float32) Field
- func Float64(k string, v float64) Field
- func Float64s(k string, v []float64) Field
- func Int(k string, v int) Field
- func Int16(k string, v int16) Field
- func Int16s(k string, v []int16) Field
- func Int32(k string, v int32) Field
- func Int32s(k string, v []int32) Field
- func Int64(k string, v int64) Field
- func Int64s(k string, v []int64) Field
- func Int8(k string, v int8) Field
- func Int8s(k string, v []int8) Field
- func Ints(k string, v []int) Field
- func Mapper(k string, v FieldMapper) Field
- func Mappers(k string, v []FieldMapper) Field
- func String(k string, v string) Field
- func Strings(k string, v []string) Field
- func Time(k string, v time.Time) Field
- func Times(k string, v []time.Time) Field
- func Uint(k string, v uint) Field
- func Uint16(k string, v uint16) Field
- func Uint16s(k string, v []uint16) Field
- func Uint32(k string, v uint32) Field
- func Uint32s(k string, v []uint32) Field
- func Uint64(k string, v uint64) Field
- func Uint64s(k string, v []uint64) Field
- func Uint8(k string, v uint8) Field
- func Uint8s(k string, v []uint8) Field
- func Uints(k string, v []uint) Field
- type FieldMapper
- type Fields
- type Flusher
- type JsonConfig
- type JsonEncoder
- type Level
- type LevelChecker
- type LevelFormatter
- type LeveledWriter
- type LeveledWriterOptionFunc
- type Logger
- type Message
- type MinSeverity
- type MultiWriter
- type NoopCheckedLogger
- type Option
- type OptionFunc
- type StackTraceDecorator
- type StdCheckedLogger
- type StdEntry
- type StdLogger
- func (l StdLogger) CheckDebug(ctx context.Context, msg Message) (CheckedLogger, bool)
- func (l StdLogger) CheckError(ctx context.Context, msg Message) (CheckedLogger, bool)
- func (l StdLogger) CheckFatal(ctx context.Context, msg Message) (CheckedLogger, bool)
- func (l StdLogger) CheckInfo(ctx context.Context, msg Message) (CheckedLogger, bool)
- func (l StdLogger) CheckWarn(ctx context.Context, msg Message) (CheckedLogger, bool)
- func (l StdLogger) Debug(ctx context.Context, msg Message)
- func (l StdLogger) Error(ctx context.Context, msg Message)
- func (l StdLogger) Fatal(ctx context.Context, msg Message)
- func (l StdLogger) Info(ctx context.Context, msg Message)
- func (l StdLogger) Warn(ctx context.Context, msg Message)
- func (l StdLogger) With(fields ...Field) Logger
- func (l StdLogger) WithCheckers(cs ...Checker) StdLogger
- func (l StdLogger) WithDecorator(ds ...Decorator) StdLogger
- type TextConfig
- type TextEncoder
- type TickFlusher
- type TimestampDecorator
- type Writer
Examples ¶
Constants ¶
const ( COLOUR_RESET = "\033[0m" COLOUR_RED = "\033[31m" COLOUR_REDBG = "\033[41m" COLOUR_GREEN = "\033[32m" COLOUR_YELLOW = "\033[33m" COLOUR_BLUE = "\033[34m" COLOUR_WHITE = "\033[97m" )
Variables ¶
var ( ErrEntriesNotFlushed = errors.New("golog: could not flush entries") ErrEntryNotWritten = errors.New("golog: could not write entry") )
var ErrLevelNotParsed = errors.New("golog: could not parse level")
ErrLevelNotParsed is an error returned when a given string can't be parsed as a log Level
Functions ¶
func NewDevelopmentLogger ¶ added in v1.12.0
NewDevelopmentLogger returns a pre-configured logger for development environment
func NewProductionLogger ¶ added in v1.12.0
NewProductionLogger returns a pre-configured logger for production environment
func NewWithEncoder ¶ added in v1.12.0
NewWithEncoder returns a preset logger with a customer Encoder
func SetCheckLogger ¶
func SetCheckLogger(l CheckLogger)
SetCheckLogger overrides the base CheckLogger
Types ¶
type BufWriter ¶
type BufWriter struct { Encoder Encoder Writer *bufio.Writer ErrHandler ErrorHandler DefaultLogLevel Level // contains filtered or unexported fields }
BufWriter is a Writer which holds a buffer behind the scene to reduce sys calls
func NewBufWriter ¶
func NewBufWriter( enc Encoder, w *bufio.Writer, errHandler ErrorHandler, defaultLogLevel Level, ) *BufWriter
NewBufWriter returns a BufWriter
func (*BufWriter) Write ¶
Write writes an Entry to the buffer This method is implemented to add provide support the std library log package
func (*BufWriter) WriteEntry ¶ added in v0.1.0
WriteEntry writes an Entry to the buffer
type CheckLogger ¶
type CheckLogger interface { CheckDebug(context.Context, Message) (CheckedLogger, bool) CheckInfo(context.Context, Message) (CheckedLogger, bool) CheckWarn(context.Context, Message) (CheckedLogger, bool) CheckError(context.Context, Message) (CheckedLogger, bool) CheckFatal(context.Context, Message) (CheckedLogger, bool) }
CheckLogger is a logger able to check if a message should be written
type CheckedLogger ¶
type CheckedLogger interface {
Log(...Field)
}
CheckedLogger logs an already checked log
func CheckDebug ¶
func CheckDebug(ctx context.Context, msg string) (CheckedLogger, bool)
CheckDebug calls the base Logger's CheckDebug method
func CheckError ¶
func CheckError(ctx context.Context, msg string) (CheckedLogger, bool)
CheckError calls the base Logger's CheckError method
func CheckFatal ¶
func CheckFatal(ctx context.Context, msg string) (CheckedLogger, bool)
CheckFatal calls the base Logger's CheckFatal method
func CheckInfo ¶
func CheckInfo(ctx context.Context, msg string) (CheckedLogger, bool)
CheckInfo calls the base Logger's CheckInfo method
func CheckWarning ¶
func CheckWarning(ctx context.Context, msg string) (CheckedLogger, bool)
CheckWarning calls the base Logger's CheckWarning method
type CheckerFunc ¶
CheckerFunc is a handy function which implements Checker
func (CheckerFunc) Check ¶
func (fn CheckerFunc) Check(e Entry) bool
Check checks if an entry should proceed to be written when using a CheckLogger
type DecoratorFunc ¶
DecoratorFunc is a handy function which implements Decorator
func (DecoratorFunc) Decorate ¶
func (fn DecoratorFunc) Decorate(e Entry) Entry
Decorate changes the entry with custom logic and return the new modified one
type DeduplicatorWriter ¶ added in v1.8.0
type DeduplicatorWriter struct {
Default Writer
}
DeduplicatorWriter is a Writer which deduplicate fields with the same name
func NewDeduplicatorWriter ¶ added in v1.8.0
func NewDeduplicatorWriter( defaultWriter Writer, ) *DeduplicatorWriter
NewDeduplicatorWriter returns a DeduplicatorWriter
func (*DeduplicatorWriter) Flush ¶ added in v1.14.1
func (m *DeduplicatorWriter) Flush() error
Flush flushes the data
func (*DeduplicatorWriter) Write ¶ added in v1.8.0
func (m *DeduplicatorWriter) Write(msg []byte) (int, error)
Write calls the Default Write method
func (*DeduplicatorWriter) WriteEntry ¶ added in v1.8.0
func (m *DeduplicatorWriter) WriteEntry(e Entry)
WriteEntry writes an Entry to the related Writer If not found, then fallback on the Default
type Entry ¶
type Entry interface { Level() Level Message() Message Fields() Fields Context() context.Context With(...Field) Entry }
Entry is a log entry
type ErrorHandler ¶
type ErrorHandler func(error)
ErrorHandler is a function which handle logging error in order to avoid returning it
func DefaultErrorHandler ¶
func DefaultErrorHandler() ErrorHandler
DefaultErrorHandler returns the default error handler
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field is a key value pair representing a log entry metadata field
func Mapper ¶ added in v1.5.0
func Mapper(k string, v FieldMapper) Field
Mapper creates a field containing a value of type "Fields"
func Mappers ¶ added in v1.13.0
func Mappers(k string, v []FieldMapper) Field
Mappers creates a field containing a value of type "Fields"
type FieldMapper ¶ added in v1.5.0
type FieldMapper interface {
ToFields() Fields
}
FieldMapper an interface which returns Fields to add to an Entry
type Flusher ¶
type Flusher interface {
Flush() error
}
Flusher ensure that the data that a Writer may hold is written
type JsonConfig ¶
JsonConfig is a configuration for JsonEncoder
func DefaultJsonConfig ¶
func DefaultJsonConfig() JsonConfig
DefaultJsonConfig returns a default JsonConfig
type JsonEncoder ¶
type JsonEncoder struct {
Config JsonConfig
}
JsonEncoder is an encoder for json
func NewJsonEncoder ¶
func NewJsonEncoder(cfg JsonConfig) JsonEncoder
NewJsonEncoder returns a JsonEncoder
type Level ¶
type Level int
Level is a log severity level
const ( DEBUG Level INFO WARN ERROR FATAL )
func ParseLevel ¶
ParseLevel returns a Level given a string, returns an error in case the string is not a recognized one
type LevelChecker ¶
type LevelChecker struct {
MinSeverity MinSeverity
}
LevelChecker is a Checker which ensure the log has an expected Level
func NewLevelChecker ¶
func NewLevelChecker(minSev MinSeverity) LevelChecker
NewLevelChecker returns a LevelChecker with the given MinSeverity
func (LevelChecker) Check ¶
func (lc LevelChecker) Check(e Entry) bool
Check returns true if the Entry's Level is at least the one configured
type LevelFormatter ¶ added in v1.11.0
func ColoredLevelFormatter ¶ added in v1.12.0
func ColoredLevelFormatter() LevelFormatter
ColoredLevelFormatter returns a colored LevelFormatter
func DefaultLevelFormatter ¶ added in v1.12.0
func DefaultLevelFormatter() LevelFormatter
DefaultLevelFormatter returns a default LevelFormatter
type LeveledWriter ¶ added in v1.2.0
LeveledWriter is a Writer which based on the log level will write to a writer It also uses a Default one for the Write method as well as supporting the case when the Writer is not found in the Level map
func NewLeveledWriter ¶ added in v1.2.0
func NewLeveledWriter( defaultWriter Writer, fns ...LeveledWriterOptionFunc, ) *LeveledWriter
NewLeveledWriter returns a LeveledWriter
func (*LeveledWriter) Flush ¶ added in v1.14.1
func (m *LeveledWriter) Flush() error
Flush flushes the data
func (*LeveledWriter) Write ¶ added in v1.2.0
func (m *LeveledWriter) Write(msg []byte) (int, error)
Write calls the Default Write method
func (*LeveledWriter) WriteEntry ¶ added in v1.2.0
func (m *LeveledWriter) WriteEntry(e Entry)
WriteEntry writes an Entry to the related Writer If not found, then fallback on the Default
type LeveledWriterOptionFunc ¶ added in v1.2.0
type LeveledWriterOptionFunc func(*LeveledWriter)
LeveledWriterOptionFunc is a handy function which implements attach a Writer for a given Level in a LeveledWriter
func DefaultLeveledWriterOptionFunc ¶ added in v1.2.0
func DefaultLeveledWriterOptionFunc(lvl Level, w Writer) LeveledWriterOptionFunc
DefaultLeveledWriterOptionFunc implements LeveledWriterOptionFunc
type Logger ¶
type Logger interface { Debug(context.Context, Message) Info(context.Context, Message) Warn(context.Context, Message) Error(context.Context, Message) Fatal(context.Context, Message) With(...Field) Logger }
Logger is a logger able to write custom log Message with Fields
Example ¶
ctx := context.Background() t, err := time.Parse("2006", "2021") if err != nil { fmt.Println(err) return } w := golog.NewBufWriter( golog.NewJsonEncoder(golog.DefaultJsonConfig()), bufio.NewWriter(os.Stdout), golog.DefaultErrorHandler(), golog.DEBUG, ) defer w.Flush() golog.SetLogger(golog.New(w, golog.NewLevelCheckerOption(golog.DEBUG))) logger := golog.With(golog.String("hello", "world")) logger.Error(ctx, "an error message") logger.Error(ctx, `another error message`) loggerWithErr := logger.With(golog.Err(fmt.Errorf("error: ops!"))) logger.Info(ctx, "an info message") loggerWithErr.Warn(ctx, "a warning message") loggerWithErr.With(golog.Mapper("user", user{ID: "uuid", Reference: 123, Birthdate: t})).Error(ctx, "a warning message")
Output: {"level":"ERROR","message":"an error message","hello":"world"} {"level":"ERROR","message":"another error \nmessage","hello":"world"} {"level":"INFO","message":"an info \tmessage","hello":"world"} {"level":"WARN","message":"a warning message","hello":"world","error":"error: ops!"} {"level":"ERROR","message":"a warning message","hello":"world","error":"error: ops!","user":{"id":"uuid","ref":123,"birthdate":"2021-01-01T00:00:00Z"}}
type MultiWriter ¶ added in v1.2.0
type MultiWriter struct {
Writers []Writer
}
MultiWriter is a Writer which based on the log level will write to a writer It also uses a Default one for the Write method as well as supporting the case when the Writer is not found in the Level map
func NewMultiWriter ¶ added in v1.2.0
func NewMultiWriter( ws ...Writer, ) *MultiWriter
NewMultiWriter returns a MultiWriter
func (*MultiWriter) Flush ¶ added in v1.14.0
func (m *MultiWriter) Flush() error
Flush flushes the data to the related Writer
func (*MultiWriter) Write ¶ added in v1.2.0
func (m *MultiWriter) Write(msg []byte) (int, error)
Write calls the Default Write method
func (*MultiWriter) WriteEntry ¶ added in v1.2.0
func (m *MultiWriter) WriteEntry(e Entry)
WriteEntry writes an Entry to the related Writer If not found, then fallback on the Default
type NoopCheckedLogger ¶
type NoopCheckedLogger struct{}
NoopCheckedLogger is a nil-like CheckedLogger
type Option ¶
Option modifies a StdLogger and returns the modified one
func NewLevelCheckerOption ¶
func NewLevelCheckerOption(minSev MinSeverity) Option
NewLevelCheckerOption returns an Option which applies a LevelChecker with the given MinSeverity
func NewStackTraceDecoratorOption ¶
NewStackTraceDecoratorOption returns an Option which applies a StackTraceDecorator with the given field name
func NewTimestampDecoratorOption ¶ added in v0.2.0
NewTimestampDecoratorOption returns an Option which applies a TimestampDecorator with the given field name
type OptionFunc ¶
OptionFunc is a handy function which implements Option
func (OptionFunc) Apply ¶
func (fn OptionFunc) Apply(l StdLogger) StdLogger
Apply change a StdLogger with custom logic and return the new modified one
type StackTraceDecorator ¶
StackTraceDecorator is a Decorator which add the log stacktrace
func NewStackTraceDecorator ¶
func NewStackTraceDecorator(n string, depth int) StackTraceDecorator
NewStackTraceDecorator returns a StackTraceDecorator with the given field name
func (StackTraceDecorator) Decorate ¶
func (sd StackTraceDecorator) Decorate(e Entry) Entry
Decorate adds the stacktrace to the entry
type StdCheckedLogger ¶
StdCheckedLogger is a CheckedLogger which will write when called
func (StdCheckedLogger) Log ¶
func (l StdCheckedLogger) Log(flds ...Field)
Log writes a log with the given Fields Log panics with the message if the Level is FATAL
type StdEntry ¶
StdEntry is a representation of the standard log Entry
func NewStdEntry ¶
NewStdEntry returns a StdEntry
func (StdEntry) Context ¶
Context returns an entry assigned to the entry This could be used to enrich the entry after it get created for the first time
type StdLogger ¶
type StdLogger struct { Writer Writer Fields Fields Decorators Decorators Checkers Checkers }
StdLogger is a representation of the standard Logger
func (StdLogger) CheckDebug ¶
CheckDebug returns a CheckedLogger and a guard When the guard is true and the CheckDebug is called a log with the DEBUG Level is written
func (StdLogger) CheckError ¶
CheckError returns a CheckedLogger and a guard When the guard is true and the CheckError is called a log with the ERROR Level is written
func (StdLogger) CheckFatal ¶
CheckFatal returns a CheckedLogger and a guard When the guard is true and the with is called a log with the FATAL Level is written CheckFatal will also panic with the given message
func (StdLogger) CheckInfo ¶
CheckInfo returns a CheckedLogger and a guard When the guard is true and the CheckInfo is called a log with the INFO Level is written
func (StdLogger) CheckWarn ¶ added in v1.0.0
CheckWarn returns a CheckedLogger and a guard When the guard is true and the CheckWarn is called a log with the WARN Level is written
func (StdLogger) Fatal ¶
Fatal writes a log with the FATAL Level Fatal also panic with the given message
func (StdLogger) WithCheckers ¶
WithCheckers returns a new StdLogger appending the given extra Checkers
func (StdLogger) WithDecorator ¶
WithDecorator returns a new StdLogger appending the given extra Decorators
type TextConfig ¶ added in v0.2.2
type TextConfig struct { LevelKeyName string MessageKeyName string TimeLayout string LevelFormatter LevelFormatter }
TextConfig is a configuration for TextEncoder
func DefaultTextConfig ¶ added in v0.2.2
func DefaultTextConfig() TextConfig
DefaultTextConfig returns a default TextConfig
type TextEncoder ¶ added in v0.2.2
type TextEncoder struct {
Config TextConfig
}
TextEncoder is an encoder for text
func NewTextEncoder ¶ added in v0.2.2
func NewTextEncoder(cfg TextConfig) TextEncoder
NewTextEncoder returns a TextEncoder
type TickFlusher ¶
TickFlusher is a Flusher triggered by a time.Ticker
func NewTickFlusher ¶
func NewTickFlusher(f Flusher, d time.Duration) *TickFlusher
NewTickFlusher returns a TickFlusher
func (*TickFlusher) Close ¶
func (f *TickFlusher) Close() error
Close stops the time.Ticker and flushes the data
func (*TickFlusher) Flush ¶
func (f *TickFlusher) Flush() error
Flush forces the data in the inner Flusher to be written
type TimestampDecorator ¶ added in v0.2.0
TimestampDecorator is a Decorator which add the log timestamp
func NewTimestampDecorator ¶ added in v0.2.0
func NewTimestampDecorator(name, layout string) TimestampDecorator
NewTimestampDecorator returns a TimestampDecorator with the given field name and layout
func (TimestampDecorator) Decorate ¶ added in v0.2.0
func (td TimestampDecorator) Decorate(e Entry) Entry
Decorate adds the timestamp to the entry
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package test is a generated GoMock package.
|
Package test is a generated GoMock package. |