GithubHelp home page GithubHelp logo

Default instance of logger about logrus HOT 11 CLOSED

sirupsen avatar sirupsen commented on May 16, 2024
Default instance of logger

from logrus.

Comments (11)

patrick-motard avatar patrick-motard commented on May 16, 2024 3

@dgsb I'm new to golang so that may be why I'm having trouble understanding. I read through the documentation and the readme, but I don't understand how to initialize logger in one package (package main, set it's debug level, and access that logger from another package, package foo.

from logrus.

sirupsen avatar sirupsen commented on May 16, 2024

I really like the idea of having a logger instance that's all customizable (hooks, etc.). I think it's more confusing than good to have two sort of independent APIs to support and document. Where'd you set the hook for the global logger for example? There isn't an obvious mapping to the current documentation.

from logrus.

Soulou avatar Soulou commented on May 16, 2024

I understand your point of view, it creates a bit of redundancy, however it's not much more work for maintaining it, and finally a lot of projects only need a global logger instance. I think that gophers are used to the stdlib log package. Creating loggers only in some specific customization tasks and using the default instance otherwise.

I may mistake and I know it's a bit of a dilemma between a small syntactic sugar for logrus users vs keeping the code base as clean as possible.

from logrus.

sirupsen avatar sirupsen commented on May 16, 2024

At some point I'd like to create some kind of inheritance for logrus, where you can create logger instances with default fields and pass them down, e.g. for prefixes. This is also handy to pass to libraries.

I don't want to put people in a situation where it's unclear how to use logrus. Currently there's one, right way and it gives me the most flexibility to expand the API without having two different ones.

from logrus.

Soulou avatar Soulou commented on May 16, 2024

All right, no problem thank you for your answer (I'm really happy with logrus btw ;) )

from logrus.

sirupsen avatar sirupsen commented on May 16, 2024

Happy to hear that! Don't hesitate to step by with other suggestions or problems you may have.

from logrus.

rnapier avatar rnapier commented on May 16, 2024

Just a note that I've been wrapping logrus this way in my own code; it doesn't have to be in logrus (though it might be more convenient if it were). It looks like this:

package log

import (
    "github.com/Sirupsen/logrus"
    "io"
)

var logger = logrus.New()

func SetOutput(w io.Writer) {
    logger.Out = w
}

func WithField(key string, value interface{}) *logrus.Entry {
    return logger.WithField(key, value)
}

func Info(args ...interface{}) {
    logger.Info(args...)
}

func WithFields(fields logrus.Fields) *logrus.Entry {
    return logger.WithFields(fields)
}

func WithError(err error) *logrus.Entry {
    return logger.WithField("error", err)
}

... etc ...

I then use it as:

import log "path/to/my/stuff/log"

And then I can:

log.Debug(...)

But I can also set defaults like:

log.SetOutput(...)

which is very convenient. It's also easy to add a DefaultLogger() function that would return the default logger, making it easy to pass down to libraries, etc.

The problem with a global logger variable is that it's only global within one package, so it seems to be a pain to share the same logger through a multi-package program (since packages aren't "instantiated" typically the way they are in other languages). This approach is exactly how the standard log does it (using std).

I appreciate the desire for a single approach, and it's a good instinct. Do you have a specific way you've been passing a logger between packages (not third party libraries; just packages within a program)?

from logrus.

sirupsen avatar sirupsen commented on May 16, 2024

Yeah, I understand that concern.. I'm not sure if the code above is the best solution, but I'd love to see this problem solved.

I haven't actually run into it myself, the Go projects I work on live in the same package.

from logrus.

sirupsen avatar sirupsen commented on May 16, 2024

6be56e6 this functionality was added to master because way of structuring Go code has become to popular.

from logrus.

patrick-motard avatar patrick-motard commented on May 16, 2024

@sirupsen was this functionality removed? I don't see the documentation added in that PR in master anymore. I would like to pass a logger between packages that doesn't require wrapping logrus in my own logger.

from logrus.

dgsb avatar dgsb commented on May 16, 2024

Hello @patrick-motard, we do still have a default logger in the logrus package. You can refer to the api documentation here or through the go doc command line tool.

from logrus.

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.