[AstroPy] Astropy logging configuration

Evert Rol evert.rol at gmail.com
Mon Mar 27 03:05:22 EDT 2017


Is there a way to configure the astropy logger using the standard Python logging configuration?
If I try the code below, I actually get double output, with only one of the two lines using the format I specified.
More specifically, I'd like to be able to use a config dict, or a yaml file that may contain the configuration for several loggers: some may require a a debug level (for specific packages I'm debugging), while I'd like to silence others (to a warning or error level); so a global (root) logging configuration doesn't really work.
(I realise that the amount of logging in astropy is low to non-existent, even at the debug level.)

Practically, this issue came up when using a Django application, where I tried to set the various loggers in the Django settings file. That doesn't work (and caused some headaches, because astropy needs to be imported before Django sets up its loggers, which means it needs to be imported outside the whole Django project).


  Evert


import logging
import astropy
logger = logging.getLogger('astropy')
formatter = logging.Formatter(fmt="%(asctime)s [%(levelname)s] %(message)s")
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.info('Hello')
astropy.log.info('Bye')


output:
INFO: Hello [__main__]
2017-03-27 14:05:28,330 [INFO] Hello
INFO: Bye [__main__]
2017-03-27 14:05:29,341 [INFO] Bye



More information about the AstroPy mailing list