Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LOG4J2: how to configure JSON layout in properties file

Does anyone know, how to write a log4j2 properties file, which outputs the logs to the console as JSON ?

i saw this link, https://logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout, but it is not clear for me, how do the configuration in a properties file.

thanks, Eran

like image 859
EranM Avatar asked Dec 28 '16 12:12

EranM


People also ask

Can log4j2 use log4j properties file?

Log4j 2 doesn't support the Log4j v1 ". properties" format anymore (yet, since v2. 4, Log4j supports a Property format, but its syntax is totally different from v1 format). New formats are XML, JSON, and YAML, see the documentation (note: if you used one of these formats in a file called ".

Does log4j support JSON format?

json provided by the log4j-layout-template-json artifact, which contains the following predefined event templates: EcsLayout. json described by the Elastic Common Schema (ECS) specification.

What is pattern layout in log4j2?

The PatternLayout class extends the abstract org. apache. log4j. Layout class and overrides the format() method to structure the logging information according to a supplied pattern.


2 Answers

Furthermore, some options can be set in the log4j2.properties:

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=myLog.json
appender.file.layout.type=JsonLayout
appender.file.layout.compact=true
appender.file.layout.eventEol=true
appender.file.layout.complete=true
appender.file.layout.properties=false
appender.file.layout.propertiesAsList=false
appender.file.layout.locationInfo=true
appender.file.layout.includeStacktrace=true
appender.file.layout.stacktraceAsString=true
appender.file.layout.includeNullDelimiter=false
appender.file.layout.objectMessageAsJsonObject=true

Please refer to the log4j2 documentation for the properties definition: https://logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout

like image 166
antinmaze Avatar answered Nov 15 '22 03:11

antinmaze


I found this Tutorial from the Springframework guru. My log4j2.properties file looks as follows;

name=JsonConfig

appenders = file

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=myLog.json
appender.file.layout.type=JsonLayout

loggers=file
logger.file.name=your.logger.package.location
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE

Hope this helps.

like image 37
Laazo Avatar answered Nov 15 '22 04:11

Laazo