Skip to content

A prefab PatternLayout for log4j that generates logstash json_event formatted data

License

Notifications You must be signed in to change notification settings

ph4t/log4j-jsonevent-layout

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logstash json_event pattern for log4j - groovy edition

What is it?

The original library has a few drawbacks when used in grails/groovy projects:

  • file name, line number, class name & method name are wrong. This is a known issue in log4j appenders.
  • external json library is obsolete due to groovy JsonBuilder.

Main differences compared to the original code are:

  • outputs correct location information (thanks to this post)
  • renamed the appender to JSONGroovyEventLayout to avoid any conflicts
  • MDC key/value pairs are added directly to @fields
  • v1 version removed

Implementation changes:

  • switched from an external json library to JsonBuilder
  • made the code more groovy...
  • gradle instead of maven

Grails Usage

Complete documentation is provided by original library To use this library inside a grails project:

  1. Build this library (gradle build) and move the produced jar file to the lib folder

  2. Define the appender inside Config.groovy:

    log4j = {
    	appenders {
    		...
    		rollingFile name: "logstash", maxFileSize: '100MB', file: "logstash.log", layout: (new net.logstash.log4j.JSONGroovyEventLayout())
    		...
    	}
    	//don't forget to use the new appender
    

Sample logstash config:

input {
  file {
    path => ["C:/workspace/planG/logstash.log" ]
	codec =>   json {
	}
  }
}

About

A prefab PatternLayout for log4j that generates logstash json_event formatted data

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 59.9%
  • Groovy 40.1%