Skip to content

Issue 009: Wrong File/Line from logger.Debug()#11

Open
jrefior wants to merge 1 commit intoian-kent:masterfrom
jrefior:issue009
Open

Issue 009: Wrong File/Line from logger.Debug()#11
jrefior wants to merge 1 commit intoian-kent:masterfrom
jrefior:issue009

Conversation

@jrefior
Copy link
Copy Markdown

@jrefior jrefior commented Mar 31, 2018

logger.Debug(x) -- or other logger entry function --
calls logger.Log, which
calls logger.Write, which
calls appender.Write, which
calls layout.Format, which
calls layout.getCaller, which
calls runtime.Caller.

Therefore the correct number of stack frames to ascend to get to
the caller of Debug -- or other logger entry function -- is 6.

See #9

See also https://golang.org/pkg/runtime/#Caller

This code reproduces the bug:

package main

import(
    "github.com/ian-kent/go-log/appenders"
    "github.com/ian-kent/go-log/layout"
    "github.com/ian-kent/go-log/log"
)

func main() {
    logger := log.Logger()
    logger.SetLevel(log.Stol("DEBUG"))
    appender := appenders.RollingFile("sample.log", true)
    appender.MaxFileSize = 1024
    appender.SetLayout(layout.Pattern("%p %l %m "))
    logger.SetAppender(appender)
    logger.Debug("Reading configuration")
}

Result of runtime.Caller(n) in pattern.go for 0 >= n >= 7:

Caller(0): DEBUG layout/pattern.go:50 Reading configuration
Caller(1): DEBUG layout/pattern.go:70 Reading configuration
Caller(2): DEBUG appenders/rollingfile.go:51 Reading configuration
Caller(3): DEBUG logger/logger.go:129 Reading configuration
Caller(4): DEBUG logger/logger.go:154 Reading configuration
Caller(5): DEBUG logger/logger.go:213 Reading configuration
Caller(6): DEBUG go-log/bug009.go:16 Reading configuration
Caller(7): DEBUG runtime/proc.go:185 Reading configuration

logger.Debug(x) -- or other logger entry function --
    calls logger.Log, which
    calls logger.Write, which
    calls appender.Write, which
    calls layout.Format, which
    calls layout.getCaller, which
    calls runtime.Caller.

Therefore the correct number of stack frames to ascend to get to
the caller of Debug -- or other logger entry function -- is 6.

See ian-kent#9

See also https://golang.org/pkg/runtime/#Caller

This code reproduces the bug:

package main

import(
    "github.com/ian-kent/go-log/appenders"
    "github.com/ian-kent/go-log/layout"
    "github.com/ian-kent/go-log/log"
)

func main() {
    logger := log.Logger()
    logger.SetLevel(log.Stol("DEBUG"))
    appender := appenders.RollingFile("sample.log", true)
    appender.MaxFileSize = 1024
    appender.SetLayout(layout.Pattern("%p %l %m "))
    logger.SetAppender(appender)
    logger.Debug("Reading configuration")
}

Result of runtime.Caller(n) in pattern.go for 0 >= n >= 7:

Caller(0): DEBUG layout/pattern.go:50 Reading configuration
Caller(1): DEBUG layout/pattern.go:70 Reading configuration
Caller(2): DEBUG appenders/rollingfile.go:51 Reading configuration
Caller(3): DEBUG logger/logger.go:129 Reading configuration
Caller(4): DEBUG logger/logger.go:154 Reading configuration
Caller(5): DEBUG logger/logger.go:213 Reading configuration
Caller(6): DEBUG go-log/bug009.go:16 Reading configuration
Caller(7): DEBUG runtime/proc.go:185 Reading configuration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant