Git Product home page Git Product logo

Comments (10)

wongak avatar wongak commented on June 12, 2024

https://github.com/inconshreveable/log15/blob/master/ext/handler.go

The EscalateErrHandler might be one way to deal with error types.

from log15.

inconshreveable avatar inconshreveable commented on June 12, 2024

You basically don't ever want to format values into your log messages, because then your data is no longer structured and can't be easily parsed by a machine. Instead prefer:

log.Info("some text", "max", max, "min", min, "someCtx", 42)

For errors, my own code uses 'err', usually like this:

f, err := os.Open(path)
log.Debug("open output file", "path", path, "err", err)
if err != nil {
    // handle error
}

I have considered allowing syntax to format the context keys into the message as well for additional human readability, but I haven't found I really needed it:

log.Debug("open %(path)s", "path" path, "err", err)

from log15.

aktau avatar aktau commented on June 12, 2024

Ok, good to know. "err" is a bit shorter than "error" indeed. Guess I'll use that. Thanks!

from log15.

aktau avatar aktau commented on June 12, 2024

Just found out that the context of child loggers doesn't override the content of a parent (because it's a slice, I just saw in the implementation). Should I re-create the necessary context each time?

from log15.

inconshreveable avatar inconshreveable commented on June 12, 2024

It appends to the context (or should at least) so:

l := log.New("foo", 1)
l2 := l.New("bar", 2)
l2.Info("test") // prints msg=test foo=1 bar=2

If you want a logger with less or no context, use the appropriate parent or the root logger, which always has no context. Does that make sense?

from log15.

aktau avatar aktau commented on June 12, 2024

Yes, my use case was this:

log := l.New("mod", "watcher", "fn", "main", "instance", 908632)

startSomeModule(log.New())

In this way, startSomeModule would override mod and fn everytime (by creating new subloggers, I hope this is not a very heavyweight operation to do on every function call), but the instance context variable would be kept (not overridden).

In any case, it's no big deal, I'll work around it.

from log15.

inconshreveable avatar inconshreveable commented on June 12, 2024

You want to create a new logger with the appropriate context whenever it becomes relevant, so for your use case:

logger := log.New("instance", 908632)
startSomeModule(logger.New("mod", watcherValue, "fn", fnName))

You could just pass the logger into startSomeModule and have it create the child logger there as well.

from log15.

aktau avatar aktau commented on June 12, 2024

You could just pass the logger into startSomeModule and have it create the child logger there as well.

That's what startSomeModule does, in fact. Like I said, it's a small change so no big deal :).

from log15.

inconshreveable avatar inconshreveable commented on June 12, 2024

awesome! closing this out

from log15.

aktau avatar aktau commented on June 12, 2024

Thanks for all the help!

from log15.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.