Skip to content

Prefix #44

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

Closed
jbenet opened this issue Jan 3, 2015 · 3 comments
Closed

Prefix #44

jbenet opened this issue Jan 3, 2015 · 3 comments

Comments

@jbenet
Copy link

jbenet commented Jan 3, 2015

Suppose I have a logger, and i want to prefix its output. Pseudo example:

func factorial(log prefix.Logger, i int) {
  log = log.Prefix("%d", i)
  log.Debug("enter")
  defer log.Debug("exit")

  switch {
  case i < 0:
    log.Error("is negative!")
    return 0
  case i == 0, i == 1:
    return 1
  default:
    return i * factorial(log, i - 1)
  }
}

func main() {
  n, _ := strconv.Atoi(os.Args[1])
  log := prefix.Logger("factorial")
  log = log.Prefix("factorial:")
  fmt.Printf("%d", factorial(log, n))
}
> go run main.go 4
factorial: 4 enter
factorial: 4 3 enter
factorial: 4 3 2 enter
factorial: 4 3 2 1 enter
factorial: 4 3 2 1 exit
factorial: 4 3 2 exit
factorial: 4 3 exit
factorial: 4 exit
24

> go run main.go -10
factorial: -10 enter
factorial: -10 is negative!
factorial: -10 exit
0

I implemented this: https://gist.github.com/jbenet/70ff524c460986eab6c3 which does what I want. (extracted from go-ipfs). BUT, the issue is the filename breaks, because op/go-logging assumes the runtime calldepth is at a particular place in the stack, and that its logger is not being wrapped.

So, if not wrapping, then it has to be done within a logging.Logger.

  • could wrap a Backend? can't get the logger's current backend, and the Record's fields are private.
  • could wrap the Formatter? the logger is not associated with a formatter. see backend.

Suggestions?

@giftig
Copy link
Contributor

giftig commented Jan 3, 2015

#42 though it has a couple of issues to iron out.

@jbenet
Copy link
Author

jbenet commented Jan 5, 2015

@giftig ah perfect

@op
Copy link
Owner

op commented Jan 9, 2015

That's pretty cool. Will merge #42 during the day if that helps!

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

No branches or pull requests

3 participants