https://github.com/dubasdey/log4j2-jsonevent-layout
Log4j2 JSON Event Layout without requirement of thirdparty libraries
https://github.com/dubasdey/log4j2-jsonevent-layout
json log log4j log4j2 log4j2-layout logging-library logstash
Last synced: 8 months ago
JSON representation
Log4j2 JSON Event Layout without requirement of thirdparty libraries
- Host: GitHub
- URL: https://github.com/dubasdey/log4j2-jsonevent-layout
- Owner: dubasdey
- License: lgpl-3.0
- Created: 2017-05-03T09:40:51.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-09-25T17:20:26.000Z (almost 2 years ago)
- Last Synced: 2025-05-07T18:12:56.573Z (about 1 year ago)
- Topics: json, log, log4j, log4j2, log4j2-layout, logging-library, logstash
- Language: Java
- Size: 119 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# log4j2-jsonevent-layout
[](https://raw.githubusercontent.com/dubasdey/log4j2-jsonevent-layout/master/LICENSE)
[](https://github.com/dubasdey/log4j2-jsonevent-layout/issues)
[](https://github.com/dubasdey/log4j2-jsonevent-layout/actions/workflows/maven.yml)
[](https://search.maven.org/artifact/com.github.dubasdey/log4j2-jsonevent-layout)
[](https://www.codacy.com/gh/dubasdey/log4j2-jsonevent-layout/dashboard?utm_source=github.com&utm_medium=referral&utm_content=dubasdey/log4j2-jsonevent-layout&utm_campaign=Badge_Grade)
[](https://www.codacy.com/gh/dubasdey/log4j2-jsonevent-layout/dashboard?utm_source=github.com&utm_medium=referral&utm_content=dubasdey/log4j2-jsonevent-layout&utm_campaign=Badge_Coverage)
Log4j2 JSON Event Layout without requirement of thirdparty libraries
## Usage
Add the `` 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:
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
```json
{
"@timestamp":"2017-05-03T15:46:34.393Z",
"logger_name":"org.dummy.logger",
"level":"DEBUG",
"level_int":500,
"thread_name":"DummyThread",
"source_host":"MYLOCALMACHINE",
"source_ip":"192.168.1.10",
"message":"Dummy Message Test with tab T\tT before here.",
"file":"File.java",
"line_number":42,
"class":"ClassName.class",
"method":"MethodElement",
"contextStack":["Entry 1","Entry 2"],
"contextMap":
[
{"X-Generator":"JSONLog2j2Layout"},
{"ThreadContextKey":"ThreadContextValue"}
],
"@version":"1"
}
```
## Maven dependency
To include this library in your project just add the dependency to your maven project
Example:
```
com.github.dubasdey
log4j2-jsonevent-layout
0.1.0
```
## 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](SECURITY.md)
# 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.
```xml
```
2.- Configure logstash using tcp input in server mode with the same port.
```
input {
tcp {
port => 9201
mode => "server"
codec => json_lines
}
}
```