Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging getChild not working properly #8983

Open
juliotux opened this issue Jul 11, 2019 · 0 comments
Open

Logging getChild not working properly #8983

juliotux opened this issue Jul 11, 2019 · 0 comments
Labels

Comments

@juliotux
Copy link

juliotux commented Jul 11, 2019

When ask Astropy's logger for a child with getChild methos, the logger returns a default python logger (logging.Logger class) and not a proper astropy.logger.AstropyLogger instance. As the following examples shows:

In [1]: from astropy.logger import log                                         

In [2]: log.__class__                                                          
Out[2]: astropy.logger.AstropyLogger

In [3]: l = log.getChild('testing_child')                                      

In [4]: l.__class__                                                            
Out[4]: logging.Logger

As everything else works with AstropyLogger's record, any message generated by this child logger will raise AttributeError: 'LogRecord' object has no attribute 'origin', since default logger makeRecord do not set the origin attribute as Astropy do. This problem was noticed by @cdeil a long time ago in astropy/package-template#134 .

A reimplementation like the following one (based on _init_log()) should fix this problem, but generate another problem of multiplicity in output.

class AstropyLogger(Logger):
    def getChild(self, suffix):
        """
        Simple reimplementation of Logger.getChild
        """
        if self.root is not self:
            suffix = '.'.join((self.name, suffix))

        orig_logger_cls = logging.getLoggerClass()
        logging.setLoggerClass(AstropyLogger)
        try:
            log = logging.getLogger(suffix)
            log._set_defaults()
        finally:
            logging.setLoggerClass(orig_logger_cls)

        return log

How could we deal with this problem?

@pllim pllim added the logging label Jul 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants