Simple logging in golang
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
tests
LICENSE
README.md
formatter.go
logging.go

README.md

go-logging

go-logging is a simple logging package for go that ressembles the python logging module

  • Easy and intuitive to use
  • Log to multiple handlers with different logging levels and different outputs
  • Write your own formatters(based on "text/template") and use common functions
  • Call logging functions variadically to format the log message with arguments

Logger functions and

The following functions are currently available for calling from the formatter:

  1. asctime - the log record creation time in "YYYY-MM-DD HH:mm:SS" format
  2. created - the log record creation time in epoch
  3. filename - the name of the file calling the function
  4. lineno - the line number of the file calling the function
  5. fileline - the filename and lineno in this format: "filename: lineno"(**this usage is preferred when choosing to present both filename and line number)

Examples:

package main

import (
	"os"

	"github.com/omrikiei/go-logging"
)

func main() {
	// Handler with default formatter
	handler, err := logging.NewHandler(logging.DEBUG, os.Stdout)
	if err != nil {
		panic("got an error")
	}
	logging.AddHandler(handler)
	logging.Debug("Testing a debug message")
	logging.Info("Testing an info message")
	logging.Warn("Testing a warning meesage")
	logging.Error("Testing an error message")
	// Setting a different formatter
	handler.SetFormatter("{{ created }}; {{ fileline }}; {{.LevelNum}}; {{.Message}}")
	logging.Debug("Testing a debug message")
	logging.Info("Testing an info message")
	logging.Warn("Testing a warning meesage")
	logging.Error("Testing an error message")
	// Return to the previous formatter
	handler.SetFormatter(logging.DefaultFormatterPattern)
	str := "hello"
	logging.Debug("Testing a debug message with arguments %s:%d", str, 0)
	logging.Info("Testing an info message with arguments %s:%d", str, 1)
	logging.Warn("Testing a warning meesage with arguments %s:%d", str, 2)
	logging.Error("Testing an error message with arguments %s:%d", str, 3)
}

Output:

eomri-mac:tests eomri$ go run main.go
2018-08-25 01:44:25; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 17; DEBUG; Testing a debug message
2018-08-25 01:44:25; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 18; INFO; Testing an info message
2018-08-25 01:44:25; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 19; WARNING; Testing a warning meesage
2018-08-25 01:44:25; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 20; ERROR; Testing an error message
1535150665; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 23; 0; Testing a debug message
1535150665; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 24; 1; Testing an info message
1535150665; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 25; 2; Testing a warning meesage
1535150665; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 26; 3; Testing an error message
2018-08-25 01:44:25; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 30; DEBUG; Testing a debug message with arguments hello:0
eomri-mac:tests eomri$ go run main.go
2018-08-25 01:52:37; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 17; DEBUG; Testing a debug message
2018-08-25 01:52:37; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 18; INFO; Testing an info message
2018-08-25 01:52:37; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 19; WARNING; Testing a warning meesage
2018-08-25 01:52:37; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 20; ERROR; Testing an error message
1535151157; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 23; 0; Testing a debug message
1535151157; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 24; 1; Testing an info message
1535151157; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 25; 2; Testing a warning meesage
1535151157; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 26; 3; Testing an error message
2018-08-25 01:52:37; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 30; DEBUG; Testing a debug message with arguments hello:0
2018-08-25 01:52:37; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 31; INFO; Testing an info message with arguments hello:1
2018-08-25 01:52:37; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 32; WARNING; Testing a warning meesage with arguments hello:2
2018-08-25 01:52:37; /Users/eomri/go/src/github.com/omrikiei/go-logging/tests/main.go: 33; ERROR; Testing an error message with arguments hello:3
eomri-mac:tests eomri$