Skip to content

Add ExtraCalldepth to Logger #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2015
Merged

Add ExtraCalldepth to Logger #42

merged 1 commit into from
Jan 9, 2015

Conversation

giftig
Copy link
Contributor

@giftig giftig commented Dec 23, 2014

ExtraCalldepth is added to the default calldepth of 2 to allow the
correct function name to be shown when exposing logger methods at
package level or wrapping them in utility functions

Add an example of how this works to examples/example.go
Update examples/example.go to include a new screenshot
Update README.md to include the new example.go

ExtraCalldepth is added to the default calldepth of 2 to allow the
correct function name to be shown when exposing logger methods at
package level or wrapping them in utility functions

Add an example of how this works to examples/example.go
Update examples/example.go to include a new screenshot
Update README.md to include the new example.go
@giftig
Copy link
Contributor Author

giftig commented Dec 23, 2014

Hi @op I've added documentation per your request; I made an attempt at adding a test, but since the memory backend seems to ignore the calldepth, my approach didn't seem to achieve much, as Formatted(calldepth) is called in the tests.

This attempt, for example, doesn't achieve much as it passes for any value of ExtraCalldepth:

func TestExtraCalldepth(t *testing.T) {
    backend := InitForTesting(DEBUG)

    // Just output the shortfunc; that's all we care about in this test
    backendFormatter := NewBackendFormatter(
        backend,
        MustStringFormatter("%{shortfunc}"),
    )
    SetBackend(backendFormatter)

    log := MustGetLogger("test")
    wrappedError := log.Error
    log.ExtraCalldepth = 1
    wrappedError("")

    if "TestExtraCalldepth" != MemoryRecordN(backend, 0).Formatted(0) {
        t.Error("Unexpected shortfunc name")
    }
}

If you have any suggestions as to a better test strategy, let me know and I'll give it another whack; I'm inexperienced with testing in golang, I'm afraid.

// Example format string. Everything except the message has a custom color
// which is dependent on the log level. Many fields have a custom output
// formatting too, eg. the time returns the hour down to the milli second.
var format = logging.MustStringFormatter(
"%{color}%{time:15:04:05.000000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}",
"%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd note that this change was caused by me copying the updated contents of example.go directly into the code block; it looks like it was updated in an earlier commit but not amended in the code block in README.md

@giftig
Copy link
Contributor Author

giftig commented Dec 23, 2014

Looks like the example doesn't work in go v1... since this isn't a compulsory feature, though, I'd suggest support for >= 1.1 is better than nothing. The v1.1-only part could be split into a secondary example and excluded from the 1.0 build maybe?

@giftig giftig mentioned this pull request Jan 3, 2015
@jbenet
Copy link

jbenet commented Jan 5, 2015

+1 to ExtraCalldepth

@op
Copy link
Owner

op commented Jan 9, 2015

All code looks good! I will merge this, but will keep the ExtraCallDepth away from the normal example. This seems like an advanced feature that will confuse first time users. Thanks!

@op op merged commit 3d55c5b into op:master Jan 9, 2015
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.

3 participants