lumber

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2021 License: MIT Imports: 9 Imported by: 0

README

🪵 lumber 🪵

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

A feature-rich and easy to use logger for golang

🚀 Install

Simply run the following from your project root:

go get -u github.com/Matt-Gleich/lumber

🌲 Common Logs

lumber.Success()

Output a success log.

Demo:

package main

import (
    "time"

    "github.com/Matt-Gleich/lumber"
)

func main() {
    lumber.Success("Loaded up the program!")
    time.Sleep(2 * time.Second)
    lumber.Success("Waited 2 seconds!")
}

Outputs:

success output

lumber.Info()

Output an info log.

Demo:

package main

import (
    "time"

    "github.com/Matt-Gleich/lumber"
)

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

Outputs:

info output

lumber.Debug()

Output a debug log.

Demo:

package main

import (
    "os"

    "github.com/Matt-Gleich/lumber"
)

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

Outputs:

debug output

lumber.Warning()

Output a warning log.

Demo:

package main

import (
    "time"

    "github.com/Matt-Gleich/lumber"
)

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

Outputs:

warning output

lumber.Error()

Output an error log. If err != nil the error will automatically get logged to the console. This auto-check can be turned off in the customization section.

Demo:

package main

import (
    "io/ioutil"

    "github.com/Matt-Gleich/lumber"
)

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

Outputs:

error output

lumber.ErrorMsg()

Output an error message.

Demo:

package main

import "github.com/Matt-Gleich/lumber"

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

Outputs:

errorMsg output

lumber.Fatal()

Output a fatal log. If err != nil the error will automatically get logged to the console and the program will exit (default code is 1). This auto-check can be turned off in the customization section.

Demo:

package main

import (
    "io/ioutil"

    "github.com/Matt-Gleich/lumber"
)

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

Outputs:

fatal output

lumber.FatalMsg()

Output a fatal message.

Demo:

package main

import "github.com/Matt-Gleich/lumber"

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

Outputs:

fatalMsg output

⚙️ Customization

You can customize lumber by changing any of its global variables:

Variable Name Description Default Value Type
lumber.NormalOut The output file for Debug, Success, Warning, and Info os.Stdout *os.File
lumber.ErrOut The output file for Fatal and Error os.Stderr *os.File
lumber.ErrNilCheck If errors should automatically be checked for a non-nil value true bool
lumber.ExitStatus Fatal exit code 1 int
lumber.Padding If the log should have an extra new line at the bottom true bool
lumber.ColoredOutput If the output should have color true bool
lumber.TrueColor If the output colors should be true colors. Default is true if terminal supports it. has256ColorSupport() bool
lumber.ShowStack If stack traces should be shown true bool

Example of changing one of these variables:

package main

import "github.com/Matt-Gleich/lumber"

func main() {
    lumber.ColoredOutput = false
    lumber.Debug("See, no color!")
}

⏰ Changing the log time

All normal log functions have a twin function that accepts a custom time. All of these functions are suffixed with WithTime.

🙌 Contributing

Before contributing please read the CONTRIBUTING.md file.

👥 Contributors

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// If the output should have color
	ColoredOutput = true
	// If the output should be true color or basic colors.
	// Default is true if the terminal supports it
	TrueColor = has256ColorSupport()
)
View Source
var (
	// The output file for Debug, Success, Warning, and Info
	NormalOut = os.Stdout
	// The output file for Fatal and Error
	ErrOut = os.Stderr
	// If errors should automatically be checked for a non-nil value
	ErrNilCheck = true

	// Fatal exit code
	ExitStatus = 1
	// If stack traces should be included
	ShowStack = true
)
View Source
var Padding = true

If the log should have an extra new line at the bottom

Functions

func Debug

func Debug(ctx ...interface{})

Output a debug log

func DebugWithTime

func DebugWithTime(t time.Time, ctx ...interface{})

Output a debug log with a given time

func Error

func Error(err error, ctx ...interface{})

Output an error log

func ErrorMsg

func ErrorMsg(ctx ...interface{})

Output an error log with no actual error value

func ErrorMsgWithTime

func ErrorMsgWithTime(t time.Time, ctx ...interface{})

Output an error log with no actual error value with a given time

func ErrorWithTime

func ErrorWithTime(err error, t time.Time, ctx ...interface{})

Output an error log with a given time

func Fatal

func Fatal(err error, ctx ...interface{})

Output a fatal log

func FatalMsg added in v1.1.0

func FatalMsg(ctx ...interface{})

Output a fatal log with no actual error value

func FatalWithTime

func FatalWithTime(err error, t time.Time, ctx ...interface{})

Output a fatal log with a given time

func Info

func Info(ctx ...interface{})

Output an info log

func InfoWithTime

func InfoWithTime(t time.Time, ctx ...interface{})

Output an info log with a given time

func Success

func Success(ctx ...interface{})

Output a success log

func SuccessWithTime

func SuccessWithTime(t time.Time, ctx ...interface{})

Output a success log with a given time

func Warning

func Warning(ctx ...interface{})

Output a warning log

func WarningWithTime

func WarningWithTime(t time.Time, ctx ...interface{})

Output a warning log with a given time

Types

This section is empty.

Jump to

Keyboard shortcuts

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