golog

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2023 License: Apache-2.0 Imports: 5 Imported by: 11

README

golog Go Reference

Simple logging library for golang

Using

Import

import "github.com/pchchv/golog"

Call golog.{Plain|Info|Debug|Error|Fatal} with a message.

golog.Info("Hello world!")
golog.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 golog.Plain function does not produce leading information (date, log-level, etc.) and just acts like fmt.Printf does.

Error handling

Recommended to use pkg/errors package to create and wrap your errors. It enables you to see stack traces.

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

err := someFunctionCall()
golog.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 golog.LogLevel. Possible value are golog.LOG_PLAIN, golog.LOG_DEBUG, golog.LOG_INFO, golog.LOG_ERROR and golog.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 golog.Plain()*
golog.Debug()
golog.Info()
golog.Error()
golog.Fatal()
golog.CheckFatal()
golog.Stack()
LOG_DEBUG golog.Debug()
golog.Info()
golog.Error()
golog.Fatal()
golog.CheckFatal()
golog.Stack()
LOG_INFO golog.Info()
golog.Error()
golog.Fatal()
golog.CheckFatal()
golog.Stack()
LOG_ERROR golog.Error()
golog.Fatal()
golog.CheckFatal()
golog.Stack()
LOG_FATAL golog.Fatal()
golog.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 golog.FormatFunctions array.

Exmaple: To specify your own debug-format:

func main() {
    // Whenever golog.Debug is called, our simpleDebug method is used to produce the output.
    golog.FormatFunctions[golog.LOG_DEBUG] = simpleDebug
    
    golog.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 golog.DateFormat variable. The format of this variable if the format described in the time package.

Example:

func main() {
    golog.DateFormat = "02.01.2006 at 15:04:05"
    
    golog.Debug("Hello world!")
}

This will produce:

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

TODO

  • Logging to file

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_DEBUG: LogDefault,
		LOG_INFO:  LogDefault,
		LOG_ERROR: LogDefault,
		LOG_FATAL: LogDefault,
		LOG_PANIC: 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_DEBUG: "[DEBUG]",
		LOG_INFO:  "[INFO] ",
		LOG_ERROR: "[ERROR]",
		LOG_FATAL: "[FATAL]",
		LOG_PANIC: "[PANIC]",
	}

	LevelOutputs = map[Level]*os.File{
		LOG_PLAIN: os.Stdout,
		LOG_DEBUG: os.Stdout,
		LOG_INFO:  os.Stdout,
		LOG_ERROR: os.Stderr,
		LOG_FATAL: os.Stderr,
		LOG_PANIC: 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. 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 Panic added in v1.0.1

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

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, a full error stack trace will be output. If normal errors are used, 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.

Types

type Level

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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