Django logging-log message is not saved in respective file ! - django

I am newbie to django...Am learning Django-logging concept now..my problem is that log messages will not saved in the file that i have used in my code below,
(In my settings.py)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
},
},
'handlers': {
'default': {
'level':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename': '/home/linuxuser/mani/f/logs/msg.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter':'simple',
},
},
'loggers': {
'sample': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': True,
},
}
}
(In my views.py)
import logging
import logging.handlers
from django.conf import settings
logger = logging.getLogger('sample')
def empdel(request,id):
e = get_object_or_404(emp, pk=id)
e.delete()
logger.info('A row is deleted successfully !!!') [# here is my prob..this msg is not saved in '/home/linuxuser/mani/f/logs/msg.log' #]
return HttpResponseRedirect('/empthanks/')
Dono why the log message is not saved in that file..Pls anyone give me the solution even it is so simple..Whats wrong here??
Thanks in advance..
Mani

Try this for your logging setting and see if it helps.
Notice disable_existing_loggers is True and I added a default '' to the loggers.
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': '/home/linuxuser/mani/f/logs/msg.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter':'standard',
},
'request_handler': {
'level':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename': 'logs/django_request.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter':'standard',
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': True
},
'django.request': { # Stop SQL debug from logging to main logger
'handlers': ['request_handler'],
'level': 'DEBUG',
'propagate': False
},
}
}

A few questions.
Are you seeing anything in /home/linuxuser/mani/f/logs/msg.log?
Or just not the log message you wanted? If you aren't seeing anything
Do you have permission to write to that file?
In your case 2, you shouldn't need this in the settings.py file.
import logging
import logging.handlers
from django.conf import settings
logging.basicConfig()
you should just need the LOGGING tuple. For more info on configuration see the django documentation.
https://docs.djangoproject.com/en/1.3/topics/logging/

Related

Django not logging all errors on production

My Django app is only properly logging and emailing errors when Debug=True.
On production, I'm only getting 'Invalid HTTP_HOST header' errors in both the log file and my inbox. Every other error is not logged in the django log nor is it emailed to me.
I've tried some things with this test request:
def test(request):
logging.debug('debug test message') // Always appears in log file
from django.core.mail import mail_admins
mail_admins('test error', 'test error via call to mail_admins()') // Always arrives in my inbox just fine
raise Appointment.DoesNotExist // When Debug=False (both locally and in production), does not appear in log file or in inbox.
My logging settings for both local and production:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
'csv_formatter': {
'format': '%(levelname)s;%(asctime)s;%(message)s'
}
},
'handlers': {
'null': {
'level': 'DEBUG',
'class': 'logging.NullHandler',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
},
'default': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'maxBytes': 1024 * 1024 * 50,
'backupCount': 10,
'formatter': 'csv_formatter',
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'formatter': 'verbose',
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': True,
},
'asyncio': {
'level': 'WARNING',
},
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'django.security': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
How do I make sure all Django errors are logged and emailed in production (when Debug=False)?

Django Raven only logs once to Sentry

I have set up my logging to go to Sentry in my app. When I run ./manage.py raven test, it works every time. When I call the log in lets say a view, it works once. After that it stops logging at all. There are no errors or output at all.
The weird thing is if remove the logging settings and set the logger as:
Logger = logging.getLogger('django.request')
It works every time
settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'root': {
'level': 'DEBUG',
'handlers': ['sentry'],
},
'formatters': {
'verbose': {
'format': ('%(levelname)s %(asctime)s %(module)s %(process)d '
'%(thread)d %(message)s')
},
},
'handlers': {
'sentry': {
'level': 'DEBUG',
'class': ('raven.contrib.django.raven_compat.handlers.'
'SentryHandler'),
},
},
'loggers': {
'log': {
'level': 'DEBUG',
'handlers': ['sentry'],
'propagate': False
},
},
}
views.py:
import logging
Logger = logging.getLogger('log')
def first_view(request):
Logger.error('First Error Works', exc_info=True)
def second_view(request):
Logger.error('Second Error Does Not', exc_info=True)

Logging in Django with raven and sentry

I may be a bit late on the train, but I wanted to use Sentry and Raven for logging in Django.
I set up sentry and raven to the point, where I ran the test for raven and it works.
So now I want to send my debug messages over to sentry, but how would I do this?
settings.py
RAVEN_CONFIG = {
'dsn': 'http://code4#mydomain:9000/2',
# If you are using git, you can also automatically configure the
# release based on the git info.
'release': raven.fetch_git_sha(BASE_DIR),
}
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
},
'handlers': {
'sentry': {
'level': 'WARNING',
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
},
'console': {
'level': 'WARNING',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
}
},
'loggers': {
'django': {
'handlers': ['sentry'],
'level': 'DEBUG',
'propagate': True,
},
'raven': {
'level': 'DEBUG',
'handlers': ['sentry'],
'propagate': False,
},
'sentry.errors': {
'level': 'DEBUG',
'handlers': ['sentry'],
'propagate': False,
},
}
}
view.py
import logger
from raven.contrib.django.models import get_client
client = get_client()
client.captureException()
logger = logging.getLogger(__name__)
def my_view(request):
[...]
logger.error('There was some crazy error', exc_info=True, extra={
# Optionally pass a request and we'll grab any information we can
'request': request,
})
[...]
At this point it just logs errors and exceptions but wont send me this error message...
How is the right way to use raven and sentry? The docs are totaly not helping and my google foo left me also. Any tips or helpfull tutorials?
You have 3 loggers defined: django, raven and sentry.errors. When you call logging.getLogger(__name__) you actually create a "throw-away" one, because your ___name__ doesn't match any of above.
You should either use the raven logger...
logger = logging.getLogger('raven')
logger.debug('Hola!')
...or setup your own:
LOGGING = {
# ...
'loggers': {
# ...
'yourapp': {
'level': 'debug',
'handlers': ['sentry'],
'propagate': False,
},
}
}
and then in yourapp/views.py:
logger = logging.getLogger(__name__)
logger.debug('Hola!')

Django logging not working in views

I have logging set-up in my Django 1.9 project. I am getting the logs in django_request.log automatically as expected but not in mylog.log when I use logging in views.py. Where am I wrong?
views.py
import logging
logr = logging.getLogger(__name__)
def sample_view(request):
logr.debug('Ran Sample view')
return HttpResponse("Done!")
settings.py
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': 'logs/mylog.log',
'maxBytes': 1024 * 1024 * 5, # 5MB
'backupCount': 5,
'formatter': 'standard'
},
'request_handler': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'logs/django_request.log',
'maxBytes': 1024 * 1024 * 5, # 5MB
'backupCount': 5,
'formatter': 'standard'
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': True
},
'django.request': {
'handlers': ['request_handler'],
'level': 'DEBUG',
'propagate': False
},
}
}
I found out this "If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the default) then all loggers from the default configuration will be disabled. Disabled loggers are not the same as removed; the logger will still exist, but will silently discard anything logged to it, not even propagating entries to a parent logger."
on https://docs.djangoproject.com/en/1.7/topics/logging/#configuring-logging
So if you set 'disable_existing_loggers': False, you will be able to see all logs.

Sentry logging in Django/Celery stopped working

I have no idea whats wrong. So far logging worked fine (and I was relying on that) but it seems to have stopped. I wrote a little test function (which does not work either):
core.tasks.py
import logging
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)
logger.setLevel(logging.DEBUG)
#app.task
def log_error():
logger.error('ERROR')
settings.py
INSTALLED_APPS += (
'raven.contrib.django.raven_compat',
)
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'root': {
'level': 'INFO', #If set to DEBUG, prints ALL DJANGO debug logs.
'handlers': ['console', 'sentry'],
},
'formatters': {
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
#log everything to the console
'console':{
'level':'DEBUG',
'class':'logging.StreamHandler',
'formatter': 'simple'
},
#logs directly to sentry
'sentry': {
'level': 'ERROR',
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
#'class': 'raven.contrib.django.handlers.SentryHandler', #I have tried both
},
},
'loggers': {
'django.request':{
'handlers': ['console', 'sentry'],
'level': 'DEBUG',
'propagate': False,
},
'celery.task':{
'handlers': ['console', 'sentry'],
'level': 'DEBUG',
'propagate': False,
},
#this is the logger for celery itself
'celery':{
'handlers': ['console', 'sentry'],
'level': 'ERROR',
'propagate': False,
},
},
}
from logging.config import dictConfig
dictConfig(LOGGING)
Executing the following in the Django shell logs to the console but it doesn't reach Sentry:
from core import tasks
tasks.log_error.delay()
It works when executing the task synchronously:
tasks.log_error()
Help!?
Django==1.6.2, raven==5.0.0, sentry==6.3.2, Python 2.7.3
I had to add
CELERYD_HIJACK_ROOT_LOGGER=False
to my Django settings.py.
I don't really understand why I explicitely have to tell celery to not hijack the root logger.