Abdullah Khan Abdullah Khan - 3 months ago 31
Apache Configuration Question

How to make apache output its logs in JSON instead of its default logging format?

I need to display statistics from log files on my custom GUI. The log files are expected to be huge so parsing them manually would be overkill. However, if there is a way to output the logs in JSON instead of simple text, it could save a lot of time. Is there a way to do that?

Answer Source

Here is how I do, I learned it from somewhere on the internet,

For Errorlog:

ErrorLogFormat "{ \"time\":\"%{%Y-%m-%d}tT%{%T}t.%{msec_frac}tZ\", \"function\" : \"[%-m:%l]\" , \"process\" : \"[pid %P:tid %T]\" , \"message\" : \"%M\" ,\ \"referer\"\ : \"%{Referer}i\" },"

For Accesslog:

LogFormat "{ \"time\":\"%{%Y-%m-%d}tT%{%T}t.%{msec_frac}tZ\", \"process\":\"%D\", \"filename\":\"%f\", \"remoteIP\":\"%a\", \"host\":\"%V\", \"request\":\"%U\", \"query\":\"%q\", \"method\":\"%m\", \"status\":\"%>s\", \"userAgent\":\"%{User-agent}i\", \"referer\":\"%{Referer}i\" }," combined

Take care when adding some new variable, a minor mistake will stop your apache instance to invoke and make a backup of http.conf before trying.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download