Skip to content

gleich/lumber

Repository files navigation

lumber

Godoc Reference lint workflow result GitHub go.mod Go version Golang report card

Easy to use & pretty logger for golang

Install

Simply run the following from your project root:

go get -u github.com/gleich/lumber/v3

Logging Functions

Output a "DONE" log.

Demo:

package main

import (
    "time"

    "github.com/gleich/lumber/v3"
)

func main() {
    lumber.Done("booted up the program!")
    time.Sleep(2 * time.Second)
    lumber.Done("waited 2 seconds!")
}

Outputs:

Done output

Output an info log.

Demo:

package main

import (
    "time"

    "github.com/gleich/lumber/v3"
)

func main() {
    lumber.Info("Getting the current year")
    now := time.Now()
    lumber.Info("Current year is", now.Year())
}

Outputs:

info output

Output a debug log.

Demo:

package main

import (
    "os"

    "github.com/gleich/lumber/v3"
)

func main() {
    homeDir, _ := os.UserHomeDir()
    lumber.Debug("User's home dir is", homeDir)
}

Outputs:

debug output

Output a warning log.

Demo:

package main

import (
    "time"

    "github.com/gleich/lumber/v3"
)

func main() {
    now := time.Now()
    if now.Year() != 2004 {
        lumber.Warning("Current year isn't 2004")
    }
}

Outputs:

warning output

Output an error log with a stack trace.

Demo:

package main

import (
    "os"

    "github.com/gleich/lumber/v3"
)

func main() {
    fname := "invisible-file.txt"
    _, err := os.ReadFile(fName)
    if err != nil {
        lumber.Error(err, "Failed to read from", fname)
    }
}

Outputs:

error output

Output an error message.

Demo:

package main

import "github.com/gleich/lumber/v3"

func main() {
    lumber.ErrorMsg("Ahhh stuff broke")
}

Outputs:

errorMsg output

Output a fatal log with a stack trace.

Demo:

package main

import (
    "os"

    "github.com/gleich/lumber/v3"
)

func main() {
    fName := "invisible-file.txt"
    _, err := os.ReadFile(fName)
    if err != nil {
        lumber.Fatal(err, "Failed to read from", fName)
    }
}

Outputs:

fatal output

Output a fatal message.

Demo:

package main

import "github.com/gleich/lumber/v3"

func main() {
    lumber.FatalMsg("Ahhh stuff broke")
}

Outputs:

fatalMsg output

Customization

You can customize the logger that lumber uses. Below is an example of some of this customization:

package main

import (
    "time"

    "github.com/gleich/lumber/v3"
)

func main() {
    lumber.SetTimezone(time.Local)
    lumber.SetTimeFormat("Mon Jan 2 15:04:05 MST 2006")
    lumber.SetFatalExitCode(0)

    lumber.Done("Calling from custom logger")
}

Examples

See some examples in the _examples/ folder.