简体   繁体   中英

Django class logger not working

I have a class named "Photo" in my Django application which is not writing messages to my log file when an error occurs.

My project hierarchy looks like this:

- myproj
  - apps
  - classes
    - classes/__init__.py
    - classes/photo.py

Here is my LOGGING configuration setting:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'null': {
            'level':'DEBUG',
            'class':'django.utils.log.NullHandler',
        },
        'logfile': {
            'level':LOG_LEVEL,
            'class':'logging.handlers.RotatingFileHandler',
            'filename': '/var/log/myproj/apps.log',
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        },
        'database_logfile': {
            'level':LOG_LEVEL,
            'class':'logging.handlers.RotatingFileHandler',
            'filename': '/var/log/myproj/database.log',
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        },
        'console':{
            'level':LOG_LEVEL,
            'class':'logging.StreamHandler',
            'formatter': 'standard'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
            # Only send emails when DEBUG = False
            #'filters': ['require_debug_false'],
        },
    },
    'loggers': {
        'django': {
            'handlers':['console'],
            'propagate': True,
            'level':'WARN',
        },
        'django.request': {
           'handlers': ['mail_admins'],
           'level': 'ERROR',
           'propagate': False,
        },
        'django.db.backends': {
            'handlers': ['database_logfile'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'apps': {
            'handlers': ['console', 'logfile', 'mail_admins'],
            'level': 'DEBUG',
        },
        'classes.photo': {
            'handlers': ['console', 'logfile', 'mail_admins'],
            'level': 'DEBUG',
        },
    }
}

Here's part of my class:

# photo.py
import logging
logger = logging.getLogger(__name__)

class Photo:
  def create_gallery_photo(self):
    ...
    photo_size = os.path.getsize()
    if photo_size > PHOTO_SIZE_LIMIT:
        logger.error("Photo too big")

At first I only had an 'apps' handler and I realized that the error wouldn't get logged since photo.py was outside the 'apps' application. But when I added a 'classes' logger I started getting a "No classes could be found for logger" error. Not sure of what to do, I changed the logger to 'classes.photo' and the 'no classes' error went away but the error message still isn't getting logged. I checked 'logger.name' and it's set to 'classes.photo'. Is there something else I need to do because this error is being logged from a class? All the logging in my 'apps' project is working just fine.

Try adding a root logger with a console handler, and see what %(name)s gets logged there. All messages should reach that, unless they first get handled by a logger with propagate=False.

...
'loggers': {
    ...,
    '': {
          'handlers': ['console'],
           'level': 'DEBUG',
    },
 }
 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM