1

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,
Mohamed Benkedadra
  • 1,964
  • 3
  • 21
  • 48
  • The level given for the handler is the **minimum** level that is being logged. That means `INFO` includes both `WARNING` and `ERROR`. If you don't want errors in your apps log you have to log errors with a different logger that is only handled by errors handler. – user2390182 Sep 08 '18 at 09:55
  • @schwobaseggl like this : pastebin.com/ZrPwpH0V ???? i have tried this and only the error handler works, the info handler doesn't ..... any ideas ?? – Mohamed Benkedadra Sep 08 '18 at 10:16
  • That won't work. In your modules, do sth like `logger = logging.getLogger(__name__)` and `error_logger = logging.getLogger()` and only use the latter to log errors. – user2390182 Sep 08 '18 at 10:19
  • @schwobaseggl should i do for example error_logger = logging.getLogger('error_logger') and create a logger called error_logger ??? – Mohamed Benkedadra Sep 08 '18 at 10:33
  • That's an option, but it is twisting the concept. After all, your app log is supposed to show you what's going on in your app. So why have it log the common, but not the important stuff? – user2390182 Sep 08 '18 at 10:53
  • @schwobaseggl i am sorry i'm not understanding what you're saying, by doing getLogger('error_logger') and getLogger('info_logger') for example, i will be able to log the info in info_logger and log the errors in error_logger , no ? what's wrong with that ? – Mohamed Benkedadra Sep 08 '18 at 11:03

1 Answers1

1

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.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191