Help us shape the future of Go! Take the 2026 Go Developer Survey

sigolo

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2023 License: Apache-2.0 Imports: 5 Imported by: 27

README

sigolo

Simple logging helper for go helping you to use log levels, create customizable outputs and to print stack traces.

How to use it

Just call sigolo.{Plain|Info|Debug|Error|Fatal} with a message.

sigolo.Info("Hello world!")
sigolo.Debug("Coordinate: %d, %d", x, y)

The default printing format is something like this:

2018-07-21 01:59:05.431 [INFO]  main.go:21 | Hello world!
2018-07-21 01:59:05.432 [DEBUG] main.go:22 | Coordinate: 42, 13

Only the sigolo.Plain function does not produce leading information (date, log-level, etc.) and just acts like fmt.Printf does.

Error handling

I recommend the pkg/errors package to create and wrap your errors. Why? It enables you to see stack traces ;)

To exit on an error, there's the sigolo.FatalCheck function:

err := someFunctionCall()
sigolo.FatalCheck(err)

When err is not nil, then the error including stack trace will be printed and your application exists with exit code 1.

Log level

Specify the log level by changing sigolo.LogLevel. Possible value are sigolo.LOG_PLAIN, sigolo.LOG_DEBUG , sigolo.LOG_INFO, sigolo.LOG_ERROR and sigolo.LOG_FATAL.

Depending on the log level, some functions will be quite and do not produce outputs anymore:

log level Methods which will produce an output
LOG_PLAIN sigolo.Plain()*
sigolo.Debug()
sigolo.Info()
sigolo.Error()
sigolo.Fatal()
sigolo.CheckFatal()
sigolo.Stack()
LOG_DEBUG sigolo.Debug()
sigolo.Info()
sigolo.Error()
sigolo.Fatal()
sigolo.CheckFatal()
sigolo.Stack()
LOG_INFO sigolo.Info()
sigolo.Error()
sigolo.Fatal()
sigolo.CheckFatal()
sigolo.Stack()
LOG_ERROR sigolo.Error()
sigolo.Fatal()
sigolo.CheckFatal()
sigolo.Stack()
LOG_FATAL sigolo.Fatal()
sigolo.CheckFatal()
* Prints to stdout but without any tags in front
** This will print the error and call os.Exit(1)

Function suffixes / Variants

Some functions have a suffix with slightly different behavior.

For non-fatal functions:

  • Suffix b: Acts like the normal function, but in order to print the correct caller you can go back in the stack.

For fatal-functions:

  • Suffix f: Acts like the normal function, but after printing the stack, the given format string will be evaluated and printed as well.

Change general output format

The format can be changed by implementing the printing function specified in the sigolo.FormatFunctions array.

Exmaple: To specify your own debug-format:

func main() {
    // Whenever sigolo.Debug is called, our simpleDebug method is used to produce the output.
    sigolo.FormatFunctions[sigolo.LOG_DEBUG] = simpleDebug
    
    sigolo.Debug("Hello world!")
    }
    
    func simpleDebug(writer *os.File, time, level string, maxLength int, caller, message string) {
    // Don't forget the \n at the end ;)
    fmt.Fprintf(writer, "Debug: %s\n", message)
}

This example will print:

Debug: Hello world!

Change time format

To change only the time format, change the value of the sigolo.DateFormat variable. The format of this variable if the format described in the time package.

Example:

func main() {
    // Use the go time formatting string as described in https://pkg.go.dev/time
    sigolo.DateFormat = "02.01.2006 at 15:04:05"
    
    sigolo.Debug("Hello world!")
}

This will produce:

21.07.2018 at 02:16:41 [DEBUG] main.go:37 | Hello world!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	LogLevel   = LOG_INFO
	DateFormat = "2006-01-02 15:04:05.000"

	FormatFunctions = map[Level]func(*os.File, string, string, int, string, string){
		LOG_PLAIN: LogPlain,
		LOG_TRACE: LogDefault,
		LOG_DEBUG: LogDefault,
		LOG_INFO:  LogDefault,
		LOG_ERROR: LogDefault,
		LOG_FATAL: LogDefault,
	}

	// The current maximum length printed for caller information. This is updated each time something gets printed
	CallerColumnWidth = 0

	LevelStrings = map[Level]string{
		LOG_PLAIN: "",
		LOG_TRACE: "[TRACE]",
		LOG_DEBUG: "[DEBUG]",
		LOG_INFO:  "[INFO] ",
		LOG_ERROR: "[ERROR]",
		LOG_FATAL: "[FATAL]",
	}

	LevelOutputs = map[Level]*os.File{
		LOG_PLAIN: os.Stdout,
		LOG_TRACE: os.Stdout,
		LOG_DEBUG: os.Stdout,
		LOG_INFO:  os.Stdout,
		LOG_ERROR: os.Stderr,
		LOG_FATAL: os.Stderr,
	}
)

Functions

func Debug

func Debug(format string, a ...interface{})

func Debugb

func Debugb(framesBackward int, format string, a ...interface{})

Debugb is equal to Debug(...) but can go back in the stack and can therefore show function positions from previous functions.

func Error

func Error(format string, a ...interface{})

func Errorb

func Errorb(framesBackward int, format string, a ...interface{})

Errorb is equal to Error(...) but can go back in the stack and can therefore show function positions from previous functions.

func Fatal

func Fatal(format string, a ...interface{})

func FatalCheck

func FatalCheck(err error)

FatalCheck checks if the error exists (!= nil). If so, it'll fatal with the error message.

func FatalCheckf

func FatalCheckf(err error, format string, a ...interface{})

FatalCheckf checks if the error exists (!= nil). If so, it'll print the error message and fatals with the given format message.

func Info

func Info(format string, a ...interface{})

func Infob

func Infob(framesBackward int, format string, a ...interface{})

Infob is equal to Info(...) but can go back in the stack and can therefore show function positions from previous functions.

func LogDefault

func LogDefault(writer *os.File, time, level string, maxLength int, caller, message string)

func LogPlain

func LogPlain(writer *os.File, time, level string, maxLength int, caller, message string)

func Plain

func Plain(format string, a ...interface{})

func Plainb

func Plainb(framesBackward int, format string, a ...interface{})

Plainb is equal to Plain(...) but can go back in the stack and can therefore show function positions from previous functions.

func Stack

func Stack(err error)

Stack tries to print the stack trace of the given error using the %+v format string. When using the https://github.com/pkg/errors package, this will print a full stack trace of the error. If normal errors are used, this function will just print the error.

func Stackb

func Stackb(framesBackward int, err error)

Stackb is equal to Stack(...) but can go back in the stack and can therefore show function positions from previous functions.

func Trace added in v1.1.0

func Trace(format string, a ...interface{})

func Traceb added in v1.1.0

func Traceb(framesBackward int, format string, a ...interface{})

Traceb is equal to Trace(...) but can go back in the stack and can therefore show function positions from previous functions.

Types

type Level

type Level int
const (
	LOG_PLAIN Level = iota
	LOG_TRACE
	LOG_DEBUG
	LOG_INFO
	LOG_ERROR
	LOG_FATAL
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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