-
Notifications
You must be signed in to change notification settings - Fork 4
Appender 1.x ‐ Log4j 1.x
Chavjoh edited this page Nov 3, 2024
·
4 revisions
In the configuration file, add a new appender ElasticsearchAppender
using package com.chavaillaz.appender.log4j
with the following properties:
-
applicationName
(defaultunknown
). It can also be specified as environment variable or system propertyAPPLICATION
. -
hostName
(default is the machine host name). It can also be specified as environment variable or system propertyHOST
. -
environmentName
(defaultlocal
). It can be specified also as environment variable or system propertyENV
. -
elasticConverter
(defaultcom.chavaillaz.appender.log4j.converter.DefaultEventConverter
) is the class used to convert a logging event into a key/value document to be stored in Elasticsearch. It can also be specified as environment variable or system propertyCONVERTER
. -
elasticIndex
(defaultha
) andelasticIndexSuffix
(default-yyyy.MM.dd
) form together the index name where the messages are sent to. Note thatelasticIndexSuffix
must contain a format pattern suitable forDateTimeFormatter
. They can also both be specified with environment variables or system propertiesINDEX
andINDEX_SUFFIX
. -
elasticUrl
is the address of the server (or its load balancer) in the format[scheme://][host]:[port]
. The scheme is optional and defaults tohttp
. -
elasticApiKey
(encoded) orelasticUser
withelasticPassword
are the credentials for the server. -
elasticParallelExecution
(defaulttrue
) specifies the way the messages are sent to the server (true
send them in a separate thread andfalse
send them sequentially). -
elasticBatchSize
(default1
) is the number of messages threshold triggering the sending to the server. -
elasticBatchInitialDelay
(default1000
) is the time in milliseconds before a first batch of messages is sent to the server (after appender startup, even if the batch of messages is incomplete, meaning not reaching the threshold). -
elasticBatchDelay
(default1000
) is the time in milliseconds between two cleanups of incomplete batches. If after this time there are less thanelasticBatchSize
messages waiting to be sent, the batch is sent nonetheless. Note that onceelasticBatchSize
messages are waiting, the batch is sent immediately, without any delay.
Note that elasticUrl
is the only mandatory configuration to give, except if you need to overwrite the default value of another ones.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %p (%t) [%c{1}::%M] - %m%n"/>
</layout>
</appender>
<appender name="ElasticAppender" class="com.chavaillaz.appender.log4j.elastic.ElasticsearchAppender">
<param name="applicationName" value="myApplication"/>
<param name="environmentName" value="myEnvironment"/>
<param name="elasticConverter" value="com.package.CustomEventConverter"/>
<param name="elasticUrl" value="myElasticsearchUrl"/>
<param name="elasticIndex" value="myIndex"/>
<param name="elasticIndexSuffix" value="-yyyy.MM.dd"/>
<param name="elasticUser" value="myUser"/>
<param name="elasticPassword" value="myPassword"/>
<param name="elasticParallelExecution" value="true"/>
<param name="elasticBatchSize" value="10"/>
<param name="elasticBatchInitialDelay" value="1000"/>
<param name="elasticBatchDelay" value="1000"/>
</appender>
<!-- Avoid to propagate the logs from ElasticAppender to itself (cyclic) -->
<category name="com.chavaillaz.appender" additivity="false">
<priority value="warn"/>
<appender-ref ref="ConsoleAppender"/>
</category>
<root>
<priority value="info"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="ElasticAppender"/>
</root>
</log4j:configuration>
log4j.appender.ELASTIC=com.chavaillaz.appender.log4j.elastic.ElasticsearchAppender
log4j.appender.ELASTIC.applicationName=myApplication
log4j.appender.ELASTIC.environmentName=myEnvironment
log4j.appender.ELASTIC.elasticConverter=com.package.CustomEventConverter
log4j.appender.ELASTIC.elasticUrl=myElasticsearchUrl
log4j.appender.ELASTIC.elasticIndex=myIndex
log4j.appender.ELASTIC.elasticIndexSuffix=-yyyy.MM.dd
log4j.appender.ELASTIC.elasticUser=myUser
log4j.appender.ELASTIC.elasticPassword=myPassword
log4j.appender.ELASTIC.elasticParallelExecution=true
log4j.appender.ELASTIC.elasticBatchSize=10
log4j.appender.ELASTIC.elasticBatchInitialDelay=1000
log4j.appender.ELASTIC.elasticBatchDelay=1000
# Avoid to propagate the logs from ElasticAppender to itself (cyclic)
log4j.logger.com.chavaillaz.appender=WARN, CONSOLE
log4j.logger.org.apache.http=WARN, CONSOLE
log4j.logger.org.elasticsearch.client=WARN, CONSOLE
log4j.additivity.com.chavaillaz.appender=false
log4j.additivity.org.apache.http=false
log4j.additivity.org.elasticsearch.client=false