Comments (11)
@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.
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.
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.
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.
All right, no problem thank you for your answer (I'm really happy with logrus btw ;) )
from logrus.
Happy to hear that! Don't hesitate to step by with other suggestions or problems you may have.
from logrus.
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.
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.
6be56e6 this functionality was added to master because way of structuring Go code has become to popular.
from logrus.
@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.
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)
- can't build for wasm (go 1.21 rc) HOT 3
- Data race in hooks.test package HOT 3
- virter vm cp not working HOT 2
- Info requested: How to buffer messages and release at the end HOT 2
- Vulnerability of dependency "gopkg.in/yaml.v3" HOT 2
- Logrus potential DOS - Please open a CVE for issue #1370 to encourage users to patch HOT 3
- Feature: Add slog compatibility layer HOT 5
- logrus+lumberjackrus, After the old log is deleted, the hard drive will not be released, HOT 2
- Vulnerability: gopkg.in/yaml.v3 CVE-2022-28948 HOT 2
- Bazel compilation failling HOT 2
- Question: Any way to view the value of a field from a logrus.FieldLogger? HOT 2
- How to apply colours for a custom log format? HOT 2
- How to safely change Logger.Out on the fly? HOT 2
- How should I position my problem? HOT 2
- Cut a new release with wasip1 support? HOT 4
- github.com/interactive-solutions/go-logrus-elasticsearch cannot find HOT 2
- Bump version of stretchr/testify? HOT 3
- Unclear on documentation regarding Hook interface HOT 3
- Please maintain the CHANGELOG HOT 3
- logger.Out.Write's mutex may be unnecessary, when Out is a file fd HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logrus.