-
-
Notifications
You must be signed in to change notification settings - Fork 74
[Suggestion/Discussion] Support for logging.getLogger(__name__)
#389
Comments
Just a note that this issue is noticed. I'm just having a hard time finding the time to think about it! Will get back to you soon :) |
@metachris I've been thinking about it a bit more too, and I'm starting to think:
But: logzero provides defaults that could make configuring a logger with That way we can choose to use a lot of the cool stuff easily, but also take advantage of a bit more flexibility by using The good news is, I think Logzero is a bit a long that journey already. The Finally, then all you would need is:
|
This isn't an issue, and not necessarily a feature request, but more of a request for feedback.
💡 tldr: I propose a refactor that puts most of logzero into a class implementing the interface expected by
logging.setLoggerClass
and reducesetup_logger()
to a function that instantiates, configures and returns an instance of that class. This is to permit libraries unaware of logzero to benefit from all that makes logzero great but using the defacto standard pattern oflogger = logging.getLogger(__name__)
. But please also read the last line. :)If I'm writing an application where the logging is under my control, including in libraries I wrote, logzero is a beautiful solution. But consider the following situations:
In these cases the recommended pattern is for code to call
logging.getLogger(__name__)
, where the stdlib logging library will return a logger likely configured by the parent application.My idea is to implement this pattern but also leverage the sane and beautiful defaults of Logzero and the very easy implementation.
Now, you know more about logging than I do, Chris, but it seems passing an appropriately implemented Logger class to
logging.setLoggerClass(klass)
would achieve this. In the following I will call that classKlass
both as the metavar for a class and to acknowledge your home in Austria, but it could be calledlogzero.DefaultLogger
or something.It seems there are three options:
Klass
that implements the interface described in the link above, offering the logzero experience to a wider set of use-cases.Klass
, or for an advanced user to implement themselves. If this is the solution you may as well just do 1) imho.In the current Logzero implementation, while some things like the
LogFormatter
from Tornado are written in classes that could be imported and reused in other ways, a lot of logic is in functions likesetup_logger()
which provides the documented Logzero interface but also handles setting up the instances the Logzero Way. I think those tasks should be separated.Tricky bits:
setLoggerClass
defines: the constructor ofKlass
may only accept aname
. However most configuration on logzero is done by calling module-level functions. If the defaults were implemented on theKlass
and the config functions continued to manipulate the defaultlogzero.logger
, then the only problem would be finding the right way for a class-based user to achieve beautiful logzero results with logzero ease where configuration is needed. My suggestion is that the module-level config functions be implemented as methods on theKlass
which update a number of class-level variables, and the module interface reduced to wrappers that call these methods onlogzero.logger
which becomes an instance ofKlass
.I realise that this proposal would require a substantial rewrite of logzero, but on the other hand it's not a big file and it's mostly just a refactor and testing. On the other hand, I think there's an opportunity to maintain the current interface and expose the benefits of Logzero to a much wider set of use-cases which need the standard logging pattern but would love the ease and beauty of the Logzero interface and defaults.
Happy to hear your thoughts. If this doesn't appeal, I might attempt to implement this myself to demonstrate it and we can look at ways to collaborate after that if I can prove it's workable.
An alternative: just monkeypatch
logging.getLogger
in the parent app tologzero.setup_logger
and be done with it. I don't know if that's a genius idea or a risky hack tbh.The text was updated successfully, but these errors were encountered: