1

I have a custom configuration for logs in views.py as below:

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)

I have created a directory as /var/log/eshot-api and given permission as chmod 777 to that directory so that there will be no problem for writing.

I've also created a function as below:

def initial_log(request, method):
    logger.debug("{ip} requested {path} with {kwargs} in {method} method".format(ip=ipaddr(request), path=request.get_full_path(), kwargs=str(dict(request.GET)), method=method))

method argument is a string to pass "GET" or "POST" to this function.

And I've put this at the beginning to my all get function of ClassBasedView. However, when I run and refresh a page a couple of time in order to generate some logs and look into my log files, they are empty.

And, I want to mention that this is a development server in my own computer.


Environment

  • django 1.9.6
  • ubuntu 14.04
  • python 3.5.1
Eray Erdin
  • 2,633
  • 1
  • 32
  • 66
  • 1
    Possible duplicate of [Why Django logging is not working](http://stackoverflow.com/questions/36571284/why-django-logging-is-not-working). Also show us your `LOGGING` configuration. – solarissmoke Jun 13 '16 at 02:52

1 Answers1

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,
}
Elwin Arens
  • 1,542
  • 10
  • 21
  • Thanks for documentation, I'll refer to it. However, that did not change the problem. Logs are still empty. I'll try the way of documentation and if it goes okay, then I will accept this answer as valid. Thanks... – Eray Erdin Jun 12 '16 at 22:51