Skip to content
/ log Public

Simple, customizable, leveled and efficient logging in Go

License

Notifications You must be signed in to change notification settings

ermanimer/log

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

log

Simple, customizable, leveled and efficient logging in Go

Go Go Report Card

Installation

go get -u github.com/ermanimer/logger

Features

log is a simple logging package for Go.

  • You can set time format and prefixes.
  • You can set logging level to filter out log messages.
  • You can set a hook function to be called after each log.

log isn't the fastest logging solution, but log is efficient while being custimizable. Please see benchmark tests.

Usage

package main

import (
	"os"

	"github.com/ermanimer/log"
)

func main() {
	// create a new logger instance with output and default parameters
	l := log.NewLogger(os.Stdout)

	// log a message
	l.Debug("this is a debug message")

	// log a formatted message
	l.Debugf("this is a %s debug message", "formatted")
}

Output:

2021-06-07T16:46:26+03:00 debug this is a debug message
2021-06-07T16:46:26+03:00 debug this is a formatted debug message

Logging Levels

  • Debug
  • Info
  • Warning
  • Error
  • Fatal

Default Parameters:

Parameter Value
Time Format RFC3339
Debug Prefix debug
Info Prefix info
Warning Prefix warning
Error Prefix error
Fatal Prefix fatal
Logging Level Debug

Set Time Format

l.SetTimeFormat(time.RFC3339Nano)

Set Prefixes

l.SetDebugPrefix("DEB")
l.SetDebugPrefix("INF")
l.SetDebugPrefix("WAR")
l.SetDebugPrefix("ERR")
l.SetDebugPrefix("FAT")

Set Logging Level

l.SetLoggingLevel(InfoLevel)

Set Hook Function:

l.SetHookFunction(func(prefix, message string) {
  //filter messages with prefix and capture for Sentry.io
  //...
})

Benchmark Tests

Test Codes:

func BenchmarkDebug(b *testing.B) {
	// create logger
	l := NewLogger(ioutil.Discard)

	// start benchmark test
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			l.Debug("test")
		}
	})
}

func BenchmarkDebugf(b *testing.B) {
	// create logger
	l := NewLogger(ioutil.Discard)

	// start benchmark test
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			l.Debugf("%s", "test")
		}
	})
}

Results:

Function Time Bytes Allocated Objects Allocated
Debug 410.7 ns/op 4 B/op 1 allocs/op
Debugf 408.7 ns/op 4 B/op 1 allocs/op
Info 404.0 ns/op 4 B/op 1 allocs/op
Infof 403.9 ns/op 4 B/op 1 allocs/op
Warning 407.0 ns/op 4 B/op 1 allocs/op
Warningf 409.4 ns/op 4 B/op 1 allocs/op
Error 404.6 ns/op 4 B/op 1 allocs/op
Errorf 406.2 ns/op 4 B/op 1 allocs/op
Fatal 402.1 ns/op 4 B/op 1 allocs/op
Fatalf 406.0 ns/op 4 B/op 1 allocs/op

About

Simple, customizable, leveled and efficient logging in Go

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages