lumber

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2021 License: MIT Imports: 10 Imported by: 17

README

🪵 lumber 🪵

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

A dead simple, pretty, and feature-rich logger for golang

🚀 Install

Simply run the following from your project root:

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

🌲 Logging Functions

lumber.Success()

Output a success log.

Demo:

package main

import (
    "time"

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

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/gleich/lumber/v2"
)

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

Outputs:

info output

lumber.Debug()

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:

debug output

lumber.Warning()

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:

warning output

lumber.Error()

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:

error output

lumber.ErrorMsg()

Output an error message.

Demo:

package main

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

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

Outputs:

errorMsg output

lumber.Fatal()

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:

fatal output

lumber.FatalMsg()

Output a fatal message.

Demo:

package main

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

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

Outputs:

fatalMsg output

⚙️ Customization

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

✨ Examples

See some examples in the examples/ folder! Just run them using go run main.go.

🙌 Contributing

Before contributing please read the CONTRIBUTING.md file.

👥 Contributors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx ...interface{})

Output a debug log

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 Fatal

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

Output a fatal log

func FatalMsg

func FatalMsg(ctx ...interface{})

Output a fatal log with no actual error value

func Info

func Info(ctx ...interface{})

Output an info log

func Success

func Success(ctx ...interface{})

Output a success log

func Warning

func Warning(ctx ...interface{})

Output a warning log

Types

type Logger added in v2.1.0

type Logger struct {
	NormalOut       *os.File       // The output file for Debug, Success, Warning, and Info. Default is os.Stdout
	ErrOut          *os.File       // The output file for Fatal, FatalMsg, Error, and ErrorMsg. Default is os.Stderr
	ExtraNormalOuts []io.Writer    // Extra normal output destinations (e.g. outputting to a file as well)
	ExtraErrOuts    []io.Writer    // Extra error output destinations (e.g. outputting to a file as well)
	ExitCode        int            // Fatal exit code. Default is 1
	ShowStack       bool           // If stack trades should be included. Default is true
	Timezone        *time.Location // Timezone for the time to be outputted in. Default is time.UTC
	Padding         bool           // If the log should have an extra new line at the bottom. Default is true
	Multiline       bool           // If the log should span multiple lines. Default is false
	ColoredOutput   bool           // If the output should have color. Default is true
	TrueColor       bool           // If the output should be true color or basic colors. Default is true if the terminal supports it
}

Custom logger for lumber to use

func NewCustomLogger added in v2.1.0

func NewCustomLogger() Logger

func (Logger) Debug added in v2.1.0

func (config Logger) Debug(ctx ...interface{})

Output a debug log using a custom logger

func (Logger) Error added in v2.1.0

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

Output an error log using a custom logger

func (Logger) ErrorMsg added in v2.1.0

func (config Logger) ErrorMsg(ctx ...interface{})

Output an error log with no actual error value using a custom logger

func (Logger) Fatal added in v2.1.0

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

Output a fatal log using a custom logger

func (Logger) FatalMsg added in v2.1.0

func (config Logger) FatalMsg(ctx ...interface{})

Output a fatal log with no actual error value using a custom logger

func (Logger) Info added in v2.1.0

func (config Logger) Info(ctx ...interface{})

Output an info log using a custom logger

func (Logger) Success added in v2.1.0

func (config Logger) Success(ctx ...interface{})

Output a success log using a custom logger

func (Logger) Warning added in v2.1.0

func (config Logger) Warning(ctx ...interface{})

Output a warning log using a custom logger

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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