Avoid `logger=logging.getLogger(__name__)`

2020-02-18 05:21发布

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__)?

2条回答
老娘就宠你
2楼-- · 2020-02-18 05:41

what about pathname? from https://docs.python.org/2/library/logging.html#formatter-objects

/Users/jluc/kds2/wk/explore/test_so_41.py

import logging

#another module, just to have another file...
import test_so_41b

#not so much to use basicConfig as a quick usage of %(pathname)s
logging.basicConfig(level=logging.DEBUG,
                    format='%(pathname)s %(asctime)s %(levelname)s %(message)s',
                    # filename='/tmp/myapp.log',
                    # filemode='w',
                    )

logging.debug('A debug message')
logging.info('Some information')
logging.warning('A shot across the bows')

test_so_41b.dosomething("hey there")

/Users/jluc/kds2/wk/explore/test_so_41b.py

import logging

def dosomething(msg):
    logging.info(msg)

audrey:explore jluc$ python test_so_41.py

output:

test_so_41.py 2016-01-16 14:46:57,997 DEBUG A debug message
test_so_41.py 2016-01-16 14:46:57,997 INFO Some information
test_so_41.py 2016-01-16 14:46:57,997 WARNING A shot across the bows
/Users/jluc/kds2/wk/explore/test_so_41b.py 2016-01-16 14:46:57,997 INFO hey there
查看更多
forever°为你锁心
3楼-- · 2020-02-18 05:45

You can use logging.basicConfig to define the default interface available through logging as follows:

import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)s.%(funcName)s +%(lineno)s: %(levelname)-8s [%(process)d] %(message)s',
                    )

This definition will now be used whenever you do the following anywhere in your application:

import logging
logging.error(...)

While __name__ is not available, the equivalent (and other options) are available through the default LogRecord attributes that can be used for error string formatting - including module, filename and pathname. The following is a two-script demonstration of this in action:

scripta.py

import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(module)s %(name)s.%(funcName)s +%(lineno)s: %(levelname)-8s [%(process)d] %(message)s',
                    )

from scriptb import my_view

my_view()

scriptb.py

import logging

def my_view():
    # Log an error message
    logging.error('Something went wrong!')

The logging definition is defined in scripta.py, with the added module parameter. In scriptb.py we simply need to import logging to get access to this defined default. When running scripta.py the following output is generated:

2016-01-14 13:22:24,640 scriptb root.my_view +9: ERROR    [14144] Something went wrong!

Which shows the module (scriptb) where the logging of the error occurs.

According to this answer you can continue to use any per-module configuration of logging from Django, by turning off Django handling and setting up the root handler as follows:

# settings.py - django config
LOGGING_CONFIG = None # disables Django handling of logging
LOGGING = {...}  # your standard Django logging configuration

import logging.config
logging.config.dictConfig(LOGGING)
查看更多
登录 后发表回答