mlog

package
v0.45.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2023 License: BSD-3-Clause Imports: 9 Imported by: 2

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

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.

If the generated string does not end with new line, it will be added.

func ErrorWriter added in v0.29.2

func ErrorWriter() io.Writer

ErrorWriter return the internal default MultiLogger. A call to Write() on returned io.Writer will forward it to all registered error writers.

func Fatalf

func Fatalf(format string, v ...interface{})

Fatalf is equal to Errf and os.Exit(1).

func Flush

func Flush()

Flush all writes in queue and wait until it finished.

func Outf

func Outf(format string, v ...interface{})

Outf write to all registered output writers. The default registered output writer is os.Stdout.

If the generated string does not end with new line, it will be added.

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 {
	sync.Mutex
	// 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
// The following example import package
// "github.com/shuLhan/share/api/slack".
// The code is intentionally commented to prevent import cycle.

var (
	buf   = bytes.Buffer{}
	wouts = []NamedWriter{
		NewNamedWriter("stdout", os.Stdout),
		NewNamedWriter("buffer", &buf),
	}
	werrs = []NamedWriter{
		NewNamedWriter("stderr", os.Stdout),
	}
	mlog            = NewMultiLogger("", "mlog:", wouts, werrs)
	slackWebhookURL = os.Getenv("SLACK_WEBHOOK_URL")
)

// Create an error writer to slack.
if len(slackWebhookURL) > 0 {
	// Get the Slack configuration from environment.
	//slackChannel := os.Getenv("SLACK_WEBHOOK_CHANNEL")
	//slackUsername := os.Getenv("SLACK_WEBHOOK_USERNAME")

	// Create Slack's client.
	//slackc, err := slack.NewWebhookClient(slackWebhookURL, slackUsername, slackChannel)
	//if err != nil {
	//log.Fatal(err)
	//}

	// Forward all errors to Slack client.
	//mlog.RegisterErrorWriter(NewNamedWriter("slack", slackc))
}

mlog.Outf("writing to standard output and buffer")
mlog.Errf("writing to standard error and slack")
mlog.Close()
// Try writing to closed mlog.
mlog.Outf("writing to standard output and buffer after close")
fmt.Println("Output on buffer:", 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) *MultiLogger

NewMultiLogger create and initialize new MultiLogger.

func (*MultiLogger) Close added in v0.39.0

func (mlog *MultiLogger) Close()

Close flush and close all log forwarders. Any write to a closed MultiLogger will be ignored.

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 generated 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.

If the generated string does not end with new line, it will be added.

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.

func (*MultiLogger) Write added in v0.29.2

func (mlog *MultiLogger) Write(b []byte) (n int, err error)

Write write the b to all error writers. It will always return the length of b without an error.

type NamedWriter

type NamedWriter interface {
	Name() string
	Write(b []byte) (n int, err error)
}

NamedWriter is io.Writer with name.

func NewNamedWriter

func NewNamedWriter(name string, w io.Writer) NamedWriter

NewNamedWriter create new NamedWriter instance.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL