Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid check if logger exists

When using logging in python like this

import logging
logging.basicConfig(level=logging.INFO)
logger = None
if <some condition>:
   logger = logging.getLogger(__name__)
... some code ...
logger.debug('message')

is it possible to avoid calling logger.debug if it does not exist without if statement?

like image 840
Bob Avatar asked Nov 21 '14 21:11

Bob


1 Answers

That's not how you use logging: it's missing the point entirely. The point is to always just log, and then (at the app level) configure handlers appropriately so that they either record the debug level or not. Your app-level configuration can even set the handler for loggers in each module. See the Python logging docs for more info.

like image 75
Daniel Roseman Avatar answered Sep 22 '22 03:09

Daniel Roseman