deplog

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2018 License: BSD-3-Clause Imports: 3 Imported by: 0

README

DepLog

forthebadgeforthebadge

Health

Branch Build status Code climate Go Report Card
master CircleCI Maintainability Go Report Card

About

As I've been writing other libraries/modules, I noticed the need to support injecting a logger such that the log files can be somewhat consistent for a user. However, there seems to be many different loggers out there and no standard interface. This project is not another logger, it is a wrapper. A wrapper for any kind of logger out there (although, it needs more testing).

DepLog identifies the single logging methods that are commonly used for any logger injected. It then creates a profile which is used to link the DepLog logger methods to the methods of the injected logger. It also supports writing profiles for any logger out there and customize behaviour.

The goal of this project is to remove the habit of forcing users of libraries to utilize one specific logger, and let them choose which ever they prefer, while still keeping a general yet expressive logger interface for the library developers.

Setup

*Note: that DepLog does not have a exported package variable to store your logger instance at. If you do want this, I recommend creating your own sub-package called logger and keep all your logger configuration in there.

// let's pretend the injected logger only has .Debug as a method
// this will create a profile showing it has a Debug method only
// profile == deplog.FlagDebug
// this profile is used later in rerouting

// create a deplogger instance which has mapped the default logger behaviour
logger, err := NewDepLogger(someRandomLoggerInstance)
if err != nil {
  panic(err) // someRandomLoggerInstance was nil
}
deplog.SetupDefaultRoutes(logger)

logger.Debug("test") // will trigger someRandomLoggerInstance.Debug(...)
logger.Info("yep") // not called, as the injected logger is missing Info method

// let's route the Info method to the .Debug method
logger.Route(deplog.FlagDebug, FlagInfo, FlagDebug)
logger.Info("this prints!") // triggers someRandomLoggerInstance.Debug(...)

This might be a little confusing. It's important to know that calling NewDepLogger will initialize a default relationship, where methods missing in the injected logger, are simply ignored. However, as we saw, we can route deplog methods to another logger method as desired per profile using the DepLog.Route option.

DepLog.Route takes tree parameters:

  1. profile: the profile for a specific logger
  2. src: the method or methods to be rerouted
  3. dest: the method to which src are routed

The reason I used deplog.FlagDebug, is because it is the specific profile for any logger that only has a .Debug method.

You can also create more advanced behaviour using the deplog.BindRoutes(...) which takes the DepLog instance with an array of logger levels (info, debug, etc.). It is here you decided which logger level should be bound to which logger method. Here I show the default network. The first line of the map tells us that when we run a DepLog.Info method, it should be routed to first found logger method that is either: .Info(string), .Info(...interface{}), .Print(string), .Print(...interface{}). While DepLog.Debugf is only routed to .Debugf(string, ...interface{})

levels := map[uint64]uint64{
  FlagInfo:   FlagInfo | FlagInfoa | FlagPrint | FlagPrinta,
  FlagInfof:  FlagInfof | FlagPrintf,
  FlagWarn:   FlagWarn | FlagWarning | FlagWarna | FlagWarninga,
  FlagWarnf:  FlagWarnf | FlagWarningf,
  FlagDebug:  FlagDebug | FlagDebuga,
  FlagDebugf: FlagDebugf,
  FlagError:  FlagError | FlagErrora,
  FlagErrorf: FlagErrorf,
  FlagCrit:   FlagCrit | FlagCritic | FlagCritical | FlagCrita | FlagCritica | FlagCriticala,
  FlagCritf:  FlagCritf | FlagCriticf | FlagCriticalf,
}
deplog.BindRoutes(deplogger, deplogger.profile, levels)

*Note: the use of deplogger.profile in the BindRoutes function, causes the network to bind to any kind of profile, and is not limited to say just a logrus profile.

When should I use which logger level?

Either refer to the way the different loggers utilise them, or check out similar discussion in Java. https://stackoverflow.com/questions/5817738/how-to-use-log-levels-in-java

I know this is not Java, but the logging concept of levels is can be reused as they are based in social understanding of the words and not uniquely defined by a programming language.

TODO

The goal is also to create network profiles for each of the most commonly used loggers out there (logrus, go-logger, google/logger, etc.), and make sure these route the methods in a intuitive way as possible so you can use DepLog right out of the box.

Documentation

Index

Constants

View Source
const (
	FlagDebug uint64 = 1 << iota
	FlagDebuga
	FlagDebugf
	FlagInfo
	FlagInfoa
	FlagInfof
	FlagWarning
	FlagWarninga
	FlagWarningf
	FlagWarn
	FlagWarna
	FlagWarnf
	FlagError
	FlagErrora
	FlagErrorf
	FlagCritical
	FlagCriticala
	FlagCriticalf
	FlagCritic
	FlagCritica
	FlagCriticf
	FlagCrit
	FlagCrita
	FlagCritf
	FlagPrint
	FlagPrinta
	FlagPrintf
	FlagNotice
	FlagNoticea
	FlagNoticef
)
View Source
const (
	ProfileLogrus uint64 = 0
)

Variables

This section is empty.

Functions

func BindRoutes

func BindRoutes(l *DepLog, profile uint64, levels map[uint64]uint64)

BindRoutes will bind the existing logger methods in the injected logger with the given map. This map tells which of the DepLog methods should be routed to which injected logger methods, or ignore if they don't exist. see SetupDefaultRoutes(...) as an example

func SetupDefaultRoutes

func SetupDefaultRoutes(l *DepLog)

SetupDefaultRoutes confiure the DepLog to only use logger methods that exists in the injected logger. If they do not exist, logging is ignored.

Types

type ArgsCriter

type ArgsCriter interface {
	Crit(args ...interface{})
}

type ArgsCriticaler

type ArgsCriticaler interface {
	Critical(args ...interface{})
}

type ArgsCriticer

type ArgsCriticer interface {
	Critic(args ...interface{})
}

type ArgsDebugger

type ArgsDebugger interface {
	Debug(args ...interface{})
}

type ArgsErrorer

type ArgsErrorer interface {
	Error(args ...interface{})
}

type ArgsInfoer

type ArgsInfoer interface {
	Info(args ...interface{})
}

type ArgsNoticer

type ArgsNoticer interface {
	Notice(args ...interface{})
}

type ArgsWarner

type ArgsWarner interface {
	Warn(args ...interface{})
}

type ArgsWarninger

type ArgsWarninger interface {
	Warning(args ...interface{})
}

type Criter

type Criter interface {
	Crit(message string)
}

type Criticaler

type Criticaler interface {
	Critical(message string)
}

type Criticer

type Criticer interface {
	Critic(message string)
}

type Debugger

type Debugger interface {
	Debug(message string)
}

type DepLog

type DepLog struct {
	// contains filtered or unexported fields
}

func (*DepLog) Crit

func (l *DepLog) Crit(message string)

func (*DepLog) Critf

func (l *DepLog) Critf(format string, args ...interface{})

func (*DepLog) Debug

func (l *DepLog) Debug(message string)

func (*DepLog) Debugf

func (l *DepLog) Debugf(format string, args ...interface{})

func (*DepLog) Error

func (l *DepLog) Error(message string)

func (*DepLog) Errorf

func (l *DepLog) Errorf(format string, args ...interface{})

func (*DepLog) Info

func (l *DepLog) Info(message string)

func (*DepLog) Infof

func (l *DepLog) Infof(format string, args ...interface{})

func (*DepLog) Route

func (l *DepLog) Route(loggerProfile uint64, depLogMethods uint64, injectedMethod uint64) *DepLog

Route overwrites existing default routes for s specific logger type such as logrus eg. DepLog.Route(profileLogrus, FlagDebugf, FlagFatalf) will bind the DepLog.Debugf to injected.Fatalf, if the injected logger has the matching profile

Yes, another injected logger migth have the same profile as logrus, in that case the routes will still take affect. This only cares about the profile, not the GoLang type

func (*DepLog) Warn

func (l *DepLog) Warn(message string)

func (*DepLog) Warnf

func (l *DepLog) Warnf(format string, args ...interface{})

type Errorer

type Errorer interface {
	Error(message string)
}

type FormatCriter

type FormatCriter interface {
	Critf(format string, args ...interface{})
}

type FormatCriticaler

type FormatCriticaler interface {
	Criticalf(format string, args ...interface{})
}

type FormatCriticer

type FormatCriticer interface {
	Criticf(format string, args ...interface{})
}

type FormatDebugger

type FormatDebugger interface {
	Debugf(format string, args ...interface{})
}

type FormatErrorer

type FormatErrorer interface {
	Errorf(format string, args ...interface{})
}

type FormatInfoer

type FormatInfoer interface {
	Infof(format string, args ...interface{})
}

type FormatNoticer

type FormatNoticer interface {
	Noticef(format string, args ...interface{})
}

type FormatWarner

type FormatWarner interface {
	Warnf(format string, args ...interface{})
}

type FormatWarninger

type FormatWarninger interface {
	Warningf(format string, args ...interface{})
}

type Infoer

type Infoer interface {
	Info(message string)
}

type Logger

type Logger interface {
	Info(message string)
	Infof(format string, args ...interface{})
	Warn(message string)
	Warnf(format string, args ...interface{})
	Debug(message string)
	Debugf(format string, args ...interface{})
	Error(message string)
	Errorf(message string, args ...interface{})
	Crit(message string)
	Critf(message string, args ...interface{})
}

func NewDepLogger

func NewDepLogger(injected interface{}) (logger Logger, err error)

type Noticer

type Noticer interface {
	Notice(message string)
}

type Warner

type Warner interface {
	Warn(message string)
}

type Warninger

type Warninger interface {
	Warning(message string)
}

Jump to

Keyboard shortcuts

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