简体   繁体   中英

Django App Log level info not getting write into file

I have added below setting in Django for Info level log.

logger = logging.getLogger(__name__)
logging.basicConfig(LOGGING=settings.LOGGING)

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
    'verbose': {
        'format': '%(asctime)s %(levelname)s %(message)s',
    },
},
'handlers': {
    'file': {
        'class': 'logging.handlers.TimedRotatingFileHandler',
        'formatter': 'verbose',
        'level': logging.INFO,
        'filename': '/logs/django/api.log',
        'interval': 1,
        'when': 'midnight',
        'encoding': 'utf8'
    },
},
'loggers': {
    'django': {
        'handlers': ['file'],
        'level': logging.INFO,
        'propagate': True,
    },
},
}

If I change this level to Debug then it works properly but when I change it to INFO Level then the log will not be written into the file. Can somebody help?

Try the following settings:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
        },
    },
    'handlers': {
        'default': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': 'test.log',
            'maxBytes': 1024*1024*5, # 5 MB
            'backupCount': 5,
            'formatter':'standard',
        },
        'request_handler': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': 'django_request.log',
            'maxBytes': 1024*1024*5, # 5 MB
            'backupCount': 5,
            'formatter':'standard',
        },
    },
    'loggers': {
        '': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'DEBUG',
            'propagate': False
        },
    }
}

In your code:

import logging

logger = logging.getLogger(__name__)

logger.info("My name is Info")

Sample output:

2017-05-25 04:56:38,912 [INFO] api.views: API - initiate_scan view, new object created. Uid: cf3822b261790186297d30e4f5b448b2

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