i'm trying to add logging to my django project here's my logger configuration:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s',
},
},
'handlers': {
'default': {
'class': 'logging.FileHandler',
'filename': os.path.join(*[BASE_DIR, 'logfiles', 'debug.log']),
'formatter': 'standard',
},
'apps_errors': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': os.path.join(*[BASE_DIR, 'logfiles', 'apps_errors.log']),
'formatter': 'standard',
},
'dev_logger': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': os.path.join(*[BASE_DIR, 'logfiles', 'apps_logs.log']),
'formatter': 'standard',
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': False,
},
'gui': {
'handlers': ['dev_logger', 'apps_errors'],
'propagate': True,
},
'crawler': {
'handlers': ['dev_logger', 'apps_errors'],
'propagate': True,
},
},
}
As you can see, I want to log everything to debug.log
and log errors on apps (crawler and gui) to apps_errors.log
and log info on apps (crawler and gui) to apps_logs.log
debug.log and apps_errors.log are working normally, everything is being logged to debug.log
and only errors in my 2 apps, are being logged to apps_errors.log
, but when it comes to apps_logs.log
i keep getting both errors and info when it should be only info
by the way when i want to log something i'm doing
import logging
logger = logging.getLogger(__name__)
for info i'm doing : logging.info(my_info)
for errors i'm doing: logging.exception(my_exception)
i also tried logging.error(my_exception)
PS:
i have tried defining two loggers, one for each handler, but doing that logs only errors using the errors logger/handler, the info one doesn't work
'gui': {
'handlers': ['dev_logger'],
'level': 'INFO',
'propagate': True,
},
'gui': {
'handlers': ['apps_errors'],
'level': 'ERROR',
'propagate': True,
If for some reason you only want INFO messages to show up in a handler's output, but nothing of higher severity, you would need to attach a filter to that handler. This is not a common requirement - though it's common to isolate errors and greater in logs, it's not common to isolate only INFO messages. Using a filter should work:
import logging
class InfoFilter(logging.Filter):
def filter(self, record):
return record.level == logging.INFO
and then assign that filter to your handler dev_logger
. Not sure why you've called it dev_logger
- perhaps you need to review your understanding of loggers and handlers. The top of the advanced tutorial gives a summary.