Documentation ¶
Overview ¶
Package llog is a generic logging library used by leven labs. The log methods come in different severities: Debug, Info, Warn, Error, and Fatal.
The log methods take in a string describing the error, and a set of key/value pairs giving the specific context around the error. The string is intended to always be the same no matter what, while the key/value pairs give information like which userID the error happened to, or any other relevant dynamic information.
By default logs will be output to Stdout, without a timestamp attached to them, and only showing entries of level Info or above. All of these can be configured.
All public functions in this package are thread-safe and can be called at any time. The public variables in this package are NOT thread-safe and should only be modified before any logging takes place
Examples:
Info("Something important has occurred") Error("Could not open file", llog.KV{"filename": filename}, llog.ErrKV(err))
Index ¶
- Variables
- func CtxWithKV(ctx context.Context, kvs ...KV) context.Context
- func Debug(msg string, kv ...KV)
- func ErrWithKV(err error, kvs ...KV) error
- func Error(msg string, kv ...KV)
- func Fatal(msg string, kv ...KV)
- func Flush()
- func Info(msg string, kv ...KV)
- func NewLogger(lvl Level, kv KV, filters ...func(string) (string, error)) *log.Logger
- func NewWriter(lvl Level, kv KV, filters ...func(string) (string, error)) io.Writer
- func SetLevel(l Level)
- func SetLevelFromString(ls string) error
- func Truncate(s string, size int) string
- func Warn(msg string, kv ...KV)
- type KV
- type Level
- type LogFunc
Constants ¶
This section is empty.
Variables ¶
var BlockByDefault = false
BlockByDefault controls whether the non-Fatal functions wait for the write to Out to complete. This can be useful to set to true for tests so that logging doesn't end up mangling test output.
var DisplayTimestamp bool
DisplayTimestamp determines whether or not a timestamp is displayed in the log messages. By default one is not displayed. This can be changed by it should only be changed before any logging occurs
var Out io.Writer = os.Stdout
Out is the io.Writer all log entries will be written to. It can be changed to anything you like, but the change should happen before any logging occurs. If an error occurs while writing to Out the entry will be written to Stdout instead
Functions ¶
func CtxWithKV ¶
CtxWithKV embeds a KV into a Context, returning a new Context instance. If the Context already has a KV embedded in it then the returned context's KV will be the merging of the two.
func Debug ¶
Debug writes a Debug message to Out, with an optional set of key/value pairs which will be Merge'd together.
func ErrWithKV ¶
ErrWithKV embeds the merging of a set of KVs into an error and Marks the function for convenience, returning a new error instance. If the error already has a KV embedded in it then the returned error will have the merging of them all.
func Error ¶
Error writes an Error message to Out, with an optional set of key/value pairs which will be Merge'd together.
func Fatal ¶
Fatal writes a Fatal message to Out, with an optional set of key/value pairs which will be Merge'd together. Once written the process will be exited with an exit code of 1
func Flush ¶
func Flush()
Flush will attempts to flush any buffered data in Out. Will block until the flushing has been completed
func Info ¶
Info writes an Info message to Out, with an optional set of key/value pairs which will be Merge'd together.
func NewLogger ¶
NewLogger returns an instance of log.Logger that uses llog to log the messages under the sent level with the passed KV. Multiple filter functions can be passed. If an error is returned from the filter function, it's sent to the caller of the Write method. If an empty string is returned then the message is ignored.
func NewWriter ¶
NewWriter returns an io.Writer that uses llog to log the sent writes with the sent log level. Multiple filter functions can be passed. If an error is returned from the filter function, it's sent to the caller of the Write method. If an empty string is returned then the message is ignored.
func SetLevel ¶
func SetLevel(l Level)
SetLevel sets the current minimum log level which will be written to Out
func SetLevelFromString ¶
SetLevelFromString attempts to interpret the given string as a log level and sets the current log level to that. If the string can't be interpreted an error is returned and the log level remains what it was
Types ¶
type KV ¶
type KV map[string]interface{}
KV is used to provide context to a log entry in the form of a dynamic set of key/value pairs which can be different for every entry.
func ErrKV ¶
ErrKV returns a copy of the KV embedded in the error by ErrWithKV as well as any line from errctx.Mark as the key "source" if "source" wasn't already set. Returns empty KV if no KV was previously embedded and no line was marked. Will automatically set the "err" field on the returned KV as well.
func Merge ¶
Merge takes in multiple KVs and returns a single KV which is the union of all the passed in ones. Key/vals on the rightmost of the set take precedence over conflicting ones to the left. This function will never return nil
func (KV) Set ¶
Set returns a copy of the KV being called on with the given key/val set on it. The original KV is unaffected
func (KV) StringSlice ¶
StringSlice converts the KV into a slice of [2]string entries (first index is the key, second is the string form of the value).