Skip to content

A simple logger with support for leveled logging

License

Notifications You must be signed in to change notification settings

twikey/go-logger

 
 

Repository files navigation

go-logger

Go Reference

A simple logger with support for leveled logging.

Installation

go get -u github.com/twikey/go-logger

Getting Started

Simple Logging Example

For simple logging you can import the global logger from github.com/twikey/go-logger/log

package main

import (
	"github.com/twikey/go-logger/log"
)

func main() {
	log.Info("hello world")
}

// Output: ts=1729066152742 lvl=info msg="hello world"

Bring Your Own Writer

You can create a logger instance and specify the writer it should use for outputting the log lines.

package main

import (
	"os"

	"github.com/twikey/go-logger"
)

func main() {
	log := logger.New(os.Stdout)
	log.Info("hello world")
}

// Output: ts=1729066279358 logger=default lvl=info msg="hello world"

Customize Your Logger

Create a fully customized logger with all your preferred options and a custom Formatter.

package main

import (
	"os"

	"github.com/twikey/go-logger"
)

func main() {
	formatter := logger.NewTextFormatter()
	formatter.TimestampField = "" // disables timestamp printing

	log := logger.NewWithOptions(logger.Options{
		Name:      "my-logger",
		Writer:    os.Stdout,
		Level:     logger.LevelTrace, // explicit logger level overwrites logger.GlobalLevel
		Formatter: formatter,
	})

	log.Info("hello world")
}

// Output: logger=my-logger lvl=info msg="hello world"

Pretty Formatting

You can make use of pretty formatting in environments where optimisation is not crucial such as during development.

package main

import (
	"os"

	"github.com/twikey/go-logger"
)

func main() {
	formatter := logger.NewPrettyFormatter()
	log := logger.NewWithOptions(logger.Options{
		Writer:    os.Stdout,
		Formatter: formatter,
		Name:      "my-logger",
	})

	log.Info("hello world")
}

// Output: 2024-10-16 14:18:03 INF [default] hello world!

Overwrite Default Logger

You can overwrite the default logger if you prefer to use other settings

package main

import (
	"os"

	"github.com/twikey/go-logger"
	"github.com/twikey/go-logger/log"
)

func main() {
	formatter := logger.NewTextFormatter()
	formatter.LevelField = "l"
	formatter.TimestampField = "t"
	formatter.NameField = "n"

	log.SetDefaultLogger(logger.NewWithOptions(logger.Options{
		Name:      "default-logger",
		Writer:    os.Stdout,
		Level:     logger.LevelTrace, // explicit logger level overwrites logger.GlobalLevel
		Formatter: formatter,
	}))
	
	// assign to default logger
	log.Info("hello world")

	// clone logger with new name
	cloned := log.WithName("clone")
	cloned.Debug("hello from clone")
}

// Output: 
// t=1729067411654 n=default-logger l=info msg="hello world"
// t=1729067411654 n=clone l=debug msg="hello from clone"

Tests

Run:

  • make test to run all test.
  • make cov to run coverage tests.
  • make bench to run benchmark tests.

Benchmark Results

BenchmarkLogEmpty-8             53943338                22.97  ns/op          0   B/op          0 allocs/op
BenchmarkDisabled-8             1000000000              0.3272 ns/op          0   B/op          0 allocs/op
BenchmarkInfo-8                 47163553                24.77  ns/op          0   B/op          0 allocs/op
BenchmarkFormatted-8            18861511                68.44  ns/op          48  B/op          1 allocs/op
BenchmarkLoggerNew-8            1000000000              0.3190 ns/op          0   B/op          0 allocs/op
BenchmarkTextFormatter-8        53173220                21.42  ns/op          282 B/op          0 allocs/op

License

The BSD 3-Clause license, the same as the Go language.

About

A simple logger with support for leveled logging

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 99.2%
  • Makefile 0.8%