This package is designed to support better logging for Go. Specifically, this project aims to support different levels of logging and the ability to customize log output via custom implementations of the interfaces provided in the package. In addition, all logged messages are wrapped in closures and are only evaluated and rendered if they will be outputed.
You can install this package via:
goinstall github.com/awreece/golog
The easiest way to start using this package is to use the Global
PackageLogger and the exported global namespace wrapper functions. For
example:
package mypackage
import "github.com/awreece/golog"
func Foo() {
golog.Info("Hello, world")
golog.Warningf("Error %d", 4)
golog.Errorc(func() { return verySlowStringFunction() })
golog.Fatal("Error opening file:", err)
}
The Global PackageLogger outputs to default files set by flags. For example,
to log to stderr and to temp.log at log level WARNING, invoke the binary
using this package as follows:
./mybinary --golog.logfile=/dev/stderr --golog.logfile=temp.log --golog.minloglevel=1
This package also makes it easy to log to a testing harness in addition to
files. To do this, invoke StartTestLogging(t) at the start of every test
and StopTestLogging() at the end. For example:
package mypackage
import (
"github.com/awreece/golog"
"testing"
)
func TestFoo(t *testing.T) {
golog.StartTestLogging(t); defer golog.StopTestLogging()
// Test the Foo() function.
Foo()
}
While in test logging mode, calls to Fatal() (and DefaultLogger.FailNow())
will call testing.(*T).FailNow() rather than exiting the program abruptly.
Another common way to use this pacakge is to create a local PackageLogger.
This can either be declared on the package level or passed in by value.
This package is highly modular and configurable; different components can be
plugged in to modify the behavior. For example, to speed up logging, an advanced
user could try creating a LocationLogger using the NoLocation function, or
even create a custom location function.
Advanced users can further take advantage of the modularity of the package to
implement and control individual parts. For example, logging in XML format
should be done by writing a proper LogOuter.
This package was designed to be highly modular, with different interfaces for each logical component. The important types are:
-
A
LogMessageis a logged message with associated metadata. -
A
LogOutercontrols the formatted output of aLogMessage. -
A
MultiLogOutermultiplexes an outputted message to a set of keyedLogOuters. The associatedMultiLogOuterFlagautomatically add logfiles to the associated set ofLogOuters. -
A
Loggerdecides whether or not to log a message, and if so renders the message and outputs it. -
A
LocationLoggeris a wrapper for aLoggerthat generates a closure to return aLogMessagewith the associate metadata and is the first easily usable entrypoint into this package. -
A
StringLoggeris a wrapper for aLocationLoggerthat exports methods for logging at semantic levels. -
A
PackageLoggerhas a set of functions designed be quickly useful and is the expected entry point into this package.
For additional documenation, see the godoc output for this package.