Simple, customizable, leveled and efficient logging in Go

Related tags

log
Overview

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
Issues
Releases(v1.0.1)
Owner
Erman İmer
Go Developer
Erman İmer
Zero Allocation JSON Logger

Zero Allocation JSON Logger The zerolog package provides a fast and simple logger dedicated to JSON output. Zerolog's API is designed to provide both

Olivier Poitrey 5k Jul 23, 2021
Logur is an opinionated collection of logging best practices

Logur is an opinionated collection of logging best practices. Table of Contents Preface Features Installation Usage FAQ Why not just X logger? Why not

Logur 129 Jul 16, 2021
Golang logging library

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

Örjan Fors 1.7k Jul 17, 2021
A pure Go contextual logging library with "batteries included"

Cue Overview Cue implements contextual logging with "batteries included". It has thorough test coverage and supports logging to stdout/stderr, file, s

Bob Ziuchkovski 26 Sep 16, 2019
Simple, customizable, leveled and efficient logging in Go

log Simple, customizable, leveled and efficient logging in Go Installation go get -u github.com/ermanimer/log Features log is a simple logging package

Erman İmer 22 Jul 13, 2021
A minimal and extensible structured logger

⚠️ PRE-RELEASE ⚠️ DO NOT IMPORT THIS MODULE YOUR PROJECT WILL BREAK package log package log provides a minimal interface for structured logging in ser

Go kit 54 Jul 21, 2021
Structured, pluggable logging for Go.

Logrus Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger. Logrus is in maintenance-mode. We wi

Simon Eskildsen 18.3k Jul 24, 2021
Logrus is a structured, pluggable logging for Go.

Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger.

Simon Eskildsen 274 May 25, 2021
Dead simple, super fast, zero allocation and modular logger for Golang

Onelog Onelog is a dead simple but very efficient JSON logger. It is one of the fastest JSON logger out there. Also, it is one of the logger with the

Francois Parquet 396 Jul 16, 2021
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

null 430 Jul 23, 2021
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

Go 2.8k Jul 22, 2021
Structured Logging Made Easy

Structured Logging Made Easy Features Dependency Free Simple and Clean Interface Consistent Writer IOWriter, io.Writer wrapper FileWriter, rotating &

phuslu 386 Jul 22, 2021
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

Uber Go 13.2k Jul 23, 2021
Logging, distilled

What is distillog? distillog aims to offer a minimalistic logging interface that also supports log levels. It takes the stdlib API and only slightly e

Akshay Moghe 26 Jun 21, 2021