Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

[Suggestion/Discussion] Support for logging.getLogger(__name__) #389

@HappyEinara

Description

@HappyEinara

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 reduce setup_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 of logger = 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:

  • I write a library that I depend on but others might want to also.
  • I depend on external libraries who have no idea about my application or its logging.

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 class Klass both as the metavar for a class and to acknowledge your home in Austria, but it could be called logzero.DefaultLogger or something.

It seems there are three options:

  1. Refactor logzero so it continues to do exactly what it does as well as it does but also offers the same default setup as a Klass that implements the interface described in the link above, offering the logzero experience to a wider set of use-cases.
  2. Fork logzero entirely, copying and pasting the details necessary to implement my idea in a new project. This would mean limited hassle for you if you aren't interested, but means manual work to keep your improvements merged in; it would also mean yet another logging library that wouldn't be as discoverable as this.
  3. Refactor logzero's code to encapsulate more things like the default formatter, default handlers etc in a way that offers an API for a wrapper project to provide a 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 like setup_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:

  • The interface setLoggerClass defines: the constructor of Klass may only accept a name. However most configuration on logzero is done by calling module-level functions. If the defaults were implemented on the Klass and the config functions continued to manipulate the default logzero.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 the Klass which update a number of class-level variables, and the module interface reduced to wrappers that call these methods on logzero.logger which becomes an instance of Klass.

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 to logzero.setup_logger and be done with it. I don't know if that's a genius idea or a risky hack tbh.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions