Documentation ¶
Overview ¶
Package kiwi is a library with an odd logic that log your application' data in its own strange way.
WIP. API and features is subject of changes. Use it carefully!
Features briefly:
simple format with explicit key for each log message (*logfmt* like) - for high readability by humans
JSON format that so liked by machines
ability to define own custom format of the output
there are not nailed levels, not hardcoded fields in the format
output dynamic filtering (change log verbosity on the fly)
can keep context of application
it fast enough and careful about memory allocs
Key feature of `kiwi` logger is dynamic filtering of incoming records. Instead of checking severety level for decide about pass or not the record to the output, `kiwi` passes all records to *all* the outputs (they called *sinks* in `kiwi` terminology). But before actual writing each record checked with a set of filters. Each sink has its own filter set. It takes into account record keys, values, ranges of values. So each sink decides pass the record to a writer or filter it out. Also any pairs in the record may be hidden: so different sinks may display different parts of the same record. Other effect is: any record may be written to any number of outputs.
Index ¶
- Constants
- Variables
- func AsJSON() *formatJSON
- func AsLogfmt() *formatLogfmt
- func FlushAll()
- func Log(kv ...interface{})
- func ResetContext()
- func With(kv ...interface{})
- func Without(keys ...string)
- type Filter
- type Formatter
- type Logger
- func (l *Logger) Add(keyVals ...interface{}) *Logger
- func (l *Logger) Fork() *Logger
- func (l *Logger) Log(keyVals ...interface{})
- func (l *Logger) New() *Logger
- func (l *Logger) Reset() *Logger
- func (l *Logger) ResetContext() *Logger
- func (l *Logger) With(keyVals ...interface{}) *Logger
- func (l *Logger) Without(keys ...string) *Logger
- type Pair
- type Sink
- func (s *Sink) Close()
- func (s *Sink) Float64NotRange(key string, from, to float64) *Sink
- func (s *Sink) Float64Range(key string, from, to float64) *Sink
- func (s *Sink) Flush() *Sink
- func (s *Sink) HasKey(keys ...string) *Sink
- func (s *Sink) HasNotKey(keys ...string) *Sink
- func (s *Sink) HasNotValue(key string, vals ...string) *Sink
- func (s *Sink) HasValue(key string, vals ...string) *Sink
- func (s *Sink) Hide(keys ...string) *Sink
- func (s *Sink) Int64NotRange(key string, from, to int64) *Sink
- func (s *Sink) Int64Range(key string, from, to int64) *Sink
- func (s *Sink) Reset(keys ...string) *Sink
- func (s *Sink) Start() *Sink
- func (s *Sink) Stop() *Sink
- func (s *Sink) TimeNotRange(key string, from, to time.Time) *Sink
- func (s *Sink) TimeRange(key string, from, to time.Time) *Sink
- func (s *Sink) Unhide(keys ...string) *Sink
- func (s *Sink) WithFilter(key string, customFilter Filter) *Sink
- type Stringer
- type Valuer
Constants ¶
const ( // BooleanVal and other types below commonly formatted unquoted. // But it depends on the formatter. BooleanVal = iota IntegerVal FloatVal ComplexVal CustomUnquoted // VoidVal and other types below commonly formatted unquoted. // But it depends on the formatter. StringVal TimeVal CustomQuoted )
Possible kinds of logged values.
Variables ¶
var ( MessageKey = "message" ErrorKey = "kiwi-error" InfoKey = "kiwi-info" )
var FloatFormat byte = 'e'
FloatFormat used in Float to String conversion. It is second parameter passed to strconv.FormatFloat()
var TimeLayout = time.RFC3339
TimeLayout used in time.Time to String conversion.
Functions ¶
func AsJSON ¶
func AsJSON() *formatJSON
AsJSON says that a sink uses JSON (RFC-7159) format for records output.
func AsLogfmt ¶
func AsLogfmt() *formatLogfmt
AsLogfmt says that a sink uses Logfmt format for records output.
func FlushAll ¶
func FlushAll()
FlushAll should wait for all the sinks to be flushed. It does nothing currently. It has left for compatibility with old API.
func Log ¶
func Log(kv ...interface{})
Log is simplified realization of Logger.Log(). You would like use it in short applications where context and initialization of logger could brought extra complexity. If you wish separate contexts and achieve better performance use Logger type instead.
func ResetContext ¶
func ResetContext()
ResetContext resets the global context for the global logger and its descendants. It is safe for concurrency.
Types ¶
type Filter ¶
Filter accepts key and value. If the filter passed it should return true. Custom filters must conform the interface.
type Formatter ¶
type Formatter interface { // Begin function allows to add prefix string for the output // or make some preparations before the output. Begin() // Pair function called for each key-value pair of the record. // ValueType is hint that helps the formatter decide how to output the value. // For example formatter can format string values in quotes but numbers without them. Pair(key, value string, valueType int) // Finish function allows to add suffix string for the output. // Also it returns result string for the displaying of the single record. // It may be multiline if you wish. Result has no restrictions for you imagination :) Finish() []byte }
Formatter represents format of the output.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger keeps context and log record. There are many loggers initialized in different places of application. Loggers are not safe for concurrent usage so then you need logger for another goroutine you will need clone existing instance. See Logger.New() method below for details.
func Fork ¶
func Fork() *Logger
Fork creates a new logger instance that inherited the context from the global logger. Thi fuction is concurrent safe.
func New ¶
func New() *Logger
New creates a new logger instance but not copy context from the global logger. The method is empty now, I keep it for compatibility with older versions of API.
func (*Logger) Add ¶
Add a new key-value pairs to the log record. If a key already added then value will be updated. If a key already exists in a contextSrc then it will be overridden by a new value for a current record only. After flushing a record with Log() old context value will be restored.
func (*Logger) Fork ¶
Fork creates a new instance of the logger. It copies the context from the logger from the parent logger. But the values of the current record of the parent logger discarded.
func (*Logger) Log ¶
func (l *Logger) Log(keyVals ...interface{})
Log is the most common method for flushing previously added key-val pairs to an output. After current record is flushed all pairs removed from a record except contextSrc pairs.
func (*Logger) New ¶
New creates a new instance of the logger. It not inherited the context of the parent logger. The method is empty now, I keep it for compatibility with older versions of API.
func (*Logger) ResetContext ¶
ResetContext resets the context of the logger. The function is not concurrent safe.
type Pair ¶
Pair is key and value together. They can be used by custom helpers for example for logging timestamps or something.
type Sink ¶
Sink used for filtering incoming log records from all logger instances and decides how to filter them. Each output wraps its own io.Writer. Sink methods are safe for concurrent usage.
func SinkTo ¶
SinkTo creates a new sink for an arbitrary number of loggers. There are any number of sinks may be created for saving incoming log records to different places. The sink requires explicit start with Start() before usage. That allows firstly setup filters before sink will really accept any records.
func (*Sink) Close ¶
func (s *Sink) Close()
Close closes the sink. It flushes records for the sink before closing.
func (*Sink) Float64NotRange ¶ added in v0.3.0
Float64NotRange sets restriction for records output.
func (*Sink) Float64Range ¶ added in v0.3.0
Float64Range sets restriction for records output.
func (*Sink) Flush ¶
Flush waits that all previously sent to the output records worked. It does nothing currently. It has left for compatibility with old API.
func (*Sink) HasKey ¶ added in v0.3.0
HasKey sets restriction for records output. Only the records WITH any of the keys will be passed to output.
func (*Sink) HasNotKey ¶ added in v0.3.0
HasNotKey sets restriction for records output. Only the records WITHOUT any of the keys will be passed to output.
func (*Sink) HasNotValue ¶ added in v0.3.0
HasNotValue sets restriction for records output.
func (*Sink) HasValue ¶ added in v0.3.0
HasValue sets restriction for records output. A record passed to output if the key equal one of any of the listed values.
func (*Sink) Hide ¶
Hide keys from the output. Other keys in record will be displayed but not hidden keys.
func (*Sink) Int64NotRange ¶ added in v0.3.0
Int64NotRange sets restriction for records output.
func (*Sink) Int64Range ¶ added in v0.3.0
Int64Range sets restriction for records output.
func (*Sink) Reset ¶
Reset all filters for the keys for the output. If no one key provided it do global reset for all filters of the sink.
func (*Sink) Start ¶
Start writing to the output. After creation of a new sink it will paused and you need explicitly start it. It allows setup the filters before the sink will accepts any records.
func (*Sink) TimeNotRange ¶ added in v0.3.0
TimeNotRange sets restriction for records output.
func (*Sink) WithFilter ¶
WithFilter setup custom filtering function for values for a specific key. Custom filter should realize Filter interface. All custom filters treated as positive filters. So if the filter returns true then it will be passed.