Skip to content

Commit

Permalink
Issue cihub#93.
Browse files Browse the repository at this point in the history
  • Loading branch information
goodsign committed Sep 30, 2015
1 parent c510775 commit 785185d
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 19 deletions.
6 changes: 3 additions & 3 deletions cfg_logconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestConfig(t *testing.T) {
return
}

context, err := currentContext()
context, err := currentContext(nil)
if err != nil {
t.Errorf("cannot get current context:" + err.Error())
return
Expand Down Expand Up @@ -91,9 +91,9 @@ func TestConfig(t *testing.T) {
}

func getFirstContext() (LogContextInterface, error) {
return currentContext()
return currentContext(nil)
}

func getSecondContext() (LogContextInterface, error) {
return currentContext()
return currentContext(nil)
}
19 changes: 15 additions & 4 deletions common_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ type LogContextInterface interface {
IsValid() bool
// Time when log function was called.
CallTime() time.Time
// Custom context that can be set by calling logger.SetContext
CustomContext() interface{}
}

// Returns context of the caller
func currentContext() (LogContextInterface, error) {
return specifyContext(1)
func currentContext(custom interface{}) (LogContextInterface, error) {
return specifyContext(1, custom)
}

func extractCallerInfo(skip int) (fullPath string, shortPath string, funcName string, line int, err error) {
Expand Down Expand Up @@ -92,7 +94,7 @@ func extractCallerInfo(skip int) (fullPath string, shortPath string, funcName st
// Context is returned in any situation, even if error occurs. But, if an error
// occurs, the returned context is an error context, which contains no paths
// or names, but states that they can't be extracted.
func specifyContext(skip int) (LogContextInterface, error) {
func specifyContext(skip int, custom interface{}) (LogContextInterface, error) {
callTime := time.Now()
if skip < 0 {
err := fmt.Errorf("can not skip negative stack frames")
Expand All @@ -103,7 +105,7 @@ func specifyContext(skip int) (LogContextInterface, error) {
return &errorContext{callTime, err}, err
}
_, fileName := filepath.Split(fullPath)
return &logContext{funcName, line, shortPath, fullPath, fileName, callTime}, nil
return &logContext{funcName, line, shortPath, fullPath, fileName, callTime, custom}, nil
}

// Represents a normal runtime caller context.
Expand All @@ -114,6 +116,7 @@ type logContext struct {
fullPath string
fileName string
callTime time.Time
custom interface{}
}

func (context *logContext) IsValid() bool {
Expand Down Expand Up @@ -144,6 +147,10 @@ func (context *logContext) CallTime() time.Time {
return context.callTime
}

func (context *logContext) CustomContext() interface{} {
return context.custom
}

// Represents an error context
type errorContext struct {
errorTime time.Time
Expand Down Expand Up @@ -181,3 +188,7 @@ func (errContext *errorContext) FileName() string {
func (errContext *errorContext) CallTime() time.Time {
return errContext.errorTime
}

func (errContext *errorContext) CustomContext() interface{} {
return nil
}
25 changes: 22 additions & 3 deletions common_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func init() {
// Here we remove the hardcoding of the package name which
// may break forks and some CI environments such as jenkins.
_, _, funcName, _, _ := extractCallerInfo(1)
commonPrefix = funcName[:strings.Index(funcName, "init·")]
preIndex := strings.Index(funcName, "init·")
if preIndex == -1 {
preIndex = strings.Index(funcName, "init")
}
commonPrefix = funcName[:preIndex]
wd, err := os.Getwd()
if err == nil {
// Transform the file path into a slashed form:
Expand All @@ -54,7 +58,7 @@ func init() {
}

func TestContext(t *testing.T) {
context, err := currentContext()
context, err := currentContext(nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
Expand All @@ -79,7 +83,7 @@ func TestContext(t *testing.T) {
}

func innerContext() (context LogContextInterface, err error) {
return currentContext()
return currentContext(nil)
}

func TestInnerContext(t *testing.T) {
Expand All @@ -106,3 +110,18 @@ func TestInnerContext(t *testing.T) {
t.Errorf("expected context.FullPath == %s ; got %s", testFullPath, context.FullPath())
}
}

type testContext struct {
field string
}

func TestCustomContext(t *testing.T) {
expected := "testStr"
context, err := currentContext(&testContext{expected})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if st, _ := context.CustomContext().(*testContext); st.field != expected {
t.Errorf("expected context.CustomContext == %s ; got %s", expected, st.field)
}
}
2 changes: 1 addition & 1 deletion dispatch_customdispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestCustomDispatcher_Message(t *testing.T) {
return
}

context, err := currentContext()
context, err := currentContext(nil)
if err != nil {
t.Error(err)
return
Expand Down
4 changes: 2 additions & 2 deletions dispatch_filterdispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestfilterDispatcher_Pass(t *testing.T) {
return
}

context, err := currentContext()
context, err := currentContext(nil)
if err != nil {
t.Error(err)
return
Expand All @@ -56,7 +56,7 @@ func TestfilterDispatcher_Deny(t *testing.T) {
return
}

context, err := currentContext()
context, err := currentContext(nil)
if err != nil {
t.Error(err)
return
Expand Down
2 changes: 1 addition & 1 deletion dispatch_splitdispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestsplitDispatcher(t *testing.T) {
return
}

context, err := currentContext()
context, err := currentContext(nil)
if err != nil {
t.Error(err)
return
Expand Down
6 changes: 3 additions & 3 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ var formatTests = []formatTest{

func TestFormats(t *testing.T) {

context, conErr := currentContext()
context, conErr := currentContext(nil)
if conErr != nil {
t.Fatal("Cannot get current context:" + conErr.Error())
return
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestDateParameterizedFormat(t *testing.T) {
testFormat := "Mon Jan 02 2006 15:04:05"
preciseForamt := "Mon Jan 02 2006 15:04:05.000"

context, conErr := currentContext()
context, conErr := currentContext(nil)
if conErr != nil {
t.Fatal("Cannot get current context:" + conErr.Error())
return
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestCustomFormatterRegistration(t *testing.T) {
t.Errorf("expected an error when trying to register a custom formatter with duplicate name")
}

context, conErr := currentContext()
context, conErr := currentContext(nil)
if conErr != nil {
t.Fatal("Cannot get current context:" + conErr.Error())
return
Expand Down
10 changes: 9 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ type LoggerInterface interface {
// function/file names in log files. Do not use it if you are not going to wrap seelog funcs.
// You may reset the value to default using a SetAdditionalStackDepth(0) call.
SetAdditionalStackDepth(depth int) error

// Sets logger context that can be used in formatter funcs and custom receivers
SetContext(context interface{})
}

// innerLoggerInterface is an internal logging interface
Expand All @@ -134,6 +137,7 @@ type commonLogger struct {
unusedLevels []bool
innerLogger innerLoggerInterface
addStackDepth int // Additional stack depth needed for correct seelog caller context detection
customContext interface{}
}

func newCommonLogger(config *logConfig, internalLogger innerLoggerInterface) *commonLogger {
Expand Down Expand Up @@ -218,6 +222,10 @@ func (cLogger *commonLogger) Critical(v ...interface{}) error {
return errors.New(message.String())
}

func (cLogger *commonLogger) SetContext(c interface{}) {
cLogger.customContext = c
}

func (cLogger *commonLogger) traceWithCallDepth(callDepth int, message fmt.Stringer) {
cLogger.log(TraceLvl, message, callDepth)
}
Expand Down Expand Up @@ -282,7 +290,7 @@ func (cLogger *commonLogger) log(level LogLevel, message fmt.Stringer, stackCall
if cLogger.Closed() {
return
}
context, _ := specifyContext(stackCallDepth + cLogger.addStackDepth)
context, _ := specifyContext(stackCallDepth+cLogger.addStackDepth, cLogger.customContext)
// Context errors are not reported because there are situations
// in which context errors are normal Seelog usage cases. For
// example in executables with stripped symbols.
Expand Down
2 changes: 1 addition & 1 deletion writers_formattedwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestformattedWriter(t *testing.T) {
return
}

context, err := currentContext()
context, err := currentContext(nil)
if err != nil {
t.Error(err)
return
Expand Down

0 comments on commit 785185d

Please sign in to comment.