Skip to content

Commit

Permalink
Add WithFields(...) to Entry and StackTrace() functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
joeybloggs authored and joeybloggs committed Jun 2, 2016
1 parent 4d75a4e commit f23bae7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## log
<img align="right" src="https://raw.githubusercontent.com/go-playground/log/master/logo.png">
![Project status](https://img.shields.io/badge/version-2.1-green.svg)
![Project status](https://img.shields.io/badge/version-2.2-green.svg)
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/log/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/log)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/log/badge.svg?branch=master)](https://coveralls.io/github/go-playground/log?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/log)](https://goreportcard.com/report/github.com/go-playground/log)
Expand Down
15 changes: 15 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"fmt"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -195,6 +196,20 @@ func (e *Entry) Fatalf(msg string, v ...interface{}) {
exitFunc(1)
}

// WithFields adds the provided fieldsto the current entry
func (e *Entry) WithFields(fields ...Field) LeveledLogger {
e.Fields = append(e.Fields, fields...)
return e
}

// StackTrace adds a field with stack trace to the current log Entry.
func (e *Entry) StackTrace() LeveledLogger {
trace := make([]byte, 1<<16)
n := runtime.Stack(trace, true)
e.Fields = append(e.Fields, F("stack trace", string(trace[:n])+"\n"))
return e
}

// Consumed lets the Entry and subsequently the Logger
// instance know that it has been used by a handler
func (e *Entry) Consumed() {
Expand Down
15 changes: 9 additions & 6 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,11 @@ type LeveledLogger interface {
Panicf(msg string, v ...interface{})
Alertf(msg string, v ...interface{})
Fatalf(msg string, v ...interface{})
}

// FieldLeveledLogger interface for logging by level and WithFields
type FieldLeveledLogger interface {
LeveledLogger
WithFields(...Field) LeveledLogger
StackTrace() LeveledLogger
}

var _ FieldLeveledLogger = Logger
var _ LeveledLogger = Logger

// Debug level formatted message.
func (l *logger) Debug(v ...interface{}) {
Expand Down Expand Up @@ -255,6 +251,13 @@ func (l *logger) WithFields(fields ...Field) LeveledLogger {
return newEntry(InfoLevel, "", fields, skipLevel)
}

// StackTrace creates a new log Entry with pre-populated field with stack trace.
func (l *logger) StackTrace() LeveledLogger {
trace := make([]byte, 1<<16)
n := runtime.Stack(trace, true)
return newEntry(DebugLevel, "", []Field{F("stack trace", string(trace[:n])+"\n")}, skipLevel)
}

func (l *logger) HandleEntry(e *Entry) {

// gather info if WarnLevel, ErrorLevel, PanicLevel, AlertLevel, FatalLevel or
Expand Down
16 changes: 16 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ func TestConsoleLoggerCaller1(t *testing.T) {
if buff.String() != "INFO log_test.go:256 Test Message\n" {
t.Errorf("test Custom Entry: Expected '%s' Got '%s'", "INFO log_test.go:256 Test Message\n", buff.String())
}

buff.Reset()
Logger.StackTrace().Debug()

expected := "DEBUG log_test.go:265 stack trace="
if !strings.HasPrefix(buff.String(), expected) {
t.Errorf("Expected Prefix '%s' Got '%s'", expected, buff.String())
}

buff.Reset()
Logger.WithFields(Logger.F("key", "value")).StackTrace().Debug()

expected = "DEBUG log_test.go:273 key=value stack trace="
if !strings.HasPrefix(buff.String(), expected) {
t.Errorf("Expected Prefix '%s' Got '%s'", expected, buff.String())
}
}

func TestLevel(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"fmt"
"runtime"
"time"
)

Expand Down Expand Up @@ -177,6 +178,13 @@ func WithFields(fields ...Field) LeveledLogger {
return newEntry(InfoLevel, "", fields, skipLevel)
}

// StackTrace creates a new log Entry with pre-populated field with stack trace.
func StackTrace() LeveledLogger {
trace := make([]byte, 1<<16)
n := runtime.Stack(trace, true)
return newEntry(DebugLevel, "", []Field{F("stack trace", string(trace[:n])+"\n")}, skipLevel)
}

// HandleEntry send the logs entry out to all the registered handlers
func HandleEntry(e *Entry) {
Logger.HandleEntry(e)
Expand Down
16 changes: 16 additions & 0 deletions pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,22 @@ func TestConsoleLoggerCaller2(t *testing.T) {
if buff.String() != "INFO pkg_test.go:399 Test Message\n" {
t.Errorf("test Custom Entry: Expected '%s' Got '%s'", "INFO pkg_test.go:399 Test Message\n", buff.String())
}

buff.Reset()
StackTrace().Debug()

expected := "DEBUG pkg_test.go:406 stack trace="
if !strings.HasPrefix(buff.String(), expected) {
t.Errorf("Expected Prefix '%s' Got '%s'", expected, buff.String())
}

buff.Reset()
StackTrace().WithFields(F("key", "value")).Debug()

expected = "DEBUG pkg_test.go:414 stack trace="
if !strings.HasPrefix(buff.String(), expected) {
t.Errorf("Expected Prefix '%s' Got '%s'", expected, buff.String())
}
}

func TestFatal2(t *testing.T) {
Expand Down

0 comments on commit f23bae7

Please sign in to comment.