Configure logging for plugin


#1

Hello,

I'm developing a plugin for Elasticsearch. Therefore I want to log into a log file with log4j2. The configuration of log4j2 is defined in a log4j2.xml in my resources folder. For unit tests everything works fine, but if I install and run the plugin in Elasticsearch these settings are ignored. Because of that I tried to configure it with the log4j2.properties of Elasticsearch:

...
logger.file.name = de.sampleplugin
logger.file.level = info
logger.file.additivity = false
logger.file.appenderRef.file.ref = LogFile

appender.file.type = File
appender.file.name = LogFile
appender.file.fileName=${sys:es.logs}_sampleplugin.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
...

This works as expected, but if I'm running integration tests with setting up a Elasticsearch instance by an ant script. The settings are ignored again (probably because the default log4j2.properties is used).

How can I configure the logging for my plugin without using the log4j2.properties of Elasticsearch to get the same behavior?

Thanks in advance!


Separate logging file for pluging
(Daniel Mitterdorfer) #2

Hi @ma-sch,

you cannot have more than one log4j config file. You should place your test configuration in /src/test/resources of your plugin. When you install it, Elasticsearch will always use the logging configuration file in /config; otherwise you'd need a dedicated log file per plugin.

I guess that your integration tests are based on David's blog post about ES integration tests? In that case you could sneak in your custom log configuration by an Ant copy task before it starts Elasticsearch. Alternatively, you could configure a different home directory for Elasticsearch by setting -Ees.path.home=/your/custom/path and place the config there. You just need to ensure to clean up properly (so this could get a bit hairy when tests fail and abort the build...)

Daniel


#3

Thanks a lot, that works! I used the Ant copy task to override the logging configuration with my own log4j2.properties file from resources folder.

If someone else has the same problem. Just put this copy task in the ant script after Elasticsearch has been unzipped:
<copy file="${project.build.sourceDirectory}/../resources/log4j2.properties" tofile="${project.build.directory}/integration-tests/run/elasticsearch-5.0.0/config/log4j2.properties" overwrite="true" />


(system) #4

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.