Skip to content

Commit

Permalink
added log levels and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mantzas committed Jan 23, 2016
1 parent 4cd3e60 commit 889dad1
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 69 deletions.
42 changes: 21 additions & 21 deletions adaptlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,63 +64,63 @@ func TestNewStandardLoggerLoggingSucceeds(t *testing.T) {
t.Fatal("Logged items should be 9!")
}

if logger.loggingData[0] != Print || logger.loggingData[1] != Printf || logger.loggingData[2] != Println ||
logger.loggingData[3] != Fatal || logger.loggingData[4] != Fatalf || logger.loggingData[5] != Fatalln ||
logger.loggingData[6] != Panic || logger.loggingData[7] != Panicf || logger.loggingData[8] != Panicln {
if logger.loggingData[0] != PrintMsg || logger.loggingData[1] != PrintfMsg || logger.loggingData[2] != PrintlnMsg ||
logger.loggingData[3] != FatalMsg || logger.loggingData[4] != FatalfMsg || logger.loggingData[5] != FatallnMsg ||
logger.loggingData[6] != PanicMsg || logger.loggingData[7] != PanicfMsg || logger.loggingData[8] != PaniclnMsg {
t.Fatal("Logged items do not match!")
}
}

const (
Print = "Print"
Printf = "Printf"
Println = "Println"
PrintMsg = "PrintMsg"
PrintfMsg = "PrintfMsg"
PrintlnMsg = "PrintlnMsg"

Panic = "Panic"
Panicf = "Panicf"
Panicln = "Panicln"
PanicMsg = "PanicMsg"
PanicfMsg = "PanicfMsg"
PaniclnMsg = "PaniclnMsg"

Fatal = "Fatal"
Fatalf = "Fatalf"
Fatalln = "Fatalln"
FatalMsg = "FatalMsg"
FatalfMsg = "FatalfMsg"
FatallnMsg = "FatallnMsg"
)

type TestStandardLogger struct {
loggingData []string
}

func (l *TestStandardLogger) Print(args ...interface{}) {
l.loggingData = append(l.loggingData, Print)
l.loggingData = append(l.loggingData, PrintMsg)
}

func (l *TestStandardLogger) Printf(msg string, args ...interface{}) {
l.loggingData = append(l.loggingData, Printf)
l.loggingData = append(l.loggingData, PrintfMsg)
}

func (l *TestStandardLogger) Println(args ...interface{}) {
l.loggingData = append(l.loggingData, Println)
l.loggingData = append(l.loggingData, PrintlnMsg)
}

func (l *TestStandardLogger) Panic(args ...interface{}) {
l.loggingData = append(l.loggingData, Panic)
l.loggingData = append(l.loggingData, PanicMsg)
}

func (l *TestStandardLogger) Panicf(msg string, args ...interface{}) {
l.loggingData = append(l.loggingData, Panicf)
l.loggingData = append(l.loggingData, PanicfMsg)
}

func (l *TestStandardLogger) Panicln(args ...interface{}) {
l.loggingData = append(l.loggingData, Panicln)
l.loggingData = append(l.loggingData, PaniclnMsg)
}

func (l *TestStandardLogger) Fatal(args ...interface{}) {
l.loggingData = append(l.loggingData, Fatal)
l.loggingData = append(l.loggingData, FatalMsg)
}

func (l *TestStandardLogger) Fatalf(msg string, args ...interface{}) {
l.loggingData = append(l.loggingData, Fatalf)
l.loggingData = append(l.loggingData, FatalfMsg)
}

func (l *TestStandardLogger) Fatalln(args ...interface{}) {
l.loggingData = append(l.loggingData, Fatalln)
l.loggingData = append(l.loggingData, FatallnMsg)
}
94 changes: 90 additions & 4 deletions level_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ package adaptlog

import "errors"

// Level type
type Level int

// These logging levels are used to setup the logging
const (
// Panic level
Panic Level = iota
// Fatal level
Fatal
// Error level
Error
// Warn level
Warn
// Info level
Info
// Debug level
Debug
)

// ErrorLogger interface. Introduces Error logging facilities.
type ErrorLogger interface {
Error(...interface{})
Expand Down Expand Up @@ -41,113 +60,180 @@ type LvlLogger interface {
}

var levelLogger LvlLogger
var defaultLevel Level

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

// NewLevelLogger creates a new level logger
func NewLevelLogger() (*LevelLogger, error) {
// 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}, nil
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
}

// LevelLogger for logging with level support
type LevelLogger struct {
logger LvlLogger
level Level
}

// Panic level logging
func (l *LevelLogger) Panic(args ...interface{}) {
if l.level < Panic {
return
}
l.logger.Panic(args)
}

// Panicf level logging with message
func (l *LevelLogger) Panicf(msg string, args ...interface{}) {
if l.level < Panic {
return
}
l.logger.Panicf(msg, args)
}

// Panicln level logging with new line
func (l *LevelLogger) Panicln(args ...interface{}) {
if l.level < Panic {
return
}
l.logger.Panicln(args)
}

// Fatal level logging
func (l *LevelLogger) Fatal(args ...interface{}) {
if l.level < Fatal {
return
}
l.logger.Fatal(args)
}

// Fatalf level logging with message
func (l *LevelLogger) Fatalf(msg string, args ...interface{}) {
if l.level < Fatal {
return
}
l.logger.Fatalf(msg, args)
}

// Fatalln level logging with new line
func (l *LevelLogger) Fatalln(args ...interface{}) {
if l.level < Fatal {
return
}
l.logger.Fatalln(args)
}

// Error level logging
func (l *LevelLogger) Error(args ...interface{}) {
if l.level < Error {
return
}
l.logger.Error(args)
}

// Errorf level logging with message
func (l *LevelLogger) Errorf(msg string, args ...interface{}) {
if l.level < Error {
return
}
l.logger.Errorf(msg, args)
}

// Errorln level logging with new line
func (l *LevelLogger) Errorln(args ...interface{}) {
if l.level < Error {
return
}
l.logger.Errorln(args)
}

// Warn level logging
func (l *LevelLogger) Warn(args ...interface{}) {
if l.level < Warn {
return
}
l.logger.Warn(args)
}

// Warnf level logging with message
func (l *LevelLogger) Warnf(msg string, args ...interface{}) {
if l.level < Warn {
return
}
l.logger.Warnf(msg, args)
}

// Warnln level logging with new line
func (l *LevelLogger) Warnln(args ...interface{}) {
if l.level < Warn {
return
}
l.logger.Warnln(args)
}

// Info level logging
func (l *LevelLogger) Info(args ...interface{}) {
if l.level < Info {
return
}
l.logger.Info(args)
}

// Infof level logging with message
func (l *LevelLogger) Infof(msg string, args ...interface{}) {
if l.level < Info {
return
}
l.logger.Infof(msg, args)
}

// Infoln level logging with new line
func (l *LevelLogger) Infoln(args ...interface{}) {
if l.level < Info {
return
}
l.logger.Infoln(args)
}

// Debug level logging
func (l *LevelLogger) Debug(args ...interface{}) {
if l.level < Debug {
return
}
l.logger.Debug(args)
}

// Debugf level logging with message
func (l *LevelLogger) Debugf(msg string, args ...interface{}) {
if l.level < Debug {
return
}
l.logger.Debugf(msg, args)
}

// Debugln level logging with new line
func (l *LevelLogger) Debugln(args ...interface{}) {
if l.level < Debug {
return
}
l.logger.Debugln(args)
}
Loading

0 comments on commit 889dad1

Please sign in to comment.