Skip to content

Commit

Permalink
simplified logger abstraction
Browse files Browse the repository at this point in the history
added example
unified to one logger
  • Loading branch information
mantzas committed Mar 7, 2016
1 parent 5e261b5 commit 5426523
Show file tree
Hide file tree
Showing 6 changed files with 551 additions and 564 deletions.
45 changes: 7 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

Package adaptlog is a logging abstraction for go. The name of the package is a composition of adapt(adaptive) and log(logging).

The developer uses this abstraction in order to avoid depending on a specific logging implementation. The package provides two abstractions:
The developer uses this abstraction in order to avoid depending on a specific logging implementation.
The package provides a abstraction that covers the standard logging(like in the standard log package) and the leveled logging (like many of them out there).

* The standard logger based on the standard log package and the leveled logger based on many leveled logging packages out there.
* The developer has only to setup either one by implementing a interface.

The simplest way to use adaptlog's standard logger is by simply implementing the StdLogger interface like below.
The simplest way to use adaptlog's logger is by simply implementing the Logger interface like illustarted in the samples of the examples folder.

package main

Expand All @@ -35,42 +33,13 @@ The simplest way to use adaptlog's standard logger is by simply implementing the
func (l *MyLogger) Println(args ...interface{}) {
log.Println(args...)
}

// Panic logging
func (l *MyLogger) Panic(args ...interface{}) {
log.Panic(args...)
}

// Panicf logging
func (l *MyLogger) Panicf(msg string, args ...interface{}) {
log.Panicf(msg, args...)
}

// Panicln logging
func (l *MyLogger) Panicln(args ...interface{}) {
log.Panicln(args...)
}

// Fatal logging
func (l *MyLogger) Fatal(args ...interface{}) {
log.Panic(args...)
}

// Fatalf logging
func (l *MyLogger) Fatalf(msg string, args ...interface{}) {
log.Panicf(msg, args...)
}

// Fatalln logging
func (l *MyLogger) Fatalln(args ...interface{}) {
log.Panicln(args...)
}
...

func main() {

// configure once
adaptlog.ConfigStandardLogger(new(MyLogger))
adaptlog.Configure(new(MyLogger), adaptlog.AnyLevel)

// use logger
Logger.Print("Hello World!")
// use logger
adaptlog.Print("Hello World!")
}
243 changes: 203 additions & 40 deletions adaptlog.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,74 @@
package adaptlog

var level LogLevel
var log Logger

// Configure the internal logger
func Configure(logger Logger, logLevel LogLevel) {
log = logger
level = logLevel
}

// LogLevel type
type LogLevel int

// These logging levels are used to setup the logging
const (
// Panic level
PanicLevel LogLevel = iota
// Fatal level
FatalLevel
// Error level
ErrorLevel
// Warn level
WarnLevel
// Info level
InfoLevel
// Debug level
DebugLevel
// Any level
AnyLevel
)

// Logger interface. Introduces Error, Warn, Info and Debug logging facilities.
type Logger interface {
FatalLogger
PanicLogger
ErrorLogger
WarnLogger
InfoLogger
DebugLogger
PrintLogger
}

// ErrorLogger interface. Introduces Error logging facilities.
type ErrorLogger interface {
Error(...interface{})
Errorf(string, ...interface{})
Errorln(...interface{})
}

// WarnLogger interface. Introduces Warn logging facilities.
type WarnLogger interface {
Warn(...interface{})
Warnf(string, ...interface{})
Warnln(...interface{})
}

// InfoLogger interface. Introduces Info logging facilities.
type InfoLogger interface {
Info(...interface{})
Infof(string, ...interface{})
Infoln(...interface{})
}

// DebugLogger interface. Introduces Debug logging facilities.
type DebugLogger interface {
Debug(...interface{})
Debugf(string, ...interface{})
Debugln(...interface{})
}

// PrintLogger interface. Introduces Print logging facilities.
type PrintLogger interface {
Print(...interface{})
Expand All @@ -21,67 +90,161 @@ type PanicLogger interface {
Panicln(...interface{})
}

// StdLogger interface
type StdLogger interface {
PrintLogger
FatalLogger
PanicLogger
// Print logging
func Print(args ...interface{}) {
log.Print(args...)
}

// Printf logging with message
func Printf(msg string, args ...interface{}) {
log.Printf(msg, args...)
}

// StandardLogger structure
type StandardLogger struct {
logger StdLogger
// Println logging with new line
func Println(args ...interface{}) {
log.Println(args...)
}

// Logger standard logger instance
var Logger StandardLogger
// Panic level logging
func Panic(args ...interface{}) {
if level < PanicLevel {
return
}
log.Panic(args...)
}

// ConfigStandardLogger configures a standard logger
func ConfigStandardLogger(logger StdLogger) {
Logger = StandardLogger{logger}
// Panicf level logging with message
func Panicf(msg string, args ...interface{}) {
if level < PanicLevel {
return
}
log.Panicf(msg, args...)
}

// Print logging
func (l *StandardLogger) Print(args ...interface{}) {
l.logger.Print(args...)
// Panicln level logging with new line
func Panicln(args ...interface{}) {
if level < PanicLevel {
return
}
log.Panicln(args...)
}

// Printf logging with message
func (l *StandardLogger) Printf(msg string, args ...interface{}) {
l.logger.Printf(msg, args...)
// Fatal level logging
func Fatal(args ...interface{}) {
if level < FatalLevel {
return
}
log.Fatal(args...)
}

// Println logging with new line
func (l *StandardLogger) Println(args ...interface{}) {
l.logger.Println(args...)
// Fatalf level logging with message
func Fatalf(msg string, args ...interface{}) {
if level < FatalLevel {
return
}
log.Fatalf(msg, args...)
}

// Fatalln level logging with new line
func Fatalln(args ...interface{}) {
if level < FatalLevel {
return
}
log.Fatalln(args...)
}

// Error level logging
func Error(args ...interface{}) {
if level < ErrorLevel {
return
}
log.Error(args...)
}

// Errorf level logging with message
func Errorf(msg string, args ...interface{}) {
if level < ErrorLevel {
return
}
log.Errorf(msg, args...)
}

// Errorln level logging with new line
func Errorln(args ...interface{}) {
if level < ErrorLevel {
return
}
log.Errorln(args...)
}

// Warn level logging
func Warn(args ...interface{}) {
if level < WarnLevel {
return
}
log.Warn(args...)
}

// Warnf level logging with message
func Warnf(msg string, args ...interface{}) {
if level < WarnLevel {
return
}
log.Warnf(msg, args...)
}

// Warnln level logging with new line
func Warnln(args ...interface{}) {
if level < WarnLevel {
return
}
log.Warnln(args...)
}

// Panic logging
func (l *StandardLogger) Panic(args ...interface{}) {
l.logger.Panic(args...)
// Info level logging
func Info(args ...interface{}) {
if level < InfoLevel {
return
}
log.Info(args...)
}

// Panicf logging with message
func (l *StandardLogger) Panicf(msg string, args ...interface{}) {
l.logger.Panicf(msg, args...)
// Infof level logging with message
func Infof(msg string, args ...interface{}) {
if level < InfoLevel {
return
}
log.Infof(msg, args...)
}

// Panicln logging with new line
func (l *StandardLogger) Panicln(args ...interface{}) {
l.logger.Panicln(args...)
// Infoln level logging with new line
func Infoln(args ...interface{}) {
if level < InfoLevel {
return
}
log.Infoln(args...)
}

// Fatal logging
func (l *StandardLogger) Fatal(args ...interface{}) {
l.logger.Fatal(args...)
// Debug level logging
func Debug(args ...interface{}) {
if level < DebugLevel {
return
}
log.Debug(args...)
}

// Fatalf logging with message
func (l *StandardLogger) Fatalf(msg string, args ...interface{}) {
l.logger.Fatalf(msg, args...)
// Debugf level logging with message
func Debugf(msg string, args ...interface{}) {
if level < DebugLevel {
return
}
log.Debugf(msg, args...)
}

// Fatalln logging with new line
func (l *StandardLogger) Fatalln(args ...interface{}) {
l.logger.Fatalln(args...)
// Debugln level logging with new line
func Debugln(args ...interface{}) {
if level < DebugLevel {
return
}
log.Debugln(args...)
}
Loading

0 comments on commit 5426523

Please sign in to comment.