0

I am new to python and Django, so if you can provide a solution with more details and explanation that would be great.

I am using python 2.7.9, Django <1, 11, 2, u'final', 0>
I started a Django project by VS2015.
I want to print log to file.

I added the following code to settings.py:

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':'logging.NullHandler',
        },
        'logfile': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': "./logfile",
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        },
        'console':{
            'level':'INFO',
            'class':'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    'loggers': {
        'django': {
            'handlers':['console'],
            'propagate': True,
            'level':'WARN',
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'MYAPP': {
            'handlers': ['console', 'logfile'],
            'level': 'DEBUG',
        },
    }
}

I added the following code to views.py:

import logging
logger = logging.getLogger(__name__)
def hello_world(request):
    logger.error('Testing error log')
    return render(request, 'hello_world.html', {
        'current_time': str(datetime.now()),
    })

The problem is that a file named logfile is generated but there is nothing inside the file.
I have my project like this:enter image description here

2
  • 2
    is your project named 'MYAPP'? if not please replace the name with project/module name it will start working. code seems fine to me. Commented Jun 26, 2017 at 8:08
  • 1
    I changed it to app then it works. Thank you very much. Commented Jun 26, 2017 at 8:19

1 Answer 1

1

As mentioned in the comment:

is your project named 'MYAPP'? if not please replace the name with project/module name it will start working. code seems fine to me.

Your project seems to be named TryDjangoWebProject, but root module is named app you can use that, as you have.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Before I asked, I have tried TryDjangoWebProject but that is not working /.\ Also, the log file can be generated so I thought that may be caused by other problem.
@sflee Corrected the answer as per your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.