Seamless printing to the terminal (stdout) and logging to a io.Writer (file) that’s as easy to use as fmt.Println.
Graphic by JonnyEtc
JWW is primarily a wrapper around the excellent standard log library. It provides a few advantages over using the standard log library alone.
I really wanted a very straightforward library that could seamlessly do the following things.
Put calls throughout your source based on type of feedback. No initialization or setup needs to happen. Just start calling things.
Available Loggers are:
These each are loggers based on the log standard library and follow the standard usage. Eg.
import (
jww "github.com/spf13/jwalterweatherman"
)
...
if err != nil {
// This is a pretty serious error and the user should know about
// it. It will be printed to the terminal as well as logged under the
// default thresholds.
jww.ERROR.Println(err)
}
if err2 != nil {
// This error isn’t going to materially change the behavior of the
// application, but it’s something that may not be what the user
// expects. Under the default thresholds, Warn will be logged, but
// not printed to the terminal.
jww.WARN.Println(err2)
}
// Information that’s relevant to what’s happening, but not very
// important for the user. Under the default thresholds this will be
// discarded.
jww.INFO.Printf("information %q", response)
NOTE: You can also use the library in a non-global setting by creating an instance of a Notebook:
notepad = jww.NewNotepad(jww.LevelInfo, jww.LevelTrace, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
notepad.WARN.Println("Some warning"")
Why 7 levels?
Maybe you think that 7 levels are too much for any application… and you are probably correct. Just because there are seven levels doesn’t mean that you should be using all 7 levels. Pick the right set for your needs. Remember they only have to mean something to your project.
Under the default thresholds :
The threshold can be changed at any time, but will only affect calls that execute after the change was made.
This is very useful if your application has a verbose mode. Of course you can decide what verbose means to you or even have multiple levels of verbosity.
import (
jww "github.com/spf13/jwalterweatherman"
)
if Verbose {
jww.SetLogThreshold(jww.LevelTrace)
jww.SetStdoutThreshold(jww.LevelInfo)
}
Note that JWW’s own internal output uses log levels as well, so set the log level before making any other calls if you want to see what it’s up to.
JWW can log to any io.Writer
:
jww.SetLogOutput(customWriter)
This is an early release. I’ve been using it for a while and this is the third interface I’ve tried. I like this one pretty well, but no guarantees that it won’t change a bit.
I wrote this for use in hugo. If you are looking for a static website engine that’s super fast please checkout Hugo.