mimiclog

package module
v0.0.0-...-3dbd443 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 0 Imported by: 4

README

mimicry log

this module exists, because I wanted to have a basic abstraction of logging without any Implementation.

there might be similar modules out there, but I just don't find them. at least none that was matching my needs.

the idea

while moving some logic from the main project to independent modules, I wanted to avoid forcing a specific logger implementation. like logrus or zap. the main app was using logrus and the first step while moving to modules was just to remove any dependecies to logrus and decide later which logger to use.

all of them could also be done by just using the log package, but I wanted to have a bit more control over the log level and the output format. and also I wanted to have a bit more control over the logger implementation.

this idea is not a new one, and it exists in many other languages, and also in go. but what i found so far was different in "mindset" and in some points did not match my needs.

the needs
  • there should be no fancy stuff in there, and no new concepts.
  • no predefined Logger specific arguments, like logrus.Fields or zap.Field
  • no predefined Logger specific return values, like logrus.Entry or zap.SugaredLogger
  • no predefined Logger specific methods, like logrus.WithField or zap.Sugar
  • the implementation should be easy to replace
  • the implementation have to take care about any specific Logger arguments, return values and methods without exposing them to the user
  • the implementation have to handle all arguments and map them to the specific Logger arguments, return values and methods

the interface

logger methods

these methods are the basic logging methods. they are implemented by the specific logger implementation.

    Trace(args ...interface{})
    Debug(args ...interface{})
    Info(args ...interface{})
    Error(args ...interface{})
    Warn(args ...interface{})
    Critical(args ...interface{})
logger level checks

these methods are used to check if a specific level is enabled.

    IsLevelEnabled(level string) bool 
    IsTraceEnabled() bool
    IsDebugEnabled() bool
    IsInfoEnabled() bool
    IsWarnEnabled() bool
    IsErrorEnabled() bool
    IsCriticalEnabled() bool
logger level set/get methods

these methods are used to set and get the log level. the level is a string, and the implementation have to map it to the specific logger level.

    SetLevelByString(level string) 
    SetLevel(level interface{})
    GetLevel() string
logger getter

this method is used to get the specific logger implementation. the implementation have to return the specific logger implementation.

    GetLogger() interface{}    

Implemented

mimiclog is mainly a interface. there is only one implementation, and that is the NullLogger implementation. This is a logger that does nothing. it is used as default logger, and can be used as a fallback logger.

Apply helper function

a second interface defines how to apply a logger to a struct. the only needed method is SetLogger(logger mimiclog.Logger).

    type MimicLogUser interface {
        SetLogger(logger Logger)
    }

so then it is easy to apply the logger.

    logger := NewLogrusLogger()
    app := &myApp{}
    mimiclog.ApplyLogger(logger, app)

this can be also done by try and error. (for example some dynamic dependencies)

    logger := NewLogrusLogger()
    theLib := &someLib{}
    if ok := mimiclog.ApplyIfPossible(logger, theLib); !ok {
        logger.Warn(
            "'theLib' seems not supporting the 'mimiclog' interface"
        )
    }

Documentation

Index

Constants

View Source
const (
	LevelTrace    = "trace"
	LevelDebug    = "debug"
	LevelInfo     = "info"
	LevelWarn     = "warn"
	LevelError    = "error"
	LevelCritical = "critical"
	LevelNone     = "none"
)

Variables

This section is empty.

Functions

func ApplyIfPossible

func ApplyIfPossible(logger Logger, logUser interface{}) bool

just a helper function to apply a logger to a module that implements the MimicLogUser interface without knowing if the logUser implements the MimicLogUser interface returns true if the logger was applied, false if not

func ApplyLogger

func ApplyLogger(logger Logger, logUser MimicLogUser)

just a helper function to apply a logger to a module that implements the MimicLogUser interface

Types

type Fields

type Fields map[string]interface{}

mapppings for the logrus logger fields

type LogFunc

type LogFunc func(args ...interface{}) []interface{}

type Logger

type Logger interface {
	// the logging methods depending on the log level
	Trace(args ...interface{})
	Debug(args ...interface{})
	Info(args ...interface{})
	Error(args ...interface{})
	Warn(args ...interface{})
	Critical(args ...interface{})

	// maintain functions
	IsLevelEnabled(level string) bool // returns true if the given level is enabled
	IsTraceEnabled() bool             // returns true if trace is enabled
	IsDebugEnabled() bool             // returns true if debug is enabled
	IsInfoEnabled() bool              // returns true if info is enabled
	IsWarnEnabled() bool              // returns true if warn is enabled
	IsErrorEnabled() bool             // returns true if error is enabled
	IsCriticalEnabled() bool          // returns true if critical is enabled

	// the level functions
	SetLevelByString(level string) // sets the log level by a string like "trace", "debug", "info", "warn", "error", "critical"
	SetLevel(level interface{})    // set the log level by 'something' that is depending the logger implementation
	GetLevel() string              // returns the current log level as string

	GetLogger() interface{} // returns the logger instance
}

type MimicLogUser

type MimicLogUser interface {
	SetLogger(logger Logger)
}

If a module wants to use the mimiclog module, it has to implement this interface. So it can be used to inject the logger into the module, without changing any other interfaces they my be implemented by the module.

type MimicLogger

type MimicLogger interface {
	// Init initializes the logger.
	// Returns the logger instance and an error if something went wrong.
	Init(args ...interface{}) (*Logger, error)
	Name() string
}

type NullLogger

type NullLogger struct {
}

func NewNullLogger

func NewNullLogger() *NullLogger

func (*NullLogger) Critical

func (l *NullLogger) Critical(args ...interface{})

func (*NullLogger) Debug

func (l *NullLogger) Debug(args ...interface{})

func (*NullLogger) Error

func (l *NullLogger) Error(args ...interface{})

func (*NullLogger) GetLevel

func (l *NullLogger) GetLevel() string

func (*NullLogger) GetLogger

func (l *NullLogger) GetLogger() interface{}

func (*NullLogger) Info

func (l *NullLogger) Info(args ...interface{})

func (*NullLogger) IsCriticalEnabled

func (l *NullLogger) IsCriticalEnabled() bool

func (*NullLogger) IsDebugEnabled

func (l *NullLogger) IsDebugEnabled() bool

func (*NullLogger) IsErrorEnabled

func (l *NullLogger) IsErrorEnabled() bool

func (*NullLogger) IsInfoEnabled

func (l *NullLogger) IsInfoEnabled() bool

func (*NullLogger) IsLevelEnabled

func (l *NullLogger) IsLevelEnabled(level string) bool

func (*NullLogger) IsTraceEnabled

func (l *NullLogger) IsTraceEnabled() bool

func (*NullLogger) IsWarnEnabled

func (l *NullLogger) IsWarnEnabled() bool

func (*NullLogger) SetLevel

func (l *NullLogger) SetLevel(level interface{})

func (*NullLogger) SetLevelByString

func (l *NullLogger) SetLevelByString(level string)

func (*NullLogger) SetOutput

func (l *NullLogger) SetOutput(output interface{})

func (*NullLogger) Trace

func (l *NullLogger) Trace(args ...interface{})

func (*NullLogger) Warn

func (l *NullLogger) Warn(args ...interface{})

Jump to

Keyboard shortcuts

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