Documentation
¶
Overview ¶
Package ligno is async structured logging library for golang.
Two main features that separate ligno from other logging libraries are that all messages are logged asynchronously, which means that all expensive operations related to logging (like writing to file or sending log message over network) will not block execution of main application.
Other main feature is format of log record. Log record in ligno is just a map holding key-value pairs. That's it. Because of this, records can be formatted to any structured format (JSON, YAML, key=value) or to traditional formats that we are used to seeing in log files.
Index ¶
- Constants
- Variables
- func Critical(event string, pairs ...interface{})
- func CriticalCtx(message string, ctx Ctx)
- func Debug(event string, pairs ...interface{})
- func DebugCtx(message string, ctx Ctx)
- func Error(event string, pairs ...interface{})
- func ErrorCtx(message string, ctx Ctx)
- func Fatal(v ...interface{})
- func Fatalf(format string, v ...interface{})
- func Fatalln(v ...interface{})
- func Info(event string, pairs ...interface{})
- func InfoCtx(message string, ctx Ctx)
- func Log(level Level, event string, pairs ...interface{})
- func LogCtx(level Level, message string, ctx Ctx)
- func Panic(v ...interface{})
- func Panicf(format string, v ...interface{})
- func Panicln(v ...interface{})
- func Print(v ...interface{})
- func Printf(format string, v ...interface{})
- func Println(v ...interface{})
- func SetHandler(handler Handler)
- func WaitAll()
- func WaitAllTimeout(t time.Duration) bool
- func Warning(event string, pairs ...interface{})
- func WarningCtx(message string, ctx Ctx)
- type Ctx
- type Formatter
- type FormatterFunc
- type Handler
- func CombiningHandler(handlers ...Handler) Handler
- func FileHandler(fileName string, formatter Formatter) Handler
- func FilterHandler(predicate Predicate, handler Handler) Handler
- func FilterLevelHandler(level Level, handler Handler) Handler
- func NullHandler() Handler
- func StreamHandler(out io.Writer, formatter Formatter) Handler
- func SyslogHandler(formatter Formatter, tag string, priority syslog.Priority) Handler
- type HandlerCloser
- type HandlerFunc
- type InspectHandler
- type Level
- type Logger
- func (l *Logger) Critical(message string, pairs ...interface{})
- func (l *Logger) CriticalCtx(message string, ctx Ctx)
- func (l *Logger) Debug(message string, pairs ...interface{})
- func (l *Logger) DebugCtx(message string, ctx Ctx)
- func (l *Logger) Error(message string, pairs ...interface{})
- func (l *Logger) ErrorCtx(message string, ctx Ctx)
- func (l *Logger) Fatal(v ...interface{})
- func (l *Logger) Fatalf(format string, v ...interface{})
- func (l *Logger) Fatalln(v ...interface{})
- func (l *Logger) FullName() string
- func (l *Logger) Handler() Handler
- func (l *Logger) Info(message string, pairs ...interface{})
- func (l *Logger) InfoCtx(message string, ctx Ctx)
- func (l *Logger) IsCritical() bool
- func (l *Logger) IsDebug() bool
- func (l *Logger) IsEnabledFor(level Level) bool
- func (l *Logger) IsError() bool
- func (l *Logger) IsInfo() bool
- func (l *Logger) IsLevel(level Level) bool
- func (l *Logger) IsRunning() bool
- func (l *Logger) IsWarning() bool
- func (l *Logger) Level() Level
- func (l *Logger) Log(calldepth int, level Level, message string, pairs ...interface{})
- func (l *Logger) LogCtx(calldepth int, level Level, message string, data Ctx)
- func (l *Logger) Name() string
- func (l *Logger) Panic(v ...interface{})
- func (l *Logger) Panicf(format string, v ...interface{})
- func (l *Logger) Panicln(v ...interface{})
- func (l *Logger) Print(v ...interface{})
- func (l *Logger) Printf(format string, v ...interface{})
- func (l *Logger) Println(v ...interface{})
- func (l *Logger) SetHandler(handler Handler)
- func (l *Logger) StopAndWait()
- func (l *Logger) StopAndWaitTimeout(t time.Duration) (finished bool)
- func (l *Logger) SubLogger(name string) *Logger
- func (l *Logger) SubLoggerOptions(name string, options LoggerOptions) *Logger
- func (l *Logger) Wait()
- func (l *Logger) WaitTimeout(t time.Duration) (finished bool)
- func (l *Logger) Warning(message string, pairs ...interface{})
- func (l *Logger) WarningCtx(message string, ctx Ctx)
- func (l *Logger) Write(p []byte) (n int, err error)
- type LoggerOptions
- type Predicate
- type Record
- type Theme
Constants ¶
const ( NOTSET Level = iota DEBUG = iota * 10 INFO = iota * 10 WARNING = iota * 10 ERROR = iota * 10 CRITICAL = iota * 10 )
Levels of log records. Additional can be created, these are just defaults offered by library.
const DefaultTimeFormat = "2006-01-02 15:05:06.0000"
DefaultTimeFormat is default time format.
Variables ¶
var ( // DefaultTheme defines theme used by default. DefaultTheme = &theme{ timeColor: color.New(color.FgWhite, color.Faint).SprintfFunc(), debugColor: color.New(color.FgWhite).SprintfFunc(), infoColor: color.New(color.FgHiWhite).SprintfFunc(), warningColor: color.New(color.FgYellow).SprintfFunc(), errorColor: color.New(color.FgHiRed).SprintfFunc(), criticalColor: color.New(color.BgRed, color.FgHiWhite).SprintfFunc(), } // NoColorTheme defines theme that does not color any output. NoColorTheme = &theme{ timeColor: fmt.Sprintf, debugColor: fmt.Sprintf, infoColor: fmt.Sprintf, warningColor: fmt.Sprintf, errorColor: fmt.Sprintf, criticalColor: fmt.Sprintf, } )
Functions ¶
func Critical ¶
func Critical(event string, pairs ...interface{})
Critical creates log record and queues it for processing with CRITICAL level. Additional parameters have same semantics as in Log method.
func CriticalCtx ¶
CriticalCtx logs message in CRITICAL level with provided context.
func Debug ¶
func Debug(event string, pairs ...interface{})
Debug creates log record and queues it for processing with DEBUG level. Additional parameters have same semantics as in Log method.
func Error ¶
func Error(event string, pairs ...interface{})
Error creates log record and queues it for processing with ERROR level. Additional parameters have same semantics as in Log method.
func Fatal ¶
func Fatal(v ...interface{})
Fatal formats message according to stdlib rules, logs it in CRITICAL level and exists application.
func Fatalf ¶
func Fatalf(format string, v ...interface{})
Fatalf formats message according to stdlib rules, logs it in CRITICAL level and exists application.
func Fatalln ¶
func Fatalln(v ...interface{})
Fatalln formats message according to stdlib rules, logs it in CRITICAL level and exists application.
func Info ¶
func Info(event string, pairs ...interface{})
Info creates log record and queues it for processing with INFO level. Additional parameters have same semantics as in Log method.
func Log ¶
Log creates record and queues it for processing. Required parameters are level for record and event that occurred. Any additional parameters will be transformed to key-value pairs for record in order in which they were provided. There should be even number of them, but in case that there is on number of parameters, empty string is appended. Example:
defaultLog.Log(INFO, "User logged in", "user_id", user_id, "platform", PLATFORM_NAME)
will be translated into log record with following keys:
{LEVEL: INFO", EVENT: "User logged in", "user_id": user_id, "platform": PLATFORM_NAME}
func Panic ¶
func Panic(v ...interface{})
Panic formats message according to stdlib rules, logs it in CRITICAL level and panics.
func Panicf ¶
func Panicf(format string, v ...interface{})
Panicf formats message according to stdlib rules, logs it in CRITICAL level and panics.
func Panicln ¶
func Panicln(v ...interface{})
Panicln formats message according to stdlib rules, logs it in CRITICAL level and panics.
func Print ¶
func Print(v ...interface{})
Print formats message according to stdlib rules and logs it in INFO level.
func Printf ¶
func Printf(format string, v ...interface{})
Printf formats message according to stdlib rules and logs it in INFO level.
func Println ¶
func Println(v ...interface{})
Println formats message according to stdlib rules and logs it in INFO level.
func WaitAll ¶
func WaitAll()
WaitAll blocks until all loggers are finished with message processing.
func WaitAllTimeout ¶
WaitAllTimeout blocks until all messages send to all loggers are processed or max specified amount of time. Boolean return value indicates if function returned because all messages were processed (true) or because timeout has expired (false).
func Warning ¶
func Warning(event string, pairs ...interface{})
Warning creates log record and queues it for processing with WARNING level. Additional parameters have same semantics as in Log method.
func WarningCtx ¶
WarningCtx logs message in WARNING level with provided context.
Types ¶
type Formatter ¶
Formatter is interface for converting log record to string representation.
func JSONFormat ¶
JSONFormat is simple formatter that only marshals log record to json.
func SimpleFormat ¶
func SimpleFormat() Formatter
SimpleFormat returns formatter that formats record with bare minimum of information. Intention of this formatter is to simulate standard library formatter.
func TerminalFormat ¶
func TerminalFormat() Formatter
TerminalFormat returns ThemeTerminalFormat with default theme set.
func ThemedTerminalFormat ¶
ThemedTerminalFormat returns formatter that produces records formatted for easy reading in terminal, but that are a bit richer then SimpleFormat (this one includes context keys)
type FormatterFunc ¶
FormatterFunc is function type that implements Formatter interface.
func (FormatterFunc) Format ¶
func (ff FormatterFunc) Format(record Record) []byte
Format is implementation of Formatter interface. It just calls function.
type Handler ¶
Handler processes log records and writes them to appropriate destination.
func CombiningHandler ¶
CombiningHandler creates and returns handler that passes records to all provided handlers.
func FileHandler ¶
FileHandler writes log records to file with provided name.
func FilterHandler ¶
FilterHandler checks records if by using predicate to check if they should be processed and only if they do, record is passed to provided handler.
func FilterLevelHandler ¶
FilterLevelHandler is FilterHandler with default predicate function that filters all records below provided level.
func NullHandler ¶
func NullHandler() Handler
NullHandler returns handler that discards all records.
func StreamHandler ¶
StreamHandler writes records to provided io.Writer
type HandlerCloser ¶
type HandlerCloser interface {
Close()
}
HandlerCloser is interface that allows handlers to be closed. If handler implements this interface, when logger is stopped, Close will be called.
type HandlerFunc ¶
HandlerFunc is function that implements Handler interface.
func (HandlerFunc) Handle ¶
func (hf HandlerFunc) Handle(record Record) error
Handle just calls HandlerFunc.
type InspectHandler ¶
InspectHandler is handler that is able to restore logged message and return them for inspection.
func MemoryHandler ¶
func MemoryHandler(formatter Formatter) InspectHandler
MemoryHandler returns handler instance that saves all message to memory.
type Level ¶
type Level uint
Level represents rank of message importance. Log records can contain level and filters can decide not to process records based on this level.
func AddLevel ¶
AddLevel add new level to system with provided name and rank. It is forbidden to register levels that already exist.
func (Level) MarshalJSON ¶
MarshalJSON returns levels JSON representation (implementation of json.Marshaler)
func (*Level) UnmarshalJSON ¶
UnmarshalJSON recreates level from JSON representation (implementation of json.Unmarshaler)
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is central data type in ligno which represents logger itself. Logger is first level of processing events. It creates them and queues for async processing. It holds slice of Handlers that process messages and context (set of key-value pairs that will be include in every log record).
func GetLogger ¶
GetLogger returns logger with provided name (creating it if needed). Name is dot-separated string with parent logger names and this function will create all intermediate loggers with default options.
func GetLoggerOptions ¶
func GetLoggerOptions(name string, options LoggerOptions) *Logger
GetLoggerOptions returns logger with provided name (creating it if needed). Name is dot-separated string with parent loggers and this function will create all intermediate loggers with default options. Provided options will be applied only to last logger in chain. If all loggers in chain already exist, no new loggers will be creates and provided options will be discarded. If options different then default are needed for intermediate loggers, create them first with appropriate options.
func (*Logger) Critical ¶
Critical creates log record and queues it for processing with CRITICAL level. Additional parameters have same semantics as in Log method.
func (*Logger) CriticalCtx ¶
CriticalCtx logs message in CRITICAL level with provided context.
func (*Logger) Debug ¶
Debug creates log record and queues it for processing with DEBUG level. Additional parameters have same semantics as in Log method.
func (*Logger) Error ¶
Error creates log record and queues it for processing with ERROR level. Additional parameters have same semantics as in Log method.
func (*Logger) Fatal ¶
func (l *Logger) Fatal(v ...interface{})
Fatal formats message according to stdlib rules, logs it in CRITICAL level and exists application.
func (*Logger) Fatalf ¶
Fatalf formats message according to stdlib rules, logs it in CRITICAL level and exists application.
func (*Logger) Fatalln ¶
func (l *Logger) Fatalln(v ...interface{})
Fatalln formats message according to stdlib rules, logs it in CRITICAL level and exists application.
func (*Logger) FullName ¶
FullName returns name of this logger prefixed with name of its parent, separated by ".". This happens recursively, so return value will contain names of all parents.
func (*Logger) Info ¶
Info creates log record and queues it for processing with INFO level. Additional parameters have same semantics as in Log method.
func (*Logger) IsCritical ¶
IsCritical returns true if logger will process messages in Critical level
func (*Logger) IsEnabledFor ¶
IsEnabledFor returns true if logger will process records with provided level.
func (*Logger) Log ¶
Log creates record and queues it for processing. Required parameters are level for record and event that occurred. Any additional parameters will be transformed to key-value pairs for record in order in which they were provided. There should be even number of them, but in case that there is on number of parameters, empty string is appended. Example:
l.Log(INFO, "User logged in", "user_id", user_id, "platform", PLATFORM_NAME)
will be translated into log record with following keys:
{LEVEL: INFO", EVENT: "User logged in", "user_id": user_id, "platform": PLATFORM_NAME}
func (*Logger) Panic ¶
func (l *Logger) Panic(v ...interface{})
Panic formats message according to stdlib rules, logs it in CRITICAL level and panics.
func (*Logger) Panicf ¶
Panicf formats message according to stdlib rules, logs it in CRITICAL level and panics.
func (*Logger) Panicln ¶
func (l *Logger) Panicln(v ...interface{})
Panicln formats message according to stdlib rules, logs it in CRITICAL level and panics.
func (*Logger) Print ¶
func (l *Logger) Print(v ...interface{})
Print formats message according to stdlib rules and logs it in INFO level.
func (*Logger) Println ¶
func (l *Logger) Println(v ...interface{})
Println formats message according to stdlib rules and logs it in INFO level.
func (*Logger) SetHandler ¶
SetHandler set handler to this logger to be used from now on.
func (*Logger) StopAndWait ¶
func (l *Logger) StopAndWait()
StopAndWait stops listening for new messages sent to this logger and blocks until all previously arrived messages are processed. Records already sent will be processed, but all new messages will silently be dropped.
func (*Logger) StopAndWaitTimeout ¶
StopAndWaitTimeout stops listening for new messages sent to this logger and blocks until all previously sent message are processed or max provided duration. Records already sent will be processed, but all new messages will silently be dropped. Return value indicates if all messages are processed (true) or if provided timeout expired (false)
func (*Logger) SubLogger ¶
SubLogger creates new logger that has current logger as parent with default options and starts it so it is ready for message processing.
func (*Logger) SubLoggerOptions ¶
func (l *Logger) SubLoggerOptions(name string, options LoggerOptions) *Logger
SubLoggerOptions creates new logger that has current logger as parent with provided options and starts it so it is ready for message processing.
func (*Logger) Wait ¶
func (l *Logger) Wait()
Wait block until all messages sent to logger are processed. If timeout is needed, see WaitTimeout.
func (*Logger) WaitTimeout ¶
WaitTimeout blocks until all messages send to logger are processed or max specified amount of time. Boolean return value indicates if function returned because all messages were processed (true) or because timeout has expired (false).
func (*Logger) Warning ¶
Warning creates log record and queues it for processing with WARNING level. Additional parameters have same semantics as in Log method.
func (*Logger) WarningCtx ¶
WarningCtx logs message in WARNING level with provided context.
type LoggerOptions ¶
type LoggerOptions struct { // Context that logger should have. Context Ctx // Handler for processing records. Handler Handler // Level is minimal level that logger will process. Level Level // BufferSize is size of buffer for records that will be process async. BufferSize int // PreventPropagation is flag that indicates if records should be passed // to parent logger for processing. PreventPropagation bool // Flag that indicates that file and line of place where logging took place // should be kept. Note that this is expensive, so use with care. If this // information will be shown depends on formatter. IncludeFileAndLine bool }
LoggerOptions is container for configuration options for logger instances. Empty value is valid for initializing logger.
type Predicate ¶
Predicate is function that returns true if record should be logged, false otherwise.
type Record ¶
type Record struct { Time time.Time `json:"time"` Level Level `json:"level"` Message string `json:"message"` Context Ctx `json:"context"` Logger *Logger `json:"-"` File string `json:"file"` Line int `json:"line"` }
Record holds information about one log message.
type Theme ¶
type Theme interface { Time(msg string, args ...interface{}) string Debug(msg string, args ...interface{}) string Info(msg string, args ...interface{}) string Warning(msg string, args ...interface{}) string Error(msg string, args ...interface{}) string Critical(msg string, args ...interface{}) string ForLevel(level Level) func(msg string, args ...interface{}) string }
Theme is definition of interface needed for colorizing log message to console.