Documentation ¶
Index ¶
- Variables
- func Debug(format string, arg ...interface{})
- func Error(format string, arg ...interface{})
- func GetLoggerMap() map[string]*logger
- func Info(format string, arg ...interface{})
- func Warn(format string, arg ...interface{})
- type Context
- type FcLoggerMap
- type Format
- type Handler
- func BufferedHandler(bufSize int, 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 LvlFilterHandler(maxLvl Lvl, h Handler) Handler
- func MultiHandler(hs ...Handler) Handler
- func NetHandler(network, addr string, fmtr Format) (Handler, error)
- func RotatingFileHandler(path string, limit uint, formatter Format) (Handler, error)
- func StreamHandler(wr io.Writer, fmtr Format) Handler
- func SyncHandler(h Handler) Handler
- type Lazy
- type Logger
- type Lvl
- type Message
- type Messages
- type Record
Constants ¶
This section is empty.
Variables ¶
var ( StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat()) TerminalHandler = StreamHandler(os.Stdout, TerminalFormat(true)) )
Functions ¶
func Debug ¶
func Debug(format string, arg ...interface{})
Debug is a convenient alias for Root().Debug
func Error ¶
func Error(format string, arg ...interface{})
Error is a convenient alias for Root().Error
func GetLoggerMap ¶
func GetLoggerMap() map[string]*logger
Types ¶
type FcLoggerMap ¶
type FcLoggerMap struct {
// contains filtered or unexported fields
}
type Format ¶
func FormatFunc ¶
func LogfmtFormat ¶
func LogfmtFormat() Format
func TerminalFormat ¶
type Handler ¶
Handler 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 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 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 logs records with the given function.
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/Crit records:
log.LvlFilterHandler(log.LvlError, 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 RotatingFileHandler ¶
RotatingFileHandler returns a handler which writes log records to file chunks at the given path. When a file's size reaches the limit, the handler creates a new file named after the timestamp of the first log record it will contain.
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.
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(name string) 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) // SetEnable enable output without handler. SetEnable(e bool) // Log a message at the given level with context key/value pairs Debug(format string, arg ...interface{}) Info(format string, arg ...interface{}) Warn(format string, arg ...interface{}) Error(format string, arg ...interface{}) }
A Logger writes key/value pairs to a Handler
func NewWithHandle ¶
type Lvl ¶
type Lvl int
func LvlFromString ¶
LvlFromString returns the appropriate Lvl from a string name. Useful for parsing command line args and configuration files.
func (Lvl) AlignedString ¶
AlignedString returns a 5-character string containing the name of a Lvl.
type Message ¶
type Message struct { Format string Args []interface{} // contains filtered or unexported fields }