Logging embedded elasticsearch logs to file with log4j2

Hi, I'm using elasticsearch 2.3 embedded in my Java program and I want to log generate logs similar to how the standalone elasticsearch does logging. Can you please point me in the right direction ?

I've tried this so far.

Settings elasticsearchSettings = Settings.settingsBuilder()
.put("path.conf", "elasticsearch/conf")
.put("node.name", "chin chao")
.put("path.home", "elasticsearch")
.put("path.data", "elasticsearch/data")
.put("path.logs", "elasticsearch/logs")
.put("cluster.name", "elasticsearch")
.build();

LogConfigurator.configure(elasticsearchSettings, true);

But I am getting following error since I am using log4j2 in my program.

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/PropertyConfigurator

This is not supported. Running embedded in general isn't supported.

It might work if you bridge log4j1 to log4j2 but since this isn't a thing
we it might break unexpectedly.

1 Like

First, the cause of the error No log4j2 configuration file found is that you do not have a log4j2.xml file in your class path.

Second, as Nik already commented, you need a bridge jar https://logging.apache.org/log4j/log4j-2.6.1/log4j-1.2-api/index.html so all log4j messages are redirected to log4j2

You should ignore org.elasticsearch.common.logging.LogConfigurator, instead, use org.apache.logging.log4j.core.config.ConfigurationFactory and related classes to control logging programmatically. See https://logging.apache.org/log4j/log4j-2.6.1/manual/customconfig.html

1 Like