Giter Site home page Giter Site logo

Level guards about zerolog HOT 4 CLOSED

Asarew avatar Asarew commented on July 28, 2024
Level guards

from zerolog.

Comments (4)

rs avatar rs commented on July 28, 2024 1

With zerolog you can just do this:

if e := log.Debug(); e.Enabled() {
    // Compute log output only if current level is debug or higher.
    value := compute()
    e.Str("foo": value).Msg("some debug message")
}

from zerolog.

rs avatar rs commented on July 28, 2024

Do you have a use-case in mind?

from zerolog.

Asarew avatar Asarew commented on July 28, 2024

I'm currently developing a system where i heavily rely on logging to debug e2e tests. The places where i have to log items are hot-paths in my execution. When constructing the log Event I first log an interface{} and grab time.Now(), the operation it self is asynchronous and returns a wait object (future/promise/deferred).
When i wan to log the duration of the operation; i have to start a goroutine and wait on object to return.
But when to the logger level is higher than debug it's unnecessary to create a goroutine everytime where the result of the func is to discard the message anyway.

This was my specific use-case but i see the same pattern in other projects as well like kubernetes with there use of the glog package: https://godoc.org/github.com/golang/glog#V

from zerolog.

Asarew avatar Asarew commented on July 28, 2024

Thnx for the quick solution/reponse! I didn't spot the Event.Enabled() method. Even though i'm not the biggest fan of this api, it does exactly what i was looking for!

closing this issue as answered 👍

from zerolog.

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.