lumber

package module
v3.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: MIT Imports: 9 Imported by: 3

README

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

lumber.Done()

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

lumber.Info()

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

lumber.Debug()

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

lumber.Warning()

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

lumber.Error()

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

lumber.ErrorMsg()

Output an error message.

Demo:

package main

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

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/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

lumber.FatalMsg()

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(v ...any)

Output a INFO log message

func Done

func Done(v ...any)

Output a DONE log message

func Error

func Error(err error, v ...any)

Output a ERROR log message with information about the error

func ErrorMsg

func ErrorMsg(v ...any)

Output a ERROR log message

func Fatal

func Fatal(err error, v ...any)

Output a FATAL log message with information about the error

func FatalMsg

func FatalMsg(v ...any)

Output a FATAL log message

func Info

func Info(v ...any)

Output a INFO log message

func SetColors added in v3.0.1

func SetColors(colors Colors)

Set the colors used for logging

func SetErrOut

func SetErrOut(out *os.File)

Set the output or Fatal, FatalMsg, Error, and ErrorMsg.

Default is os.Stderr

func SetExtraErrOuts

func SetExtraErrOuts(outs []io.Writer)

Set the extra normal out destinations (e.g. logging to a file)

func SetExtraNormalOuts

func SetExtraNormalOuts(outs []io.Writer)

Set the extra normal out destinations (e.g. logging to a file)

func SetFatalExitCode

func SetFatalExitCode(code int)

Set the exit code used by Fatal and FatalMsg.

Default is 1

func SetNormalOut

func SetNormalOut(out *os.File)

Set the output or Debug, Done, Warning, and Info.

Default is os.Stdout

func SetShowStack

func SetShowStack(show bool)

Set if the stack trace should be shown or not when calling Error or Fatal.

Default is true

func SetTimeFormat

func SetTimeFormat(format string)

Set the time format

Default is 2006/01/02 15:04:05 MST

func SetTimezone

func SetTimezone(loc *time.Location)

Set the timezone

Default is time.UTC

func Warning

func Warning(v ...any)

Output a WARN log message

Types

type Colors

type Colors struct {
	DebugStyle   lipgloss.Style
	InfoStyle    lipgloss.Style
	DoneStyle    lipgloss.Style
	WarningStyle lipgloss.Style
	ErrorStyle   lipgloss.Style
	FatalStyle   lipgloss.Style
}

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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