Skip to content

pchchv/golog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Reference

golog

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:

2023-01-11 11:59:23.411 [INFO]  main.go:21 | Hello world!
2023-01-11 11:59:23.412 [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:

11.01.2023 at 11:34:22 [DEBUG] main.go:37 | Hello world!