Reputation: 1719
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:
Upvotes: 0
Views: 1553
Reputation: 14011
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.
Upvotes: 1