logger

package
v0.0.0-...-3215105 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2018 License: MIT Imports: 6 Imported by: 0

README

logger

package logger contains LoggerWrapper, BasicLogger and logging interfaces.

LoggerWrapper

In order not to extort any specific logging package, a logger wraper has been created. LoggerWrapper wraps around third-party loggers that implement one of the logging-interfaces:

	- StdLogger - standard library logger interface
	- LeveledLogger - basic leveled logger interface
	- ShortLeveledLogger - basic leveled logger interfaces with shortened method names
	- ExtendedLeveledLogger - a fully leveled logger interface

This solution allows to use ExtendedLeveledLogger interface methods for most of the third-party logging packages.

Wrapping third-party logger
import (
	"github.com/kucjac/go-rest-sdk/logger"
	"some/loggingpkg"
)

func main(){
	// Having a third-party logger that implements any of the package interfaces
	var myLogger *logginpkg.Logger = loggingpkg.New()

	// If 'myLogger' doesn't implement 'ExtendedLeveledLogger' but any generic function
	// uses logs of that interface, it can be wrapped using LoggerWrapper.

	// if 'myLogger' doesn't implement any of the listed interfaces MustGetLoggingWrapper would panic.
	var wrappedLoggerMust *logger.LoggerWrapper = logger.MustGetLoggerWrapped(myLogger)

	// The other function to get a logging wrapper is NewLoggingWrapper(myLogger) which returns 
	// new *LoggerWrapper or an error if it doesn't implement listed interfaces.
	var wrappedLoggerNew *logger.LoggerWrapper
	var err error
	wrappedLoggerNew, err = logger.NewLoggingWrapper(myLogger)
	if err != nil {
		...
	}

	wrappedLoggerNew.Infoln("It works!")
}
BasicLogger

The package contains also BasicLogger that implements 'LeveledLogger' interface. It is very simple and lightweight implementation of leveled logger.

import (
	"log"
	"os"
	"github.com/kucjac/go-rest-sdk/logger"
)
		


func main(){
	// BasicLogger is simple leveled logger that implements LeveledLogger interface.
	var basicLogger *logger.BasicLogger

	// In order to get new basic logger use NewBasicLogger() function
	basicLogger = logger.NewBasicLogger(os.Stderr, "", log.Ltime)

	// BasicLogger implements LeveledLogger interface
	basicLogger.Error("This should log an error.")
}
Log Levels

The package uses 6 basic log levels.

// Level defines a logging level used in BasicLogger
type Level int

// Following levels are supported in BasicLogger
const (
	DEBUG Level = iota
	INFO
	WARNING
	ERROR
	CRITICAL
	PRINT
)

The BasicLogger allows to set a logging level so that no lower level logs would be printed. This allows to control the logging output just for specified level (or higher).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicLogger

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

BasicLogger is simple leveled logger that implements LeveledLogger interface. It uses 5 basic log levels:

# DEBUG
# INFO
# WARNING
# ERROR
# CRITICAL

By default DEBUG level is used. It may be reset using SetLevel() method. It allows to filter the logs by given level. I.e. Having BasicLogger with level Set to WARNING, then there would be no DEBUG and INFO logs (the hierarchy goes up only).

func NewBasicLogger

func NewBasicLogger(out io.Writer, prefix string, flags int) *BasicLogger

NewBasicLogger creates new BasicLogger that shares common sequence id. By default it uses DEBUG level. It can be changed later using SetLevel() method. BasicLogger uses standard library *log.Logger for logging purpose. The arguments used in this function are described in log.New() method.

func (*BasicLogger) Debug

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

Logs a message with DEBUG level.

func (*BasicLogger) Debugf

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

Logs a formatted message with DEBUG level

func (*BasicLogger) Error

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

Logs a message with ERROR level. Arguments are handled in a log.Print manner.

func (*BasicLogger) Errorf

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

Logs a formatted message with ERROR level. Arguments are handled in a log.Printf manner.

func (*BasicLogger) Fatal

func (l *BasicLogger) Fatal(args ...interface{})

Logs a message with CRITICAL level. Afterwards the function execute os.Exit(1). Arguments are handled in a log.Print manner.

func (*BasicLogger) Fatalf

func (l *BasicLogger) Fatalf(format string, args ...interface{})

Logs a formatted message with CRITICAL level. Afterwards the function execute os.Exit(1). Arguments are handled in a log.Printf manner.

func (*BasicLogger) Info

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

Logs a message with INFO level

func (*BasicLogger) Infof

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

Logs a formatted message with INFO level.

func (*BasicLogger) Panic

func (l *BasicLogger) Panic(args ...interface{})

Logs a message with CRITICAL level. Afterwards the function panics with given message. Arguments are handled in a log.Print manner.

func (*BasicLogger) Panicf

func (l *BasicLogger) Panicf(format string, args ...interface{})

Logs a formatted message with CRITICAL level. Afterwards the function panics with given formatted message. Arguments are handled in a log.Printf manner.

func (*BasicLogger) Print

func (l *BasicLogger) Print(args ...interface{})

Logs a message. Arguments are handled in a log.Print manner.

func (*BasicLogger) Printf

func (l *BasicLogger) Printf(format string, args ...interface{})

Logs a formatted message. Arguments are handled in a log.Printf manner.

func (*BasicLogger) SetLevel

func (l *BasicLogger) SetLevel(level Level)

SetLevel sets the level of logging for given Logger.

func (*BasicLogger) Warning

func (l *BasicLogger) Warning(args ...interface{})

Logs a message with WARNING level. Arguments are handled in a log.Print manner.

func (*BasicLogger) Warningf

func (l *BasicLogger) Warningf(format string, args ...interface{})

Logs a formatted message with WARNING level. Arguments are handled in a log.Printf manner.

type ExtendedLeveledLogger

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

	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})

	Debug(args ...interface{})
	Info(args ...interface{})
	Warning(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})

	Debugln(args ...interface{})
	Infoln(args ...interface{})
	Warningln(args ...interface{})
	Errorln(args ...interface{})
	Fatalln(args ...interface{})
	Panicln(args ...interface{})
}

ExtendedLogger adds distinction between Leveled methods that starts new or not i.e.: 'Debugln' and 'Debug'. It also adds all Print's methods.

type Level

type Level int

*

Levels

Level defines a logging level used in BasicLogger

const (
	DEBUG Level = iota
	INFO
	WARNING
	ERROR
	CRITICAL
	PRINT
)

Following levels are supported in BasicLogger

func (Level) String

func (l Level) String() string

type LeveledLogger

type LeveledLogger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})

	Debug(args ...interface{})
	Info(args ...interface{})
	Warning(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})
}

LeveledLogger is a logger that uses basic logging leveles

type LoggerWrapper

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

LoggerWrapper is wrapper around any third-party logger that implements any of the following interfaces:

# ExtendedLeveledLogger
# ShortLeveledLogger
# LeveledLogger
# StdLogger

By wrapping the logger it implements ExtendedLeveledLogger. For loggers that implements only StdLogger, LoggerWrapper tries to virtualize leveled logger behaviour. It simply adds level name before logging message. If a logger implements LeveledLogger that doesn't have specific log line '****ln()' methods, it uses default non 'ln' functions - i.e. instead 'Infoln' uses 'Info'.

func MustGetLoggerWrapper

func MustGetLoggerWrapper(logger interface{}) *LoggerWrapper

MustGetLoggerWrapper creates a LoggerWrapper wrapper over provided 'logger' argument. By default the function checks if provided logger implements logging interfaces in a following hierarchy:

# ExtendedLeveledLogger
# ShortLeveledLogger
# LeveledLogger
# StdLogger

if logger doesn't implement an interface it tries to check the next in hierarchy. If it doesn't implement any of known logging interfaces the function panics.

func NewLoggerWrapper

func NewLoggerWrapper(logger interface{}) (*LoggerWrapper, error)

NewLoggerWrapper creates a LoggerWrapper wrapper over provided 'logger' argument By default the function checks if provided logger implements logging interfaces in a following hierarchy:

# ExtendedLeveledLogger
# ShortLeveledLogger
# LeveledLogger
# StdLogger

if logger doesn't implement an interface it tries to check the next in hierarchy. If it doesn't implement any of known logging interfaces the function returns error.

func (*LoggerWrapper) Debug

func (c *LoggerWrapper) Debug(args ...interface{})

Debug logs a message with DEBUG level. Arguments are handled in the manner of log.Print for StdLogger, log.Debug for ExtendedLeveledLogger and LeveledLogger.

func (*LoggerWrapper) Debugf

func (c *LoggerWrapper) Debugf(format string, args ...interface{})

Debugf logs a formatted message with DEBUG level. Arguments are handled in the manner of log.Printf for StdLogger, log.Debugf for ExtendedLeveledLogger, ShortLeveledLogger and LeveledLogger.

func (*LoggerWrapper) Debugln

func (c *LoggerWrapper) Debugln(args ...interface{})

Debugln logs a message with DEBUG level. Arguments are handled in the manner of log.Println for StdLogger, log.Debugln for ExtendedLeveledLogger and log.Debug for LeveledLogger and ShortLeveledLogger.

func (*LoggerWrapper) Error

func (c *LoggerWrapper) Error(args ...interface{})

Error logs a message with ERROR level. Arguments are handled in the manner of log.Print for StdLogger, log.Error for ExtendedLeveledLogger, LeveledLogger and ShortLeveledLogger.

func (*LoggerWrapper) Errorf

func (c *LoggerWrapper) Errorf(format string, args ...interface{})

Errorf logs a formatted message with ERROR level. Arguments are handled in the manner of log.Printf for StdLogger, log.Errorf for ExtendedLeveledLogger, LeveledLogger and ShortLeveledLogger.

func (*LoggerWrapper) Errorln

func (c *LoggerWrapper) Errorln(args ...interface{})

Errorln logs a message with ERROR level. Arguments are handled in the manner of log.Println for StdLogger, log.Debugln for ExtendedLeveledLogger and log.Error for LeveledLogger and ShortLeveledLogger.

func (*LoggerWrapper) Fatal

func (c *LoggerWrapper) Fatal(args ...interface{})

Fatal logs a message with CRITICAL level. Afterwards it should excute os.Exit(1). Arguments are handled in the manner of log.Fatal for StdLogger, LeveledLogger, ShortLeveledLogger and ExtendedLeveledLogger.

func (*LoggerWrapper) Fatalf

func (c *LoggerWrapper) Fatalf(format string, args ...interface{})

Fatalf logs a formatted message with CRITICAL level. Afterwards it should excute os.Exit(1). Arguments are handled in the manner of log.Fatalf for StdLogger, LeveledLogger, ShortLeveledLogger and ExtendedLeveledLogger.

func (*LoggerWrapper) Fatalln

func (c *LoggerWrapper) Fatalln(args ...interface{})

Fatalln logs a message with CRITICAL level. Afterwards it should excute os.Exit(1). Arguments are handled in the manner of log.Fatalln for StdLogger and ExtendedLeveldLogger, and log.Fatal for LeveledLogger and ShortLeveledLogger.

func (*LoggerWrapper) Info

func (c *LoggerWrapper) Info(args ...interface{})

Info logs a message with INFO level. Arguments are handled in the manner of log.Print for StdLogger, log.Info for ExtendedLeveledLogger, ShortLeveledLogger and LeveledLogger.

func (*LoggerWrapper) Infof

func (c *LoggerWrapper) Infof(format string, args ...interface{})

Infof logs a formatted message with INFO level. Arguments are handled in the manner of log.Printf for StdLogger, log.Infof for ExtendedLeveledLogger, ShortLeveledLogger and LeveledLogger.

func (*LoggerWrapper) Infoln

func (c *LoggerWrapper) Infoln(args ...interface{})

Infoln logs a message with INFO level. Arguments are handled in the manner of log.Println for StdLogger, log.Infoln for ExtendedLeveledLogger and log.Info for LeveledLogger and ShortLeveledLogger.

func (*LoggerWrapper) Panic

func (c *LoggerWrapper) Panic(args ...interface{})

Panic logs a message with CRITICAL level. Afterwards it should panic. Arguments are handled in the manner of log.Panic for StdLogger, LeveledLogger, ShortLeveledLogger and ExtendedLeveledLogger .

func (*LoggerWrapper) Panicf

func (c *LoggerWrapper) Panicf(format string, args ...interface{})

Panifc logs a formatted message with CRITICAL level. Afterwards it should panic. Arguments are handled in the manner of log.Panicf for StdLogger, LeveledLogger, ShortLeveledLogger and ExtendedLeveledLogger.

func (*LoggerWrapper) Panicln

func (c *LoggerWrapper) Panicln(args ...interface{})

Panicln logs a message with CRITICAL level. Afterwards it should panic. Arguments are handled in the manner of log.Panicln for StdLogger and ExtendedLeveledLogger, and log.Panic LeveledLogger and ShortLeveledLogger.

func (*LoggerWrapper) Print

func (c *LoggerWrapper) Print(args ...interface{})

Print logs a message. Arguments are handled in the manner of log.Print for StdLogger and Extended LeveledLogger as well as log.Info for LeveledLogger

func (*LoggerWrapper) Printf

func (c *LoggerWrapper) Printf(format string, args ...interface{})

Printf logs a formatted message. Arguments are handled in the manner of log.Printf for StdLogger and Extended LeveledLogger as well as log.Infof for LeveledLogger

func (*LoggerWrapper) Println

func (c *LoggerWrapper) Println(args ...interface{})

Println logs a message. Arguments are handled in the manner of log.Println for StdLogger and Extended LeveledLogger as well as log.Info for LeveledLogger

func (*LoggerWrapper) Warning

func (c *LoggerWrapper) Warning(args ...interface{})

Warning logs a message with WARNING level. Arguments are handled in the manner of log.Print for StdLogger, log.Warning for ExtendedLeveledLogger, LeveledLogger and log.Warn for ShortLeveledLogger.

func (*LoggerWrapper) Warningf

func (c *LoggerWrapper) Warningf(format string, args ...interface{})

Warningf logs a formatted message with WARNING level. Arguments are handled in the manner of log.Printf for StdLogger, log.Warningf for ExtendedLeveledLogger, LeveledLogger and log.Warnf for ShortLeveledLogger.

func (*LoggerWrapper) Warningln

func (c *LoggerWrapper) Warningln(args ...interface{})

Warningln logs a message with WARNING level. Arguments are handled in the manner of log.Println for StdLogger, log.Warningln for ExtendedLeveledLogger, log.Warning for LeveledLogger and log.Warn for ShortLeveledLogger.

type Message

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

Message is a basic logging record structure used in BasicLogger

func (*Message) Message

func (m *Message) Message() string

Message prepares the string message based on the format and args private fields of the message

func (*Message) String

func (m *Message) String() string

String returns string that concantates: id hash - 4 digits|time formatted in RFC339|level|message

type ShortLeveledLogger

type ShortLeveledLogger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})

	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})
}

ShortLeveledLogger is a logger that uses basic logging leveles with short name for Warn

type StdLogger

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

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

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

StdLogger is the logger interface for standard log library

Jump to

Keyboard shortcuts

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