metalog

package module
v0.0.0-...-73f3cc7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2019 License: MIT Imports: 0 Imported by: 0

README

metalog

Metalog is a logging interface with as few dependencies as possible.

logrus is a great tool, but with one flaw: it doesn't have a "pure interface"

example: the FieldLogger: https://godoc.org/github.com/sirupsen/logrus#FieldLogger

type FieldLogger interface {
    WithField(key string, value interface{}) *logrus.Entry
    WithFields(fields logrus.Fields) *logrus.Entry
    ...
}

this interface is tied to the logrus package via logrus.Entry and logrus.Fields, so if you want to accept that interface in your library you end up with a dependency on the logrus package. That kinda defeats the point of having an interface.

Fortunately this problem can be easily solved:

  • logrus.Fields is just a map[string]interface{}
  • logrus.Entry is just a logrus.FieldLogger

so it's pretty trivial to create a thin compatibility layer over logrus in order to reach this interface:

type FieldLogger interface {
	WithField(string, interface{}) Logger
	WithFields(map[string]interface{}) Logger
}

This interface allows your library to accept a Logger without having a dependency directly on logrus.

Clients that want to use your library can just wrap logrus with this client that's so tiny I can paste it here

type Logger struct {
	logrus.FieldLogger
}

func (l *Logger) WithField(field string, value interface{}) FieldLogger {
	return &Logger{
		FieldLogger: l.FieldLogger.WithField(field, value),
	}
}

func (l *Logger) WithFields(fields map[string]interface{}) FieldLogger {
	return &Logger{
		FieldLogger: l.FieldLogger.WithFields(fields),
	}
}

Of course you can use directly the metalogrus package for convenience

Documentation

Overview

package metalog implements a logging interface that's similar to logrus but doesn't actually depends on the logrus package

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Debug(args ...interface{})
	Info(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	WithField(string, interface{}) Logger
	WithFields(map[string]interface{}) Logger
}

Logger is a logging interface that allows to write to the 4 most common levels and to add fields to the logger

Directories

Path Synopsis
Package logrus is a thin wrapper around logrus, providing a way to instantiate it with formatter and
Package logrus is a thin wrapper around logrus, providing a way to instantiate it with formatter and

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL