Simply run the following from your project root:
go get -u github.com/gleich/lumber/v2
Output a success log.
Demo:
package main
import (
"time"
"github.com/gleich/lumber/v2"
)
func main() {
lumber.Success("booted up the program!")
time.Sleep(2 * time.Second)
lumber.Success("waited 2 seconds!")
}
Outputs:
Output an info log.
Demo:
package main
import (
"time"
"github.com/gleich/lumber/v2"
)
func main() {
lumber.Info("Getting the current year")
now := time.Now()
lumber.Info("Current year is", now.Year())
}
Outputs:
Output a debug log.
Demo:
package main
import (
"os"
"github.com/gleich/lumber/v2"
)
func main() {
homeDir, _ := os.UserHomeDir()
lumber.Debug("User's home dir is", homeDir)
}
Outputs:
Output a warning log.
Demo:
package main
import (
"time"
"github.com/gleich/lumber/v2"
)
func main() {
now := time.Now()
if now.Year() != 2004 {
lumber.Warning("Current year isn't 2004")
}
}
Outputs:
Output an error log with a stack trace.
Demo:
package main
import (
"os"
"github.com/gleich/lumber/v2"
)
func main() {
fname := "invisible-file.txt"
_, err := os.ReadFile(fName)
if err != nil {
lumber.Error(err, "Failed to read from", fname)
}
}
Outputs:
Output an error message.
Demo:
package main
import "github.com/gleich/lumber/v2"
func main() {
lumber.ErrorMsg("Ahhh stuff broke")
}
Outputs:
Output a fatal log with a stack trace.
Demo:
package main
import (
"os"
"github.com/gleich/lumber/v2"
)
func main() {
fName := "invisible-file.txt"
_, err := os.ReadFile(fName)
if err != nil {
lumber.Fatal(err, "Failed to read from", fName)
}
}
Outputs:
Output a fatal message.
Demo:
package main
import "github.com/gleich/lumber/v2"
func main() {
lumber.FatalMsg("Ahhh stuff broke")
}
Outputs:
You can customize lumber by creating a custom logger and changing values on it. You then call the log functions on the custom logger. Below is an example of this.
package main
import "github.com/gleich/lumber/v2"
func main() {
log := lumber.NewCustomLogger()
log.ColoredOutput = false
log.ExitCode = 2
log.Success("Calling from custom logger!")
}
Here are all the variables that can be changed:
Variable Name | Description | Default Value | Type |
---|---|---|---|
NormalOut |
The output file for Debug, Success, Warning, and Info | os.Stdout |
*os.File |
ErrOut |
The output file for Fatal and Error | os.Stderr |
*os.File |
ExtraNormalOuts |
Extra normal output destinations (e.g. outputting to a file as well as stdout) | []io.Writer{} |
[]io.Writer |
ExtraErrOuts |
Extra error output destinations (e.g. outputting to a file as well as stderr) | []io.Writer{} |
[]io.Writer |
ExitCode |
Fatal exit code | 1 |
int |
Padding |
If the log should have an extra new line at the bottom | false |
bool |
ColoredOutput |
If the output should have color | true |
bool |
TrueColor |
If the output colors should be true colors. Default is true if terminal supports it. | has256ColorSupport() |
bool |
ShowStack |
If stack traces should be shown | true |
bool |
Multiline |
If the should should be spread out to more than one line | false |
bool |
Timezone |
Timezone you want the times to be logged in | time.UTC |
*time.Location |
See some examples in the _examples/ folder! Just run them using go run main.go
.