Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: logging only for my project's apps

Tags:

logging

django

By default I can enable logging in settings.py in the LOGGING configuration by creating a logger "" which will catch all logs. But what if I only want to see logging from my project's apps as opposed to Django internals?

I can imagine explicitly getting a logger in each of my Django app modules and naming it by some convention, e.g. logging.getLogger("myproject." + __file__). Then I can create a logger called 'myproject' (in SETTINGS) which picks up all of these to output. I'd prefer not to hardcode my project name, so I'd do some os.path logic on ___file___ to extract the full namespace up to the file at any arbitrary depth.

At this point, I stop and wonder is there an easier way?

like image 575
John Lehmann Avatar asked Jun 01 '13 18:06

John Lehmann


People also ask

How do I enable logging in Django?

By default, the LOGGING setting is merged with Django's default logging configuration using the following scheme. If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the dictConfig default if the key is missing) then all loggers from the default configuration will be disabled.

What does logging getLogger (__ Name __) do?

getLogger(name) is typically executed. The getLogger() function accepts a single argument - the logger's name. It returns a reference to a logger instance with the specified name if provided, or root if not. Multiple calls to getLogger() with the same name will return a reference to the same logger object.

Where are Django logs?

Application level logging can be found in /var/log/upstart/gunicorn. log By default, Gunicorn logs to stderr and Upstart will collect output to stderr/stdout in /var/log/upstart/$JOB_NAME.


1 Answers

You could use a scheme like the following to create identical loggers for all of your local apps without having to manually add them all to the logging configuration.

First, split out your local apps:

LOCAL_APPS = [
    'myapp1',
    'myapp2',
    ...
]

THIRD_PARTY_APPS = [
    'django. ...',
    ...
]

INSTALLED_APPS = LOCAL_APPS + THIRD_PARTY_APPS

Next create the logger configuration for your local apps:

local_logger_conf = {
    'handlers': ['my_handler',],
    'level': 'DEBUG',
}

Finally, define your loggers as follows:

'loggers': { app: copy.deepcopy(local_logger_conf) for app in LOCAL_APPS }
like image 126
alanwj Avatar answered Sep 19 '22 14:09

alanwj