loggers

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2018 License: MIT Imports: 0 Imported by: 39

README

loggers : Golang Abstract Loggers

loggers define an abstract and common logging interface in three flavors.

GoDoc Build Status

Inspiration

If you have been using Go for a while, you've probably been asking yourself: "Why, oh why, wasn't the standard library logger made an interface!?" Often was I faced with having to decide about what kind of logger I needed before I was ready to, wondering:

  • Where's Golang's answer to log4j ?
  • Is there a log4go or log4golang ?

Well this package should help. Install and call log.Info("Log all my stuff") and you're off and you can easily switch out loggers later with only a single line of code.

Design

All loggers are interfaces and should be declared and used as such. The actual implementations can vary and are easily switched out. The main packages should have no external dependencies. See here.

Standard

Standard interface is the same as used by the Go standard library log package.

Advanced

A common pattern for level discrete loggers using debug, info, warn and error levels, along with those defined by the standard interface.

Contextual

A superset of Advanced, adds contextual logging, such that lines can have a number of additional parameters set to provide clearer separation of message and context.

Installation

go get gopkg.in/birkirb/loggers.v1

Usage

You can choose between declaring one of the three interfaces above that best suits your needs, or use the built in Contextual interface directly with a default standard library logger implementation to do your bidding. You can switch out all the loggers later as long as they satisfy the right interface.

Direct

You can use the loggers interface as a drop in replacement for the standard library logger. Just change your import statement from "log" to "gopkg.in/birkirb/loggers.v1/log". It should work just the same and you can make use of advanced and contextual methods only if you so decide. You can then easily switch out the log package implementation later with your own logger as long as it implements the Contextual interface.

    log.Infof("Logger is started") // Defaults to stdout.
    log.Logger = stdlib.NewLogger(fileWriter, "", log.LstdFlags)
    log.Infof("Now logging to fileWriter") // writes to fileWriter
Embedded

Declare your own project logging interface.

import (
    "log"

    "gopkg.in/birkirb/loggers.v1"
)

var Logger loggers.Standard

func init() {
    Logger = log.New(writer, "myapp", log.LstdFlags)
    Logger.Println("Logger is started")
}
Mappers

A few loggers have been mapped to the above interfaces and could thus be used with any of them. Instead of the using the Standard logger as above, we could use the standard logger much like a leveled logger.

import (
    "gopkg.in/birkirb/loggers.v1"
    "gopkg.in/birkirb/loggers.v1/mappers/stdlib"
)

var Logger loggers.Advanced

func init() {
    Logger = stdlib.NewDefaultLogger()
    Logger.Info("Logger is started")
}

A level mapper exist to ease with implementing plugins/mappers for other loggers that don't naturally implement any of the designed interfaces. This can be found in the mappers package.

Existing mappers

Contributing

Any new mappers for different Go logging solutions would be most welcome.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advanced

type Advanced interface {
	Standard

	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
	Debugln(args ...interface{})

	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Errorln(args ...interface{})

	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Infoln(args ...interface{})

	Warn(args ...interface{})
	Warnf(format string, args ...interface{})
	Warnln(args ...interface{})
}

Advanced is an interface with commonly used log level methods.

type Contextual

type Contextual interface {
	Advanced

	WithField(key string, value interface{}) Advanced
	WithFields(fields ...interface{}) Advanced
}

Contextual is an interface that allows context addition to a log statement before calling the final print (message/level) method.

type Standard

type Standard interface {
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Fatalln(args ...interface{})

	Panic(args ...interface{})
	Panicf(format string, args ...interface{})
	Panicln(args ...interface{})

	Print(args ...interface{})
	Printf(format string, args ...interface{})
	Println(args ...interface{})
}

Standard is the interface used by Go's standard library's log package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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