We set up logging like the django docs told us:
https://docs.djangoproject.com/en/2.1/topics/logging/#using-logging
# import the logging library
import logging
# Get an instance of a logger
logger = logging.getLogger(__name__)
def my_view(request, arg1, arg):
...
if bad_mojo:
# Log an error message
logger.error('Something went wrong!')
I want to avoid this line in every Python file which wants to log:
logger = logging.getLogger(__name__)
I want it simple:
logging.error('Something went wrong!')
But we want to keep one feature: We want to see the Python file name in the logging output.
Up to now we use this format:
'%(asctime)s %(name)s.%(funcName)s +%(lineno)s: %(levelname)-8s [%(process)d] %(message)s'
Example output:
2016-01-11 12:12:31 myapp.foo +68: ERROR Something went wrong
How to avoid logger = logging.getLogger(__name__)
?
__name__
becomes something like 'abc.qwe.zxc', by default, everything logged is captured by the root logger, and then in a separate config file you can assign a specific logger to 'abc.qwe' namespace and tell it not to propagate the event to the root logger, but mute or log into a separate handler (file) instead. You do not have to modify source code to configure logging. And since the module is basically a log4j for python, most admins and developers understand how to configure loffing in your application. – Maquis