Skip to content

Simplest version of a customisable Go Logger.

License

Notifications You must be signed in to change notification settings

forsam-education/simplelogger

Repository files navigation

Go Simple Logger

CircleCI GoDoc Go Report Card License: MIT FOSSA Status

Background

While writing some lambdas in Golang, I found myself searching for a very light, adaptable logger that would fit for basic printing, elasticsearch indexing or any other logging system, with the most simple interface possible.

The result is this logger.

How to implement

This package exports a Logger struct defined like this (you'll find an interface representation just bellow):

package simplelogger

type LogExtraData map[string]interface{}

type Logger struct {
	formatter Formatter
	writer    Writer
	MinLevel  LogLevel
}

type _ interface {
	Log(level LogLevel, message string, data LogExtraData)
	Debug(message string, data LogExtraData)
	Info(message string, data LogExtraData)
	Warn(message string, data LogExtraData)
	Error(message string, data LogExtraData)
	Critical(message string, data LogExtraData)
	StdErrorCritical(err error, data LogExtraData)
	StdError(err error, data LogExtraData)
}

You can either use the DefaultLogger that logs to standard output with this format:

2019-07-16T17:07:46Z | [ERROR] | issou l'erreur | map[extra_data:data]

this way:

package yourpackage

logger := simplelogger.NewDefaultLogger()

Or you can implement your own writer and formatter according to these interfaces:

package simplelogger

type Formatter interface {
	Format(level LogLevel, message string, data map[string]interface{}) (string, error)
}

type Writer interface {
	Write(message string) error
}

Other Go Loggers

This is of course not an exhaustive list but here are some logger you may want to check out!

License

FOSSA Status