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