Skip to content

Commit

Permalink
added context to std level logger
Browse files Browse the repository at this point in the history
  • Loading branch information
mantzas committed Apr 3, 2016
1 parent 4ef0eb3 commit e878294
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 45 deletions.
102 changes: 67 additions & 35 deletions std_level_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,64 @@ func (l LogLevel) String() string {
}
}

var logFunc func(LogLevel, string, ...interface{})
var loglnFunc func(LogLevel, string, ...interface{})
var logfFunc func(LogLevel, string, string, ...interface{})

// StdLevelLogger is a level logger based on the standard log package
type StdLevelLogger struct {
logFunc func(LogLevel, string, ...interface{})
loglnFunc func(LogLevel, string, ...interface{})
logfFunc func(LogLevel, string, string, ...interface{})
context string
}

// NewStdLevelLogger creates a new standard level logger
func NewStdLevelLogger(context string) *StdLevelLogger {
return &StdLevelLogger{logFunc, loglnFunc, logfFunc, context}
}

// ConfigureStdLevelLogger configures the standard level logger with the default log level.
// By providing a different io.Writer and prefix you can control the logging output (testing etc)
func ConfigureStdLevelLogger(defaultLoglevel LogLevel, w io.Writer) {
func ConfigureStdLevelLogger(defaultLoglevel LogLevel, w io.Writer, context string) {

logFunc := func(level LogLevel, args ...interface{}) {
logFunc = func(level LogLevel, context string, args ...interface{}) {
if defaultLoglevel > level {
return
}

if w != nil {
log.SetOutput(w)
}
args = prepend(args, strings.Join([]string{level.String(), " "}, ""))

if context == "" {
args = prepend(args, strings.Join([]string{level.String(), " "}, ""))
} else {
args = prepend(args, level.String(), " ", context, " ")
}

log.Print(args...)
}

loglnFunc := func(level LogLevel, args ...interface{}) {
loglnFunc = func(level LogLevel, context string, args ...interface{}) {
if defaultLoglevel > level {
return
}

if w != nil {
log.SetOutput(w)
}
args = prepend(args, level.String())

if context == "" {
args = prepend(args, level.String())
} else {
args = prepend(args, level.String(), context)
}

log.Println(args...)
}

logfFunc := func(level LogLevel, msg string, args ...interface{}) {
logfFunc = func(level LogLevel, context string, msg string, args ...interface{}) {
if defaultLoglevel > level {
return
}
Expand All @@ -73,114 +102,117 @@ func ConfigureStdLevelLogger(defaultLoglevel LogLevel, w io.Writer) {
log.SetOutput(w)
}

log.Printf(strings.Join([]string{level.String(), msg}, " "), args...)
if context == "" {
log.Printf(strings.Join([]string{level.String(), msg}, " "), args...)
} else {
log.Printf(strings.Join([]string{level.String(), context, msg}, " "), args...)
}
}

Level = &StdLevelLogger{logFunc, loglnFunc, logfFunc}
Level = NewStdLevelLogger(context)
}

func prepend(args []interface{}, value string) []interface{} {
argsExt := make([]interface{}, len(args)+1)
argsExt[0] = value
for index := 0; index < len(args); index++ {
argsExt[index+1] = args[index]
func prepend(args []interface{}, values ...string) []interface{} {
valuesLength := len(values)
argsLength := len(args)
argsExt := make([]interface{}, argsLength+valuesLength)

for index := 0; index < valuesLength; index++ {
argsExt[index] = values[index]
}
return argsExt
}

// StdLevelLogger is a level logger based on the standard log package
type StdLevelLogger struct {
logFunc func(LogLevel, ...interface{})
loglnFunc func(LogLevel, ...interface{})
logfFunc func(LogLevel, string, ...interface{})
for index := valuesLength; index < argsLength+valuesLength; index++ {
argsExt[index] = args[index-valuesLength]
}
return argsExt
}

// Fatal logging
func (l *StdLevelLogger) Fatal(args ...interface{}) {

l.logFunc(FatalLevel, args...)
l.logFunc(FatalLevel, l.context, args...)
}

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

l.logfFunc(FatalLevel, msg, args...)
l.logfFunc(FatalLevel, l.context, msg, args...)
}

// Fatalln logging
func (l *StdLevelLogger) Fatalln(args ...interface{}) {

l.loglnFunc(FatalLevel, args...)
l.loglnFunc(FatalLevel, l.context, args...)
}

// Error logging
func (l *StdLevelLogger) Error(args ...interface{}) {

l.logFunc(ErrorLevel, args...)
l.logFunc(ErrorLevel, l.context, args...)
}

// Errorf logging
func (l *StdLevelLogger) Errorf(msg string, args ...interface{}) {

l.logfFunc(ErrorLevel, msg, args...)
l.logfFunc(ErrorLevel, l.context, msg, args...)
}

// Errorln logging
func (l *StdLevelLogger) Errorln(args ...interface{}) {

l.loglnFunc(ErrorLevel, args...)
l.loglnFunc(ErrorLevel, l.context, args...)
}

// Warn logging
func (l *StdLevelLogger) Warn(args ...interface{}) {

l.logFunc(WarnLevel, args...)
l.logFunc(WarnLevel, l.context, args...)
}

// Warnf logging
func (l *StdLevelLogger) Warnf(msg string, args ...interface{}) {

l.logfFunc(WarnLevel, msg, args...)
l.logfFunc(WarnLevel, l.context, msg, args...)
}

// Warnln logging
func (l *StdLevelLogger) Warnln(args ...interface{}) {

l.loglnFunc(WarnLevel, args...)
l.loglnFunc(WarnLevel, l.context, args...)
}

// Info logging
func (l *StdLevelLogger) Info(args ...interface{}) {

l.logFunc(InfoLevel, args...)
l.logFunc(InfoLevel, l.context, args...)
}

// Infof logging
func (l *StdLevelLogger) Infof(msg string, args ...interface{}) {

l.logfFunc(InfoLevel, msg, args...)
l.logfFunc(InfoLevel, l.context, msg, args...)
}

// Infoln logging
func (l *StdLevelLogger) Infoln(args ...interface{}) {

l.loglnFunc(InfoLevel, args...)
l.loglnFunc(InfoLevel, l.context, args...)
}

// Debug logging
func (l *StdLevelLogger) Debug(args ...interface{}) {

l.logFunc(DebugLevel, args...)
l.logFunc(DebugLevel, l.context, args...)
}

// Debugf logging
func (l *StdLevelLogger) Debugf(msg string, args ...interface{}) {

l.logfFunc(DebugLevel, msg, args...)
l.logfFunc(DebugLevel, l.context, msg, args...)
}

// Debugln logging
func (l *StdLevelLogger) Debugln(args ...interface{}) {

l.loglnFunc(DebugLevel, args...)
l.loglnFunc(DebugLevel, l.context, args...)
}
47 changes: 37 additions & 10 deletions std_level_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestLogLevelToString(t *testing.T) {

func TestStdLevelLoggerNew(t *testing.T) {

ConfigureStdLevelLogger(DebugLevel, nil)
ConfigureStdLevelLogger(DebugLevel, nil, "")

if Level == nil {
t.Fatal("Should have returned a error")
Expand All @@ -38,7 +38,7 @@ func TestStdLevelLoggerNew(t *testing.T) {

func TestStdLevelLoggerNewWithWriter(t *testing.T) {

ConfigureStdLevelLogger(DebugLevel, new(TestWriter))
ConfigureStdLevelLogger(DebugLevel, new(TestWriter), "")

if Level == nil {
t.Fatal("Should have returned a error")
Expand All @@ -49,7 +49,7 @@ func TestStdLevelLoggerWithHigherDefaultLevel(t *testing.T) {

w := new(TestWriter)

ConfigureStdLevelLogger(ErrorLevel, w)
ConfigureStdLevelLogger(ErrorLevel, w, "")

Level.Warn("Test")
Level.Error("Test")
Expand All @@ -58,11 +58,11 @@ func TestStdLevelLoggerWithHigherDefaultLevel(t *testing.T) {
if len(w.data) != 2 {
t.Fatalf("Data should have been 2 %s", w.data)
}
if strings.HasSuffix(w.data[0], "Error Test") {
if !strings.HasSuffix(w.data[0], "Error Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}

if strings.HasSuffix(w.data[1], "Fatal Test") {
if !strings.HasSuffix(w.data[1], "Fatal Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}
w.data = nil
Expand All @@ -74,11 +74,11 @@ func TestStdLevelLoggerWithHigherDefaultLevel(t *testing.T) {
if len(w.data) != 2 {
t.Fatalf("Data should have been 2 %s", w.data)
}
if strings.HasSuffix(w.data[0], "Error Test") {
if !strings.HasSuffix(w.data[0], "Error Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}

if strings.HasSuffix(w.data[1], "Fatal Test") {
if !strings.HasSuffix(w.data[1], "Fatal Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}
w.data = nil
Expand All @@ -90,15 +90,42 @@ func TestStdLevelLoggerWithHigherDefaultLevel(t *testing.T) {
if len(w.data) != 2 {
t.Fatalf("Data should have been 2 %s", w.data)
}
if strings.HasSuffix(w.data[0], "Error Test") {
if !strings.HasSuffix(w.data[0], "Error Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}

if strings.HasSuffix(w.data[1], "Fatal Test") {
if !strings.HasSuffix(w.data[1], "Fatal Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}
}

func TestStdLevelLoggerWithContext(t *testing.T) {

w := new(TestWriter)

ConfigureStdLevelLogger(DebugLevel, w, "Context")

Level.Debug("Test")
Level.Debugf("Test %s", "one")
Level.Debugln("Test")

if len(w.data) != 3 {
t.Fatalf("Data should have been 3 %s", w.data)
}

if !strings.HasSuffix(w.data[0], "Debug Context Test\n") {
t.Fatalf("Expected %s actual %s", "Debug Context Test", w.data[0])
}

if !strings.HasSuffix(w.data[1], "Debug Context Test one\n") {
t.Fatalf("Expected %s actual %s", "Debug Context Test one", w.data[1])
}

if !strings.HasSuffix(w.data[2], "Debug Context Test\n") {
t.Fatalf("Expected %s actual %s", "Debug Context Test", w.data[2])
}
}

var stdLevelLogTests = []struct {
in contextLevelLogTestCase
out string
Expand Down Expand Up @@ -127,7 +154,7 @@ var stdLevelLogTests = []struct {
func TestStdLeveledLogger(t *testing.T) {

w := new(TestWriter)
ConfigureStdLevelLogger(DebugLevel, w)
ConfigureStdLevelLogger(DebugLevel, w, "")

logFunctions := make(map[string]logFunction, 0)
logFunctions["Debug"] = func(msg string, args ...string) { Level.Debug(convertToInterfaceSlice(args)...) }
Expand Down

0 comments on commit e878294

Please sign in to comment.