[Django]-How to get the app name in django logger

8👍

Use pathname instead of filename in your logging configuration.

FORMAT = "[%(pathname)s:%(lineno)s - %(funcName)20s() ] %(message)s"

There are also other variables you can use — check the logging module documentation for a list.


Note that if you’re acquiring a Logger instance using logger = logging.getLogger(__name__) (which is a common way to do it), you can also retrieve the module name (e.g. myapp.views) using name.

This is (arguably) better practice but will not work if you’re doing e.g. logger = logging.getLogger("mylogger") or logger = logging.getLogger()

Leave a comment