Documentation ¶
Overview ¶
Package mlog implement buffered multi writers of log. It can have zero or more normal writers (for example, os.Stdout and os.File) and zero or more error writers (for example, os.Stderr and different os.File).
The MultiLogger is buffered to minimize waiting time when writing to multiple writers that have different latencies. For example, if we have one writer to os.Stdout, one writer to file, and one writer to network; the writer to network may take more time to finish than to os.Stdout and file, which may slowing down the program if we want to wait for all writes to finish.
For this reason, do not forget to call Flush when your program exit.
The default MultiLogger use time.RFC3339 as the default time layout, empty prefix, os.Stdout for the output writer, and os.Stderr for the error writer.
Format of written log,
[time] [prefix] <message>
The time and prefix only printed if its not empty, and the single space is added for convenience.
Index ¶
- func Errf(format string, v ...interface{})
- func ErrorWriter() io.Writer
- func Fatalf(format string, v ...interface{})
- func Flush()
- func Outf(format string, v ...interface{})
- func Panicf(format string, v ...interface{})
- func PrintStack()
- func RegisterErrorWriter(errw NamedWriter)
- func RegisterOutputWriter(outw NamedWriter)
- func SetPrefix(prefix string)
- func SetTimeFormat(layout string)
- func UnregisterErrorWriter(name string)
- func UnregisterOutputWriter(name string)
- type MultiLogger
- func (mlog *MultiLogger) Errf(format string, v ...interface{})
- func (mlog *MultiLogger) Fatalf(format string, v ...interface{})
- func (mlog *MultiLogger) Flush()
- func (mlog *MultiLogger) Outf(format string, v ...interface{})
- func (mlog *MultiLogger) Panicf(format string, v ...interface{})
- func (mlog *MultiLogger) PrintStack()
- func (mlog *MultiLogger) RegisterErrorWriter(errw NamedWriter)
- func (mlog *MultiLogger) RegisterOutputWriter(outw NamedWriter)
- func (mlog *MultiLogger) SetPrefix(prefix string)
- func (mlog *MultiLogger) SetTimeFormat(layout string)
- func (mlog *MultiLogger) UnregisterErrorWriter(name string)
- func (mlog *MultiLogger) UnregisterOutputWriter(name string)
- func (mlog *MultiLogger) Write(b []byte) (n int, err error)
- type NamedWriter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Errf ¶
func Errf(format string, v ...interface{})
Errf write to all registered error writers. The default registered error writer is os.Stderr.
func ErrorWriter ¶ added in v0.29.2
ErrorWriter return the internal default MultiLogger. A call to Write() on returned io.Writer will forward it to all registered error writers.
func Outf ¶
func Outf(format string, v ...interface{})
Outf write to all registered output writers. The default registered output writer is os.Stdout.
func Panicf ¶ added in v0.29.1
func Panicf(format string, v ...interface{})
Panicf is equal to Errf followed by panic.
func PrintStack ¶ added in v0.27.0
func PrintStack()
PrintStack writes to error writers the stack trace returned by debug.Stack.
This function can be useful when debugging panic using recover in the main program by logging the stack trace. For example,
err := recover() if err != nil { mlog.PrintStack() os.Exit(1) }
func RegisterErrorWriter ¶
func RegisterErrorWriter(errw NamedWriter)
RegisterErrorWriter register the named writer to one of error writers.
func RegisterOutputWriter ¶
func RegisterOutputWriter(outw NamedWriter)
RegisterOutputWriter register the named writer to one of output writers.
func SetPrefix ¶
func SetPrefix(prefix string)
SetPrefix set the default prefix for the subsequence writes.
func SetTimeFormat ¶
func SetTimeFormat(layout string)
SetTimeFormat set the default time format for the subsequence writes.
func UnregisterErrorWriter ¶
func UnregisterErrorWriter(name string)
UnregisterErrorWriter remove the error writer by name.
func UnregisterOutputWriter ¶
func UnregisterOutputWriter(name string)
UnregisterOutputWriter remove the output writer by name.
Types ¶
type MultiLogger ¶
type MultiLogger struct {
// contains filtered or unexported fields
}
MultiLogger represent a single log writer that write to multiple outputs. MultiLogger can have zero or more writers for standard output (normal log) and zero or more writers for standard error.
Each call to write APIs (Errf, Fatalf, or Outf) will be prefixed with time format in UTC and optional prefix.
Example ¶
buf := bytes.Buffer{} wouts := []NamedWriter{ NewNamedWriter("stdout", os.Stdout), NewNamedWriter("buffer", &buf), } werrs := []NamedWriter{ NewNamedWriter("stderr", os.Stdout), } mlog := NewMultiLogger("", "mlog:", wouts, werrs) // Create an error writer to slack. slackWebhookURL := os.Getenv("SLACK_WEBHOOK_URL") if len(slackWebhookURL) > 0 { slackChannel := os.Getenv("SLACK_WEBHOOK_CHANNEL") slackUsername := os.Getenv("SLACK_WEBHOOK_USERNAME") slackc, err := slack.NewWebhookClient(slackWebhookURL, slackUsername, slackChannel) if err != nil { log.Fatal(err) } mlog.RegisterErrorWriter(NewNamedWriter("slack", slackc)) } mlog.Outf("writing to standard output and buffer\n") mlog.Errf("writing to standard error and slack\n") mlog.Flush() fmt.Printf("Output on buffer: %s\n", buf.String())
Output: mlog: writing to standard output and buffer mlog: writing to standard error and slack Output on buffer: mlog: writing to standard output and buffer
func NewMultiLogger ¶
func NewMultiLogger(timeFormat, prefix string, outs, errs []NamedWriter) (mlog *MultiLogger)
NewMultiLogger create and initialize new MultiLogger.
func (*MultiLogger) Errf ¶
func (mlog *MultiLogger) Errf(format string, v ...interface{})
Errf write the formatted string and its optional values to all error writers.
If the format string does not end with new line, it will be added.
func (*MultiLogger) Fatalf ¶
func (mlog *MultiLogger) Fatalf(format string, v ...interface{})
Fatalf is equal to Errf and os.Exit(1).
func (*MultiLogger) Flush ¶
func (mlog *MultiLogger) Flush()
Flush all writes and wait until it finished.
func (*MultiLogger) Outf ¶
func (mlog *MultiLogger) Outf(format string, v ...interface{})
Outf write the formatted string and its values to all output writers.
func (*MultiLogger) Panicf ¶ added in v0.29.1
func (mlog *MultiLogger) Panicf(format string, v ...interface{})
Panicf is equal to Errf and followed by panic.
func (*MultiLogger) PrintStack ¶ added in v0.27.0
func (mlog *MultiLogger) PrintStack()
PrintStack writes to error writers the stack trace returned by debug.Stack.
This function can be useful when debugging panic using recover in the main program by logging the stack trace. For example,
err := recover() if err != nil { mlog.PrintStack() os.Exit(1) }
func (*MultiLogger) RegisterErrorWriter ¶
func (mlog *MultiLogger) RegisterErrorWriter(errw NamedWriter)
RegisterErrorWriter register the named writer to one of error writers. The writer Name() must not be empty or it will not registered.
func (*MultiLogger) RegisterOutputWriter ¶
func (mlog *MultiLogger) RegisterOutputWriter(outw NamedWriter)
RegisterOutputWriter register the named writer to one of output writers. The writer Name() must not be empty or it will not registered.
func (*MultiLogger) SetPrefix ¶
func (mlog *MultiLogger) SetPrefix(prefix string)
SetPrefix set the default prefix for the subsequence writes.
func (*MultiLogger) SetTimeFormat ¶
func (mlog *MultiLogger) SetTimeFormat(layout string)
SetTimeFormat set the default time format for the subsequence writes.
func (*MultiLogger) UnregisterErrorWriter ¶
func (mlog *MultiLogger) UnregisterErrorWriter(name string)
UnregisterErrorWriter remove the error writer by name.
func (*MultiLogger) UnregisterOutputWriter ¶
func (mlog *MultiLogger) UnregisterOutputWriter(name string)
UnregisterOutputWriter remove the output writer by name.
type NamedWriter ¶
NamedWriter is io.Writer with name.
func NewNamedWriter ¶
func NewNamedWriter(name string, w io.Writer) NamedWriter
NewNamedWriter create new NamedWriter instance.