Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging: subloggers or extra information

Tags:

python

logging

I have a Python application that uses dynamically discovered plugins to extends it capabilities. I am trying to figure out what is the best way to log information from the plugins. I think there are at least two solutions. Either I create a hierarchy of loggers:

myapp
myapp.plugin1
myapp.plugin2

And withing the base app:

logger = logging.getLogger('myapp')
logger.debug('mymessage')

and withing the plugin1

logger = logging.getLogger('myapp.plugin1')
logger.debug('mymessage from plugin1')

Or I add additional information using the extra parameter in the default logger: Within base app:

logger = logging.getLogger('myapp')
logger.debug('mymessage', extra=dict(plugin=None)

Within plugin1

logger = logging.getLogger('myapp')
logger.debug('mymessage from plugin1', extra=dict(plugin='plugin1')

What are the advantages or disadvantages (if any) between these two schemes?

like image 474
Hernan Avatar asked Jan 02 '26 04:01

Hernan


1 Answers

The best practice in the Python world is that each Python module declares its own logger. At the head of each file you have:

 import logging

 logger = logging.getLogger(__name__)

Then you have your Python modules in nice hierarchy, so that you can tell to increase or decrease the package logging level hierarchically using the dotted package name lookups. Some examples:

    'django': {
        'handlers': ['mail_admins'],
        'level': 'WARN',
        'propagate': True,
    },


    'django.request': {
        'handlers': ['mail_admins'],
        'level': 'ERROR',
        'propagate': True,
    },

    'django.db.backends': {
        'handlers': [],
        'level': 'ERROR',
        'propagate': True,
    },
like image 172
Mikko Ohtamaa Avatar answered Jan 03 '26 19:01

Mikko Ohtamaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!