Structured JSON Logging with Log4perl

With the help of the Log::Log4perl::Layout::JSON module Log4perl is able to output structured logs.

Example Log4perl Configuration with JSON output to syslog:

Example Code:

Example Output:

The Log4perl MDC feature could be used to attach additional information to all log events produced by your program. For a CGI script it may be usefull to attach the client ip or session information to log events.

Attach additional fields to the MDC:

MDC Example Output:

Leave a Reply

Your email address will not be published. Required fields are marked *