configuring ajaxAppender of log4javascript with Django backend

736 views Asked by At

I am trying to configure ajaxAppender of log4javascript in DJango. I have made a file frontendlog.json where I want to write the logs going from the front end. This is how I write the script in myPage.html.

    <script type="text/javascript" src="/static/js/log4javascript.js"></script>
  <script language="javascript">
    var url = '/frontEndLog/';
    var log = log4javascript.getLogger("serverlog");
    var ajaxAppender = new log4javascript.AjaxAppender(url);
    ajaxAppender.addHeader("Content-Type", "application/json");
    var jsonLayout = new log4javascript.JsonLayout();
    ajaxAppender.setLayout(jsonLayout);
    log.addAppender(ajaxAppender);
    window.onerror = function(errorMsg, url, lineNumber){
        log.fatal("Uncaught error "+errorMsg+" in "+url+", line "+lineNumber);
    };
    log.info("Front End Log");
    alert('!!')
  </script>

In my django urls.py I have this entry url(r'^frontEndLog/$', 'TryOn.views.frontEndLog'),

and in my django view I have this view function

def frontEndLog(request):
    LOGGER.info ("frontEndLog")
    return render_to_response('frontEndLog.json', mimetype="text/json")

So I expected the frontEndLog to be written in frontEndLog.json in the same location as other HTMLs are found in django. However, it tells me that XMLhttpRequest Request to URL returned status code 500. Can somebody please tell me where I am going wrong here and is this the correct way to use log4javascript in django?

1

There are 1 answers

0
naiveDeveloper On BEST ANSWER

I solved it. I printed the django request object in views.py. There I was able to find the log messages in the request.POST. It appears in the form of a dictionary since it is JSON-ified. You can access the logs with this

clientLogs = request.POST.get('data')

'data' is the key in the key : value pair here. (You can easily understand that when you see the POST object).

Whether you want to print it in the views.py itself or write it to a a txt file is up to you. So all this while the logs were actually getting logged without me being able to identify it! I guess I should have read the documentation better.