Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 959
Abdullah Khan Avatar asked Sep 20 '16 11:09

Abdullah Khan


2 Answers

The ErrorLogFormat did not work for me - I'm not sure if this is because I'm using Apache V2.4. I ended up with these formats:

ErrorLogFormat "{ \"time\":\"%{cu}tZ\", \"function\" : \"[%-m:%l]\" , \"process\" : \"[pid %P:tid %T]\" , \"message\" : \"%M\"}"

I found the format codes from the docs (https://httpd.apache.org/docs/2.4/mod/core.html#errorlogformat)

The LogFormat is as above:

LogFormat "{ \"time\":\"%{%Y-%m-%dT%T}t.%{usec_frac}t%{%z}t\", \"remoteIP\":\"%a\", \"host\":\"%V\", \"requestPath\":\"%U\", \"query\":\"%q\", \"method\":\"%m\", \"status\":\"%>s\", \"userAgent\":\"%{User-agent}i\", \"referer\":\"%{Referer}i\" }" json

Which is documented in the module (as opposed to the error log format, which is documented in the core, because it is not a module)

like image 61
Daniel Scott Avatar answered Oct 24 '22 18:10

Daniel Scott


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.

like image 21
Abhishek Gurjar Avatar answered Oct 24 '22 20:10

Abhishek Gurjar