Documentation ¶
Overview ¶
Package mlog provides a purposefully basic logging library for Go.
mlog only has 3 logging levels: debug, info, and fatal.
Each logging level has 3 logging methods. As an example, the following methods log at the "info" level: Info, Infof, Infom. There are similar methods for the fatal and debug levels.
Example usage:
import ( "bytes" "github.com/cactus/mlog" ) func main() { mlog.Infom("this is a log", mlog.Map{ "interesting": "data", "something": 42, }) mlog.Debugm("this won't print") // set flags for the default logger // alternatively, you can create your own logger // and supply flags at creation time mlog.SetFlags(mlog.Ldebug) mlog.Debugm("this will print!") mlog.Debugm("can it print?", mlog.Map{ "how_fancy": []byte{'v', 'e', 'r', 'y', '!'}, "this_too": bytes.NewBuffer([]byte("if fmt.Print can print it!")), }) // you can use a more classical Printf type log method too. mlog.Debugf("a printf style debug log: %s", "here!") mlog.Infof("a printf style info log: %s", "here!") mlog.Fatalm("time for a nap", mlog.Map{"cleanup": false}) }
Index ¶
- Variables
- func Debug(v ...interface{})
- func Debugf(format string, v ...interface{})
- func Debugm(message string, v Map)
- func Debugx(message string, attrs ...*Attr)
- func Fatal(v ...interface{})
- func Fatalf(format string, v ...interface{})
- func Fatalm(message string, v Map)
- func Fatalx(message string, attrs ...*Attr)
- func HasDebug() bool
- func Info(v ...interface{})
- func Infof(format string, v ...interface{})
- func Infom(message string, v Map)
- func Infox(message string, attrs ...*Attr)
- func Panic(v ...interface{})
- func Panicf(format string, v ...interface{})
- func Panicm(message string, v Map)
- func Panicx(message string, attrs ...*Attr)
- func Print(v ...interface{})
- func Printf(format string, v ...interface{})
- func Printm(message string, v Map)
- func Printx(message string, attrs ...*Attr)
- func SetEmitter(e Emitter)
- func SetFlags(flags FlagSet)
- func SetOutput(writer io.Writer)
- type Attr
- type Emitter
- type FlagSet
- type FormatWriterJSON
- type FormatWriterPlain
- type FormatWriterStructured
- type Logger
- func (l *Logger) Debug(v ...interface{})
- func (l *Logger) Debugf(format string, v ...interface{})
- func (l *Logger) Debugm(message string, v Map)
- func (l *Logger) Debugx(message string, attrs ...*Attr)
- func (l *Logger) Emit(level int, message string, extra Map)
- func (l *Logger) EmitAttrs(level int, message string, extra ...*Attr)
- func (l *Logger) Fatal(v ...interface{})
- func (l *Logger) Fatalf(format string, v ...interface{})
- func (l *Logger) Fatalm(message string, v Map)
- func (l *Logger) Fatalx(message string, attrs ...*Attr)
- func (l *Logger) Flags() FlagSet
- func (l *Logger) HasDebug() bool
- func (l *Logger) Info(v ...interface{})
- func (l *Logger) Infof(format string, v ...interface{})
- func (l *Logger) Infom(message string, v Map)
- func (l *Logger) Infox(message string, attrs ...*Attr)
- func (l *Logger) Panic(v ...interface{})
- func (l *Logger) Panicf(format string, v ...interface{})
- func (l *Logger) Panicm(message string, v Map)
- func (l *Logger) Panicx(message string, attrs ...*Attr)
- func (l *Logger) Print(v ...interface{})
- func (l *Logger) Printf(format string, v ...interface{})
- func (l *Logger) Printm(message string, v Map)
- func (l *Logger) Printx(message string, attrs ...*Attr)
- func (l *Logger) SetEmitter(e Emitter)
- func (l *Logger) SetFlags(flags FlagSet)
- func (l *Logger) SetOutput(writer io.Writer)
- func (l *Logger) Write(b []byte) (int, error)
- type Map
- type TestingLogWriter
Constants ¶
This section is empty.
Variables ¶
var DefaultLogger = New(os.Stderr, Lstd)
DefaultLogger is the default package level Logger
Functions ¶
func Debugf ¶
func Debugf(format string, v ...interface{})
Debugf logs to the default Logger. See Logger.Debugf
func Fatalf ¶
func Fatalf(format string, v ...interface{})
Fatalf logs to the default Logger. See Logger.Fatalf
func HasDebug ¶ added in v1.0.2
func HasDebug() bool
HasDebug returns true if the default Logger has debug logging FlagSet enabled. See Logger.HasDebug
func Infof ¶
func Infof(format string, v ...interface{})
Infof logs to the default Logger. See Logger.Infof
func Panic ¶ added in v1.0.1
func Panic(v ...interface{})
Panic is equivalent to Print() followed by a call to panic(). See Logger.Panic
func Panicf ¶ added in v1.0.1
func Panicf(format string, v ...interface{})
Panicf is equivalent to Printf() followed by a call to panic(). See Logger.Panicf
func Printf ¶
func Printf(format string, v ...interface{})
Printf logs to the default Logger. See Logger.Printf
func SetEmitter ¶
func SetEmitter(e Emitter)
SetEmitter sets the Emitter for the degault logger. See Logger.SetEmitter.
Types ¶
type Emitter ¶
type Emitter interface { Emit(logger *Logger, level int, message string, extra Map) EmitAttrs(logger *Logger, level int, message string, extra ...*Attr) }
Emitter is the interface implemented by mlog logging format writers.
type FlagSet ¶
type FlagSet uint64
FlagSet defines the output formatting flags (bitfield) type, which define certainly fields to appear in the output.
const ( // Ltimestamp specifies to log the date+time stamp Ltimestamp FlagSet = 1 << iota // Ltai64n specifies to use tia64n timestamps // overrides Ltimestamp. Ltai64n // Llevel specifies to log message level. Llevel // Llongfile specifies to log file path and line number: /a/b/c/d.go:23 Llongfile // Lshortfile specifies to log file name and line number: d.go:23. // overrides Llongfile. Lshortfile // Lsort specifies to sort Map key value pairs in output. Lsort // Ldebug specifies to enable debug level logging. Ldebug // Lstd is the standard log format if none is specified. Lstd = Ltimestamp | Llevel | Lsort )
func Flags ¶
func Flags() FlagSet
Flags returns the FlagSet of the default Logger. See Logger.Flags.
func (FlagSet) GoString ¶
GoString fulfills the GoStringer interface, defining the format used for the %#v format string.
type FormatWriterJSON ¶
type FormatWriterJSON struct{}
FormatWriterJSON writes a json structured log line. Example:
{"time": "2016-04-29T20:49:12Z", "level": "I", "msg": "this is a log"}
type FormatWriterPlain ¶
type FormatWriterPlain struct{}
FormatWriterPlain a plain text structured log line. Example:
2016-04-29T20:49:12Z INFO this is a log
type FormatWriterStructured ¶
type FormatWriterStructured struct{}
FormatWriterStructured writes a plain text structured log line. Example:
time="2016-04-29T20:49:12Z" level="I" msg="this is a log"
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
A Logger represents a logging object, that embeds log.Logger, and provides support for a toggle-able debug flag.
func NewFormatLogger ¶
NewFormatLogger creates a new Logger, using the specified Emitter.
func (*Logger) Debug ¶
func (l *Logger) Debug(v ...interface{})
Debug conditionally logs message at level="debug". If the Logger does not have the Ldebug flag, nothing is logged.
func (*Logger) Debugf ¶
Debugf formats and conditionally logs message at level="debug". If the Logger does not have the Ldebug flag, nothing is logged.
func (*Logger) Debugm ¶
Debugm conditionally logs message and any Map elements at level="debug". If the Logger does not have the Ldebug flag, nothing is logged.
func (*Logger) Debugx ¶ added in v1.0.8
Debugx conditionally logs message and any Attr elements at level="debug". If the Logger does not have the Ldebug flag, nothing is logged.
func (*Logger) Fatal ¶
func (l *Logger) Fatal(v ...interface{})
Fatal logs message at level="fatal", then calls os.Exit(1)
func (*Logger) Fatalm ¶
Fatalm logs message and any Map elements at level="fatal", then calls os.Exit(1)
func (*Logger) Fatalx ¶ added in v1.0.8
Fatalx logs message and any Map elements at level="fatal", then calls os.Exit(1)
func (*Logger) HasDebug ¶
HasDebug returns true if the debug logging FlagSet is enabled, false otherwise.
func (*Logger) Panic ¶ added in v1.0.1
func (l *Logger) Panic(v ...interface{})
Panic logs message at level="fatal", then calls panic().
func (*Logger) Panicf ¶ added in v1.0.1
Panicf formats and logs message at level="fatal", then calls panic().
func (*Logger) Panicm ¶ added in v1.0.1
Panicm logs message and any Map elements at level="fatal", then calls panic().
func (*Logger) Panicx ¶ added in v1.0.8
Panicx logs message and any Map elements at level="fatal", then calls panic().
type Map ¶
type Map map[string]interface{}
Map is a key value element used to pass data to the Logger functions.
func (Map) SortedString ¶
SortedString returns a sorted string representation of the Map's key value pairs.
type TestingLogWriter ¶ added in v1.0.10
type TestingLogWriter struct {
// contains filtered or unexported fields
}
TestoingLogWriter is an adapter between mlog and Go's testing package, which lets us send all output to `t.Log` so that it's correctly collated with the test that emitted it. This helps especially when using parallel testing where output would otherwise be interleaved and make debugging extremely difficult.