项目作者: dubasdey

项目描述 :
Log4j2 JSON Event Layout without requirement of thirdparty libraries
高级语言: Java
项目地址: git://github.com/dubasdey/log4j2-jsonevent-layout.git
创建时间: 2017-05-03T09:40:51Z
项目社区:https://github.com/dubasdey/log4j2-jsonevent-layout

开源协议:GNU Lesser General Public License v3.0

下载


log4j2-jsonevent-layout

GitHub license
GitHub issues
CI Build Status
Maven Central
Codacy Badge
Codacy Badge

Log4j2 JSON Event Layout without requirement of thirdparty libraries

Usage

Add the <JSONLog4j2Layout> tag to any appender to use it.

Note: It could be required to add to the configuration node in the packages attribute the package of the plugin.

Example:

  1. <Configuration packages="org.erc.log4j2.layout">

Check the Log4j2 configuration java doc for more references
https://logging.apache.org/log4j/2.x/manual/configuration.html

Optional Attributes

  • locationInfo - Adds location info to the Trace
  • singleLine - Removes \r and \n in JSON String
  • htmlSafe - Escapes additional characters to print the JSON on HTML pages.
  • newLineFormat - Custom new line for each line (if singleLine is off) or the final line (if singleLine is on)
  • plainContextMap - Prints the content of the ContextMap in the root as key:value instead of a contextMap object with the values
  • charset - Charset to use (Default UTF-8)
  • UserField - Collection of user fields with key and value that will be printed in the LogEntry

Result Example

Example expanded to multiple lines

  1. {
  2. "@timestamp":"2017-05-03T15:46:34.393Z",
  3. "logger_name":"org.dummy.logger",
  4. "level":"DEBUG",
  5. "level_int":500,
  6. "thread_name":"DummyThread",
  7. "source_host":"MYLOCALMACHINE",
  8. "source_ip":"192.168.1.10",
  9. "message":"Dummy Message Test with tab T\tT before here.",
  10. "file":"File.java",
  11. "line_number":42,
  12. "class":"ClassName.class",
  13. "method":"MethodElement",
  14. "contextStack":["Entry 1","Entry 2"],
  15. "contextMap":
  16. [
  17. {"X-Generator":"JSONLog2j2Layout"},
  18. {"ThreadContextKey":"ThreadContextValue"}
  19. ],
  20. "@version":"1"
  21. }

Maven dependency

To include this library in your project just add the dependency to your maven project

Example:

  1. <dependency>
  2. <groupId>com.github.dubasdey</groupId>
  3. <artifactId>log4j2-jsonevent-layout</artifactId>
  4. <version>0.0.7</version>
  5. </dependency>

Donate

Buy me a coffe to help me continue supporting this project.
Buy me a coffe

Security

You could check any important security information at the security document

How-Tos

Use the appender formatter to send data to logstash

1.- To send to logstash, add the jar to your application classpath and configure a log4j2 socket appender with the host and port used by logstash. Then add to the desired loggers.

  1. <Socket name="socketAppender" host="myhost" port="9201">
  2. <JSONLog4j2Layout singleLine="true" ></JSONLog4j2Layout>
  3. </Socket>

2.- Configure logstash using tcp input in server mode with the same port.

  1. input {
  2. tcp {
  3. port => 9201
  4. mode => "server"
  5. codec => json_lines
  6. }
  7. }