Documentation ¶
Index ¶
- Variables
- func Crit(msg string, ctx ...interface{})
- func Debug(msg string, ctx ...interface{})
- func Error(msg string, ctx ...interface{})
- func Info(msg string, ctx ...interface{})
- func Trace(msg string, ctx ...interface{})
- func Warn(msg string, ctx ...interface{})
- type Call
- type CallStack
- type Ctx
- type Format
- type Handler
- func BoundLvlFilterHandler(maxLvl Lvl, minLvl Lvl, h Handler) Handler
- func BufferedHandler(bufSize int, h Handler) Handler
- func CallerFileHandler(h Handler) Handler
- func CallerFuncHandler(h Handler) Handler
- func CallerStackHandler(format string, h Handler) Handler
- func ChannelHandler(recs chan<- *Record) Handler
- func DiscardHandler() Handler
- func FailoverHandler(hs ...Handler) Handler
- func FileHandler(path string, fmtr Format) (Handler, error)
- func FilterHandler(fn func(r *Record) bool, h Handler) Handler
- func FuncHandler(fn func(r *Record) error) Handler
- func LazyHandler(h Handler) Handler
- func LvlFilterHandler(maxLvl Lvl, h Handler) Handler
- func MatchFilterHandler(key string, value interface{}, h Handler) Handler
- func MultiHandler(hs ...Handler) Handler
- func NetHandler(network, addr string, fmtr Format) (Handler, error)
- func RotateFileHandler(path string, fmtr Format, interval int, backupCount int) (Handler, error)
- func StreamHandler(wr io.Writer, fmtr Format) Handler
- func SyncHandler(h Handler) Handler
- func SyslogHandler(priority syslog.Priority, tag string, fmtr Format) (Handler, error)
- func SyslogNetHandler(net, addr string, priority syslog.Priority, tag string, fmtr Format) (Handler, error)
- type Lazy
- type Logger
- type Lvl
- type Record
- type RecordKeyNames
- type TimeRotateWriter
Constants ¶
This section is empty.
Variables ¶
var ( StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat()) StderrHandler = StreamHandler(os.Stderr, LogfmtFormat()) )
Predefined handlers
var ErrNoFunc = errors.New("no call stack information")
ErrNoFunc means that the Call has a nil *runtime.Func. The most likely cause is a Call with the zero value.
var Must muster
Must object provides the following Handler creation functions which instead of returning an error parameter only return a Handler and panic on failure: FileHandler, NetHandler, SyslogHandler, SyslogNetHandler
Functions ¶
func Debug ¶
func Debug(msg string, ctx ...interface{})
Debug is a convenient alias for Root().Debug
func Error ¶
func Error(msg string, ctx ...interface{})
Error is a convenient alias for Root().Error
Types ¶
type Call ¶
type Call struct {
// contains filtered or unexported fields
}
Call records a single function invocation from a goroutine stack.
func Caller ¶
Caller returns a Call from the stack of the current goroutine. The argument skip is the number of stack frames to ascend, with 0 identifying the calling function.
func (Call) Format ¶
Format implements fmt.Formatter with support for the following verbs.
%s source file %d line number %n function name %k last segment of the package path %v equivalent to %s:%d
It accepts the '+' and '#' flags for most of the verbs as follows.
%+s path of source file relative to the compile time GOPATH, or the module path joined to the path of source file relative to module root %#s full path of source file %+n import path qualified function name %+k full package path %+v equivalent to %+s:%d %#v equivalent to %#s:%d
func (Call) MarshalText ¶
MarshalText implements encoding.TextMarshaler. It formats the Call the same as fmt.Sprintf("%v", c).
type CallStack ¶
type CallStack []Call
CallStack records a sequence of function invocations from a goroutine stack.
func StackTrace ¶
func StackTrace() CallStack
Trace returns a CallStack for the current goroutine with element 0 identifying the calling function.
func (CallStack) Format ¶
Format implements fmt.Formatter by printing the CallStack as square brackets ([, ]) surrounding a space separated list of Calls each formatted with the supplied verb and options.
func (CallStack) MarshalText ¶
MarshalText implements encoding.TextMarshaler. It formats the CallStack the same as fmt.Sprintf("%v", cs).
func (CallStack) TrimAbove ¶
TrimAbove returns a slice of the CallStack with all entries above c removed.
func (CallStack) TrimBelow ¶
TrimBelow returns a slice of the CallStack with all entries below c removed.
func (CallStack) TrimRuntime ¶
TrimRuntime returns a slice of the CallStack with the topmost entries from the go runtime removed. It considers any calls originating from unknown files, files under GOROOT, or _testmain.go as part of the runtime.
type Ctx ¶
type Ctx map[string]interface{}
Ctx is a map of key/value pairs to pass as context to a log function Use this only if you really need greater safety around the arguments you pass to the logging functions.
type Format ¶
Format is the interface implemented by StreamHandler formatters.
func FormatFunc ¶
FormatFunc returns a new Format object which uses the given function to perform record formatting.
func JsonFormat ¶
func JsonFormat() Format
JsonFormat formats log records as JSON objects separated by newlines. It is the equivalent of JsonFormatEx(false, true).
func JsonFormatEx ¶
JsonFormatEx formats log records as JSON objects. If pretty is true, records will be pretty-printed. If lineSeparated is true, records will be logged with a new line between each record.
func LogfmtFormat ¶
func LogfmtFormat() Format
LogfmtFormat prints records in logfmt format, an easy machine-parseable but human-readable format for key/value pairs.
For more details see: http://godoc.org/github.com/kr/logfmt
func TerminalFormat ¶
func TerminalFormat() Format
TerminalFormat formats log records optimized for human readability on a terminal with color-coded level output and terser human friendly timestamp. This format should only be used for interactive programs or while developing.
[TIME] [LEVEL] MESSAGE key=value key=value ...
Example:
[May 16 20:58:45] [DEBUG] remove route ns=haproxy addr=127.0.0.1:50002
type Handler ¶
Handler interface defines where and how log records are written. A logger prints its log records by writing to a Handler. Handlers are composable, providing you great flexibility in combining them to achieve the logging structure that suits your applications.
func BoundLvlFilterHandler ¶
BoundLvlFilterHandler returns a Handler that only writes records which are less than the given `maxLvl` verbosity level and greater than `minLvl` level to the wrapped Handler. For example, to only log Trace/Info records:
log.BoundLvlFilterHandler(log.LvlTrace, log.LvlInfo, log.StdoutHandler)
func BufferedHandler ¶
BufferedHandler writes all records to a buffered channel of the given size which flushes into the wrapped handler whenever it is available for writing. Since these writes happen asynchronously, all writes to a BufferedHandler never return an error and any errors from the wrapped handler are ignored.
func CallerFileHandler ¶
CallerFileHandler returns a Handler that adds the line number and file of the calling function to the context with key "caller".
func CallerFuncHandler ¶
CallerFuncHandler returns a Handler that adds the calling function name to the context with key "fn".
func CallerStackHandler ¶
CallerStackHandler returns a Handler that adds a stack trace to the context with key "stack". The stack trace is formated as a space separated list of call sites inside matching []'s. The most recent call site is listed first. Each call site is formatted according to format. See the documentation of package github.com/wooyang2018/corechain/logger/mlog for the list of supported formats.
func ChannelHandler ¶
ChannelHandler writes all records to the given channel. It blocks if the channel is full. Useful for async processing of log messages, it's used by BufferedHandler.
func DiscardHandler ¶
func DiscardHandler() Handler
DiscardHandler reports success for all writes but does nothing. It is useful for dynamically disabling logging at runtime via a Logger's SetHandler method.
func FailoverHandler ¶
FailoverHandler writes all log records to the first handler specified, but will failover and write to the second handler if the first handler has failed, and so on for all handlers specified. For example you might want to log to a network socket, but failover to writing to a file if the network fails, and then to standard out if the file write fails:
log.FailoverHandler( log.Must.NetHandler("tcp", ":9090", log.JsonFormat()), log.Must.FileHandler("/var/log/app.log", log.LogfmtFormat()), log.StdoutHandler)
All writes that do not go to the first handler will add context with keys of the form "failover_err_{idx}" which explain the error encountered while trying to write to the handlers before them in the list.
func FileHandler ¶
FileHandler returns a handler which writes log records to the give file using the given format. If the path already exists, FileHandler will append to the given file. If it does not, FileHandler will create the file with mode 0644.
func FilterHandler ¶
FilterHandler returns a Handler that only writes records to the wrapped Handler if the given function evaluates true. For example, to only log records where the 'err' key is not nil:
logger.SetHandler(FilterHandler(func(r *Record) bool { for i := 0; i < len(r.Ctx); i += 2 { if r.Ctx[i] == "err" { return r.Ctx[i+1] != nil } } return false }, h))
func FuncHandler ¶
FuncHandler returns a Handler that logger records with the given function.
func LazyHandler ¶
LazyHandler writes all values to the wrapped handler after evaluating any lazy functions in the record's context. It is already wrapped around StreamHandler and SyslogHandler in this library, you'll only need it if you write your own Handler.
func LvlFilterHandler ¶
LvlFilterHandler returns a Handler that only writes records which are less than the given verbosity level to the wrapped Handler. For example, to only log Error/Fatal records:
log.LvlFilterHandler(log.LvlError, log.StdoutHandler)
func MatchFilterHandler ¶
MatchFilterHandler returns a Handler that only writes records to the wrapped Handler if the given key in the logged context matches the value. For example, to only log records from your ui package:
log.MatchFilterHandler("pkg", "app/ui", log.StdoutHandler)
func MultiHandler ¶
MultiHandler dispatches any write to each of its handlers. This is useful for writing different types of log information to different locations. For example, to log to a file and standard error:
log.MultiHandler( log.Must.FileHandler("/var/log/app.log", log.LogfmtFormat()), log.StderrHandler)
func NetHandler ¶
NetHandler opens a socket to the given address and writes records over the connection.
func RotateFileHandler ¶
RotateFileHandler returns a handler which writes log records to the give file using the given format. If the path already exists, RotateFileHandler will append to the given file. If it does not, RotateFileHandler will create the file with mode 0644.
`interval`: the log rotation interval in minutes. `backupCount`: keep at most `backupCount` old log files.
func StreamHandler ¶
StreamHandler writes log records to an io.Writer with the given format. StreamHandler can be used to easily begin writing log records to other outputs.
StreamHandler wraps itself with LazyHandler and SyncHandler to evaluate Lazy objects and perform safe concurrent writes.
func SyncHandler ¶
SyncHandler can be wrapped around a handler to guarantee that only a single Log operation can proceed at a time. It's necessary for thread-safe concurrent writes.
func SyslogHandler ¶
SyslogHandler opens a connection to the system syslog daemon by calling syslog.New and writes all records to it.
type Lazy ¶
type Lazy struct {
Fn interface{}
}
Lazy allows you to defer calculation of a logged value that is expensive to compute until it is certain that it must be evaluated with the given filters.
Lazy may also be used in conjunction with a Logger's New() function to generate a child logger which always reports the current value of changing state.
You may wrap any function which takes no arguments to Lazy. It may return any number of values of any type.
type Logger ¶
type Logger interface { // New returns a new Logger that has this logger's context plus the given context New(ctx ...interface{}) Logger // GetHandler gets the handler associated with the logger. GetHandler() Handler // SetHandler updates the logger to write records to the specified handler. SetHandler(h Handler) // Set minimal level to print SetLevelLimit(minLvl Lvl) // Log a message at the given level with context key/value pairs Debug(msg string, ctx ...interface{}) Trace(msg string, ctx ...interface{}) Info(msg string, ctx ...interface{}) Warn(msg string, ctx ...interface{}) Error(msg string, ctx ...interface{}) Fatal(msg string, ctx ...interface{}) }
A Logger writes key/value pairs to a Handler
type Lvl ¶
type Lvl int
Lvl is a type for predefined log levels.
List of predefined log Levels
func LvlFromString ¶
LvlFromString returns the appropriate Lvl from a string name. Useful for parsing command line args and configuration files.
type Record ¶
type Record struct { Time time.Time Lvl Lvl Msg string Ctx []interface{} Call Call KeyNames RecordKeyNames }
A Record is what a Logger asks its handler to write
type RecordKeyNames ¶
RecordKeyNames are the predefined names of the log props used by the Logger interface.
type TimeRotateWriter ¶
type TimeRotateWriter struct {
// contains filtered or unexported fields
}
func NewTimeRotateWriter ¶
func NewTimeRotateWriter(filename string, interval int, backupCount int) (*TimeRotateWriter, error)
func (*TimeRotateWriter) Close ¶
func (wr *TimeRotateWriter) Close() (err error)
Close of WriterCloser