2👍
✅
Check your settings.py for a LOGGING = {...}
setting and make sure that it has disable_existing_loggers
set to False
:
LOGGING = {
...
'disable_existing_loggers': False,
..
}
Or remove the LOGGING setting completely. And add handlers to the logger:
logger.addHandler(log_handlers["file_debug"])
Also i’de like to advise you to configure LOGGING in your settings.py.
Fully working example
urls.py
:
from django.conf.urls import url
from .views import IndexView
urlpatterns = [
url(r'^$', IndexView.as_view()),
]
views.py
:
import logging
from django.http import HttpResponse
from django.views.generic import View
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Handlers
log_handlers = {
"terminal": logging.StreamHandler(),
"file_debug": logging.FileHandler("/var/log/eshot-api/{name}-{level}.log".format(name=__name__, level="DEBUG"), mode="w"),
"file_error": logging.FileHandler("/var/log/eshot-api/{name}-{level}.log".format(name=__name__, level="ERROR"), mode="w")
}
log_handlers["terminal"].setLevel(logging.INFO)
log_handlers["file_debug"].setLevel(logging.DEBUG)
log_handlers["file_error"].setLevel(logging.ERROR)
# Formatters
log_formatters = {
"terminal": logging.Formatter("[%(name)s]::[%(levelname)s]@[%(asctime)s]: %(message)s"),
"file_debug": logging.Formatter("[%(levelname)s]@[%(asctime)s]: %(message)s"),
"file_error": logging.Formatter("[%(asctime)s]: %(message)s")
}
for k, v in log_formatters.items():
log_handlers[k].setFormatter(v)
logger.addHandler(log_handlers['file_debug'])
class IndexView(View):
def get(self, request):
logger.debug("requested {path} with {kwargs} in method".format(path=request.get_full_path(), kwargs=str(dict(request.GET))))
return HttpResponse('app:index')
settings.py
LOGGING = {
'disable_existing_loggers': False,
}
Source:stackexchange.com