Giter Site home page Giter Site logo

global SetLevel about go-logging HOT 6 OPEN

op avatar op commented on August 18, 2024
global SetLevel

from go-logging.

Comments (6)

op avatar op commented on August 18, 2024

That sounds like a great feature.

I like the idea of thinking of this as the argument beeing a prefix, eg. "".

The level logic would not set all loggers beneath it to a level but the
opposite; when fetching the level, stop when a defined level is found,
splitting the module name by '/'.

from go-logging.

jbenet avatar jbenet commented on August 18, 2024

@op sgtm. In that case, I suggest prefixing everything with /. Many systems make the mistake of removing the leading slash (favoring things like "foo/bar", "foo/bar/baz"), but pathing systems have the leading / for good reasons, like namespacing/mounting.

Extending this analogy, i could totally see this making sense:

log1 := logging.MustGetLogger("/")
log2 := logging.MustGetLogger("/foo")
log3 := logging.MustGetLogger("/foo/bar")

logging.SetLevel(logging.ERROR, "/foo") // log2 and log2 are now at logging.ERROR

Or:

log1 := logging.RootLogger()  // has prefix of "/"
log2 := logging.NewLogger(log1, "foo")  // has prefix /foo
log3 := logging.NewLogger(log2, "bar") // has prefix /foo/bar

logging.SetLevel(logging.ERROR, "/foo") // log2 and log2 are now at logging.ERROR

Logger Tree

Note that up there I'm constructing loggers explicitly, whereas you prefer static loggers (GetLogger). It's your module and probably should think thrice before breaking the interface, but I think constructing Loggers explicitly makes sense. This is particularly apparent when instantiating objects in a module twice, and wanting to redirect their logging separately.

I think go-logging could work phenomenally well with a logger-tree model similar to how google's context module works. If you haven't seen it, check it out. it's really a great piece of interface design. http://godoc.org/code.google.com/p/go.net/context

from go-logging.

op avatar op commented on August 18, 2024

@jbenet That's worth a thought. Why do you want to have relative paths for loggers though? I believe that might lead to code that is harder to reason about. Very flexible, I agree.

I've been thinking of going the opposite route. Somehow, make it possible to just say logging.New(), and have that automatically figure out the package path and re-use that.

Keep the ideas coming. :)

from go-logging.

jbenet avatar jbenet commented on August 18, 2024

Why do you want to have relative paths for loggers though? I believe that might lead to code that is harder to reason about. Very flexible, I agree.

On the contrary. Static initialziation is very hard to reason about / deal with when changing the package structure or instantiation of things. I'd follow the context example.

logging.New(), and have that automatically figure out the package path and re-use that.

seems too magic to me. sometimes i don't care about logging per package, but --say-- per incoming request. (useful to change logging level on specific requests across packages)

from go-logging.

jbenet avatar jbenet commented on August 18, 2024

One more reason. being able to do this (on a per-request, or whatever, basis):

log := logger.WithPrefix("[Peer: %s] ", local.peer.ID())

log.Debug("trying to do something with %s and %s, with %s", thisThing, thatThing, anotherPeer.ID())
// [Peer: abcbacbacbbacbabc] trying to do something with aaaaa and bbbbb, with dfdffdfdfdffdfdfdfd

log.Error("something bad happened: %s", err)  
// [Peer: abcbacbacbbacbabc] something bad happened: all the things broke.

Instead of, everywhere:

log.Debug("[Peer: %s] trying to do something with %s and %s, with %s", local.peer.ID(), thisThing, thatThing, anotherPeer.ID())
// [Peer: abcbacbacbbacbabc] trying to do something with aaaaa and bbbbb, with dfdffdfdfdffdfdfdfd

log.Error("[Peer: %s] something bad happened: %s", local.peer.ID(), err)  
// [Peer: abcbacbacbbacbabc] something bad happened: all the things broke.

from go-logging.

rasky avatar rasky commented on August 18, 2024

Instead of "WithPrefix", I would check the design of structlog and go that way:

log := log.Bind("Peer", local.peer.ID())
log = log.Bind("Other", foobar)
log.Debug("doing something")

so basically store a map[string]interface{} and dump that as a prefix. You need a value semantic for return values, but it's more powerful because you can even overwrite an object by simply rebinding it. Moreover, it opens the door to having a json.formatter to dump logs in a machine-parsable format.

from go-logging.

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.