Skip to content

Commit

Permalink
simplified logger instances
Browse files Browse the repository at this point in the history
  • Loading branch information
mantzas committed Mar 6, 2016
1 parent 646f2c0 commit 50c15f3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 149 deletions.
17 changes: 3 additions & 14 deletions adaptlog.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package adaptlog

import "errors"

// PrintLogger interface. Introduces Print logging facilities.
type PrintLogger interface {
Print(...interface{})
Expand Down Expand Up @@ -35,21 +33,12 @@ type StandardLogger struct {
logger StdLogger
}

var stdLogger StdLogger
// Logger standard logger instance
var Logger StandardLogger

// ConfigStandardLogger configures a standard logger
func ConfigStandardLogger(logger StdLogger) {
stdLogger = logger
}

// NewStandardLogger creates a new standard logger
func NewStandardLogger() (*StandardLogger, error) {

if stdLogger == nil {
return nil, errors.New("Standard logger is not configured!")
}

return &StandardLogger{stdLogger}, nil
Logger = StandardLogger{logger}
}

// Print logging
Expand Down
44 changes: 13 additions & 31 deletions adaptlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ package adaptlog

import "testing"

func TestNewStandardLoggerWithoutConfigReturnsError(t *testing.T) {
func TestLoggerWithoutConfigIsNil(t *testing.T) {

logger, err := NewStandardLogger()

if logger != nil {
t.Fatal("Logger should have been nil!")
}

if err == nil {
if Logger.logger != nil {
t.Fatal("Should have returned a error")
}
}
Expand All @@ -21,15 +15,9 @@ func TestNewStandardLoggerSucceeds(t *testing.T) {

ConfigStandardLogger(logger)

stdLogger, err := NewStandardLogger()

if stdLogger == nil {
if Logger.logger == nil {
t.Fatal("Logger should have been not nil!")
}

if err != nil {
t.Fatal("Should not have returned a error")
}
}

func TestNewStandardLoggerLoggingSucceeds(t *testing.T) {
Expand All @@ -38,27 +26,21 @@ func TestNewStandardLoggerLoggingSucceeds(t *testing.T) {

ConfigStandardLogger(logger)

stdLogger, err := NewStandardLogger()

if stdLogger == nil {
if Logger.logger == nil {
t.Fatal("Logger should have been not nil!")
}

if err != nil {
t.Fatal("Should not have returned a error")
}

stdLogger.Print("")
stdLogger.Printf("Test")
stdLogger.Println("")
Logger.Print("")
Logger.Printf("Test")
Logger.Println("")

stdLogger.Fatal("")
stdLogger.Fatalf("Test")
stdLogger.Fatalln("")
Logger.Fatal("")
Logger.Fatalf("Test")
Logger.Fatalln("")

stdLogger.Panic("")
stdLogger.Panicf("Test")
stdLogger.Panicln("")
Logger.Panic("")
Logger.Panicf("Test")
Logger.Panicln("")

if len(logger.loggingData) != 9 {
t.Fatal("Logged items should be 9!")
Expand Down
28 changes: 4 additions & 24 deletions level_logger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package adaptlog

import "errors"

// Level type
type Level int

Expand Down Expand Up @@ -59,33 +57,15 @@ type LvlLogger interface {
DebugLogger
}

var levelLogger LvlLogger
var defaultLevel Level

// LLogger instance
var LLogger LevelLogger

// ConfigLevelLogger configures a leveled logger
func ConfigLevelLogger(logger LvlLogger, level Level) {
levelLogger = logger
defaultLevel = level
}

// NewDefaultLevelLogger creates a new logger with the default level
func NewDefaultLevelLogger() (*LevelLogger, error) {

if levelLogger == nil {
return nil, errors.New("Level logger is not configured!")
}

return &LevelLogger{levelLogger, defaultLevel}, nil
}

// NewLevelLogger creates a new level logger with a specified log level
func NewLevelLogger(level Level) (*LevelLogger, error) {

if levelLogger == nil {
return nil, errors.New("Level logger is not configured!")
}

return &LevelLogger{levelLogger, level}, nil
LLogger = LevelLogger{logger, defaultLevel}
}

// LevelLogger for logging with level support
Expand Down
123 changes: 43 additions & 80 deletions level_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,22 @@ import (
"testing"
)

func TestNewLeveledLoggerWithoutConfigReturnsError(t *testing.T) {
func TestLLoggerWithoutConfigReturnsIsNil(t *testing.T) {

logger, err := NewLevelLogger(Debug)

if logger != nil {
t.Fatal("Logger should have been nil!")
}

if err == nil {
t.Fatal("Should have returned a error")
}
}

func TestNewDefaultLeveledLoggerWithoutConfigReturnsError(t *testing.T) {

logger, err := NewDefaultLevelLogger()

if logger != nil {
if LLogger.logger != nil {
t.Fatal("Logger should have been nil!")
}

if err == nil {
t.Fatal("Should have returned a error")
}
}

func TestNewDefaultLeveledLoggerSucceeds(t *testing.T) {
func TestLLoggerSucceeds(t *testing.T) {

var logger = new(TestLevelLogger)

ConfigLevelLogger(logger, Debug)

stdLogger, err := NewDefaultLevelLogger()

if stdLogger == nil {
if LLogger.logger == nil {
t.Fatal("Logger should have been not nil!")
}

if err != nil {
t.Fatal("Should not have returned a error")
}
}

func TestNewDefaultLevelLoggerLoggingSucceeds(t *testing.T) {
Expand All @@ -54,39 +29,33 @@ func TestNewDefaultLevelLoggerLoggingSucceeds(t *testing.T) {

ConfigLevelLogger(logger, Debug)

stdLogger, err := NewDefaultLevelLogger()

if stdLogger == nil {
if LLogger.logger == nil {
t.Fatal("Logger should have been not nil!")
}

if err != nil {
t.Fatal("Should not have returned a error")
}

stdLogger.Fatal("")
stdLogger.Fatalf("Test")
stdLogger.Fatalln("")
LLogger.Fatal("")
LLogger.Fatalf("Test")
LLogger.Fatalln("")

stdLogger.Panic("")
stdLogger.Panicf("Test")
stdLogger.Panicln("")
LLogger.Panic("")
LLogger.Panicf("Test")
LLogger.Panicln("")

stdLogger.Error("")
stdLogger.Errorf("Test")
stdLogger.Errorln("")
LLogger.Error("")
LLogger.Errorf("Test")
LLogger.Errorln("")

stdLogger.Warn("")
stdLogger.Warnf("Test")
stdLogger.Warnln("")
LLogger.Warn("")
LLogger.Warnf("Test")
LLogger.Warnln("")

stdLogger.Info("")
stdLogger.Infof("Test")
stdLogger.Infoln("")
LLogger.Info("")
LLogger.Infof("Test")
LLogger.Infoln("")

stdLogger.Debug("")
stdLogger.Debugf("Test")
stdLogger.Debugln("")
LLogger.Debug("")
LLogger.Debugf("Test")
LLogger.Debugln("")

if len(logger.loggingData) != 18 {
t.Fatal("Logged items should be 18!")
Expand Down Expand Up @@ -120,41 +89,35 @@ func TestLevelLoggingTableTest(t *testing.T) {
for _, test := range leveledLoggingTests {

var logger = new(TestLevelLogger)
ConfigLevelLogger(logger, Debug)

lvllogger, err := NewLevelLogger(test.in)
ConfigLevelLogger(logger, test.in)

if lvllogger == nil {
if LLogger.logger == nil {
t.Fatal("Logger should have been not nil!")
}

if err != nil {
t.Fatal("Should not have returned a error")
}

lvllogger.Panic("")
lvllogger.Panicf("Test")
lvllogger.Panicln("")
LLogger.Panic("")
LLogger.Panicf("Test")
LLogger.Panicln("")

lvllogger.Fatal("")
lvllogger.Fatalf("Test")
lvllogger.Fatalln("")
LLogger.Fatal("")
LLogger.Fatalf("Test")
LLogger.Fatalln("")

lvllogger.Error("")
lvllogger.Errorf("Test")
lvllogger.Errorln("")
LLogger.Error("")
LLogger.Errorf("Test")
LLogger.Errorln("")

lvllogger.Warn("")
lvllogger.Warnf("Test")
lvllogger.Warnln("")
LLogger.Warn("")
LLogger.Warnf("Test")
LLogger.Warnln("")

lvllogger.Info("")
lvllogger.Infof("Test")
lvllogger.Infoln("")
LLogger.Info("")
LLogger.Infof("Test")
LLogger.Infoln("")

lvllogger.Debug("")
lvllogger.Debugf("Test")
lvllogger.Debugln("")
LLogger.Debug("")
LLogger.Debugf("Test")
LLogger.Debugln("")

if len(logger.loggingData) == len(test.out) && len(test.out) == 0 {
if test.in != -1 {
Expand Down

0 comments on commit 50c15f3

Please sign in to comment.