Simple, customizable, leveled and efficient logging in Go

log

Simple, customizable, leveled and efficient logging in Go

Go Go Report Card

Installation

go get -u github.com/ermanimer/log

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 customizable. 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.SetInfoPrefix("INF")
l.SetWarningPrefix("WAR")
l.SetErrorPrefix("ERR")
l.SetFatalPrefix("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
Owner
Erman İmer
Go Developer
Erman İmer
Similar Resources

Detecctor is a ⚡ fast, fully customizable 💗 monitoring platform. It uses Telegram as a notification 📥 service

Detecctor is a ⚡ fast, fully customizable 💗 monitoring platform. It uses Telegram as a notification 📥 service. The main components are a TCP server, MongoDB and multiple clients.

Nov 16, 2021

Simple and configurable Logging in Go, with level, formatters and writers

go-log Logging package similar to log4j for the Golang. Support dynamic log level Support customized formatter TextFormatter JSONFormatter Support mul

Sep 26, 2022

Simple and blazing fast lockfree logging library for golang

Simple and blazing fast lockfree logging library for golang

glg is simple golang logging library Requirement Go 1.11 Installation go get github.com/kpango/glg Example package main import ( "net/http" "time"

Nov 28, 2022

Simple, configurable and scalable Structured Logging for Go.

log Log is a simple, highly configurable, Structured Logging library Why another logging library? There's allot of great stuff out there, but also tho

Sep 26, 2022

Simple & efficient Go library for getting daily foreign exchange rates. Built-in support for 50+ currencies.

go-forex Simple and efficient Go library for getting daily foreign exchange rates. Built-in support for ca. 50 currencies. Also includes a simple comm

Sep 12, 2022

A simple logging interface for Go

A more minimal logging API for Go Before you consider this package, please read this blog post by the inimitable Dave Cheney. I really appreciate what

Dec 30, 2022

A simple logging framework for Go program.

A simple logging framework for Go program.

ASLP A Go language based log library, simple, convenient and concise. Three modes, standard output, file mode and common mode. Convenient, simple and

Nov 14, 2022

A Go (golang) package providing high-performance asynchronous logging, message filtering by severity and category, and multiple message targets.

ozzo-log Other languages 简体中文 Русский Description ozzo-log is a Go package providing enhanced logging support for Go programs. It has the following fe

Dec 17, 2022

The Simplest and worst logging library ever written

gologger A Simple Easy to use go logger library. Displays Colored log into console in any unix or windows platform. You can even store your logs in fi

Sep 26, 2022
Blazing fast, structured, leveled logging in Go.

⚡ zap Blazing fast, structured, leveled logging in Go. Installation go get -u go.uber.org/zap Note that zap only supports the two most recent minor ve

Jan 7, 2023
Tiny structured logging abstraction or facade for various logging libraries, allowing the end user to plug in the desired logging library in main.go
Tiny structured logging abstraction or facade for various logging libraries, allowing the end user to plug in the desired logging library in main.go

Tiny structured logging abstraction or facade for various logging libraries, allowing the end user to plug in the desired logging library in main.go.

Dec 7, 2022
A simple logging module for go, with a rotating file feature and console logging.

A simple logging module for go, with a rotating file feature and console logging. Installation go get github.com/jbrodriguez/mlog Usage Sample usage W

Dec 14, 2022
Leveled execution logs for Go

glog ==== Leveled execution logs for Go. This is an efficient pure Go implementation of leveled logs in the manner of the open source C++ package h

Dec 24, 2022
CoLog is a prefix-based leveled execution log for Go
CoLog is a prefix-based leveled execution log for Go

What's CoLog? CoLog is a prefix-based leveled execution log for Go. It's heavily inspired by Logrus and aims to offer similar features by parsing the

Dec 14, 2022
Leveled execution logs for Go.

glog Leveled execution logs for Go. This is an efficient pure Go implementation of leveled logs in the manner of the open source C++ package glog. By

Nov 29, 2021
Gomol is a library for structured, multiple-output logging for Go with extensible logging outputs

gomol Gomol (Go Multi-Output Logger) is an MIT-licensed structured logging library for Go. Gomol grew from a desire to have a structured logging libra

Sep 26, 2022
FactorLog is a logging infrastructure for Go that provides numerous logging functions for whatever your style may be
FactorLog is a logging infrastructure for Go that provides numerous logging functions for whatever your style may be

FactorLog FactorLog is a fast logging infrastructure for Go that provides numerous logging functions for whatever your style may be. It could easily b

Aug 3, 2022
Package logging implements a logging infrastructure for Go
Package logging implements a logging infrastructure for Go

Golang logging library Package logging implements a logging infrastructure for Go. Its output format is customizable and supports different logging ba

Nov 10, 2021
Binalyze logger is an easily customizable wrapper for logrus with log rotation

logger logger is an easily customizable wrapper for logrus with log rotation Usage There is only one function to initialize logger. logger.Init() When

Oct 2, 2022