Elasticsearch config with log4j2.xml - java

I'm iniatilizing the elasticsearch client with new PreBuiltTransportClient(Settings.EMPTY); and I have the following config:
pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.1</version>
</dependency>
since log4j2 takes the xml file ==> log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
</Console>
<RollingFile name="RollingFile" filename="log/rolling.log"
filepattern="${logPath}/%d{YYYYMMddHHmmss}-rolling.log">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
<logger name="org.springframework.web">
<level value="info" />
<appender-ref ref="Console" />
</logger>
</Loggers>
</Configuration>
but the initialization throws nasty config exceptions, namely
Error while converting string [] to type [class.org.apache.logging.log4j.Level]. Using default value [null]. java.lang.IllegalArgumentException: Unknown level constant [].
I would have expected some config is missing for the logging. Searching for proper config I found only hints for the log4j.properties files - which I don't want to use. I guess I need to configure an appropriate logger name - but don't know which. org.elasticsearch.common.logging did not help.
How to configure it properly?

I think the error lies in your xml file. Inside your loggers tag you have defined a logger incorrectly. Could you try the following?
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile" />
</Root>
<Logger name="corg.springframework.web" level="INFO">
<AppenderRef ref="Console"/>
</Logger>
</Loggers>
The level needs to be inside the logger line, instead of a separate level line
Source: log4j

Related

Stop Debug Statements from publishing in log

I have the following log.properties file in my Java project:
handlers= java.util.logging.ConsoleHandler
.level= INFO
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$s] %5$s %n
I am declaring the Logger as follows in one file which generates most of the log:
public static final Logger logger = LogManager.getLogger();
The log4j2.xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval ="5">
<Properties>
<Property name="log-path">${LOGDIR}</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile"
fileName="workerApplication.log"
filePattern="workerApplication-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<!-- <DefaultRolloverStrategy max="4" /> -->
</RollingFile>
</Appenders>
<Loggers>
<Logger name="root" level="warn" additivity="false">
<appender-ref ref="RollingFile" />
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
In my workerApplication.log, I am still getting the debug statements too that are in my code. I only want INFO and above level. I have changed in both places the level of the log but it is not working. Can anyone point out what am I doing wrong? The project is being deployed as an RPM in Linux
You set the loglevel for each logger in log4j2.xml. Since you only have one logger you can change the loglevel for that logger to only get logging for level info and higher.
Please change
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
</Root>
to
<Root level="info" additivity="false">
<AppenderRef ref="RollingFile" />
</Root>

log4j 2 not showing dependency logs

I am using apache HttpClient to execute a POST request:
CloseableHttpResponse response = HttpClients.createDefault().execute(request);
I want to see the request (and other apache client library log statements) in the logs, but I can only see my application logs, no other logs from any dependency.
Here is my log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Lambda name="Lambda">
<PatternLayout>
<pattern>%d %X{AWSRequestId} %t %-5p [%X{userId}] %c{1}:%L - %m%n</pattern>
</PatternLayout>
</Lambda>
</Appenders>
<Loggers>
<Logger name="org.apache.http" level="debug">
<AppenderRef ref="Lambda"/>
</Logger>
<Root level="debug">
<AppenderRef ref="Lambda"/>
</Root>
</Loggers>
</Configuration>
Using this answer, I added the following, but it still doesn't show the apache logs.
<Logger name="org.apache.http.client" level="debug">
<AppenderRef ref="Lambda"/>
</Logger>
<Logger name="org.apache.http.impl.client" level="debug">
<AppenderRef ref="Lambda"/>
</Logger>
<Logger name="org.apache.http.impl.conn" level="debug">
<AppenderRef ref="Lambda"/>
</Logger>
I can confirm that this log4j2.xml is being used by log4j because the logs are following the <pattern> I wrote.
What am I missing? Please help.
Adding the Commons Logging Bridge dependency fixed it:-
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.11.0</version>
</dependency>
See this for more details.

Why does log4j2 not log to a file after packaging as a jar in Eclipse?

I'm new to java, and I've written a simple java program to test a few things out before attempting the same things with the more complicated app that I'm almost ready to deploy. I'm using Eclipse, and I've exported the package as a jar file. When I run the jar file, everything appears to be working correctly. Log4j2 is logging properly to the console, and the application is doing what I want it to. The problem I'm having is that the log files are not being created / written to. This all works correctly when I'm running the app from Eclipse. What I was expecting is that I could move the .jar file anywhere I wanted. That I could then execute the app and a logs folder would be created in that same directory and a trace log and an error log would be created and written to.
Here is the log4j2.xml that I'm using:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<RollingFile name="trace-log" fileName="${log-path}/logging-trace.log" filePattern="${log-path}/logging-trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/logging-error.log" filePattern="${log-path}/logging-error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
</Root>
<Logger name="multithreadTest" level="info" additivity="false">
<appender-ref ref="trace-log" level="debug"/>
<appender-ref ref="error-log" level="error"/>
<appender-ref ref="console-log" level="debug"/>
</Logger>
</Loggers>
</Configuration>
Here are the contents of the manifest file:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ log4j-api-2.5.jar log4j-core-2.5.jar
Class-Path: .
Rsrc-Main-Class: multithreadTest.RunTest
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
I'm executing the application like this:
java -jar mtest.jar
Any help would be greatly appreciated.
add ref to all your loogers
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
<AppenderRef ref="trace-log"/>
<AppenderRef ref="error-log"/>
</Root>
Thanks dom farr. I changed this:
<Logger name="multithreadTest" level="info" additivity="false">
<appender-ref ref="trace-log" level="debug"/>
<appender-ref ref="error-log" level="error"/>
<appender-ref ref="console-log" level="debug"/>
</Logger>
to this:
<Logger name="multithreadTest" level="info" additivity="false">
<AppenderRef ref="trace-log" level="debug"/>
<AppenderRef ref="error-log" level="error"/>
<AppenderRef ref="console-log" level="debug"/>
</Logger>
and it is now working correctly. I'm not sure why. I guess I need to read more about the difference between the two. Not sure why it would work correctly in Eclipse if this is incorrect XML. Anyway, it is working now. Thanks again.

Log4j2: Suppress only DEBUG messages

I am getting a lot of DEBUG messages in my logs. I want to suppress these, while preserving the INFO and ERROR messages. Here is my log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<!-- Author: Crunchify.com -->
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %5p %c - %m%n" />
</Console>
<RollingFile name="RollingFile" filename="test.log"
filepattern="${LOG_PATH}${LOG_NAME}Log.%d{yyyyMMdd}.log.gz">
<PatternLayout pattern="%d [%t] %5p %c - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<NoSql name="elasticAppender">
<Elasticsearch cluster="test" host="test" port="test" index="test" type="log4j2"/>
</NoSql>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="NoSql" />
<AppenderRef ref="RollingFile" />
</Root>
<Logger name="test" level="info" additivity="false">
<AppenderRef ref="RollingFile" />
</Logger>
<Logger name="test" level="info">
</Logger>
<Logger name="test" level="INFO">
</Logger>
<Logger name="com.datastax.driver.core" level="INFO">
</Logger>
</Loggers>
</Configuration>
Does anyone know what change needs to be made here?
According to the Log4j manual, you might want to change <Root level="debug"> to <Root level="info">.

log4j2 slf4j logging only error

Only error logs are showing in console. No debug, info, warn logs are showing up. Below are the configurations. Can you please let me know what i am doing wrong.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.0-rc1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0-rc1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0-rc1</version>
</dependency>
I used the above depnedencies with the below xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="A1" fileName="A1.log" append="false">
<PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.log4j.xml" level="debug">
<AppenderRef ref="A1"/>
</Logger>
<Logger name="com.mypackage" level="debug">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="A1"/>
</Logger>
<Logger name="org.apache.log4j.xml" level="info"/>
<Root level="info">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
I used the below code to log:
logger.trace("Entering application.");
logger.info("Test");
System.out.println("Hello");
logger.debug("dbg");
logger.error("dbg");
logger.warn("dbgfatal");
logger.error("dbg error");
The config file should be named log4j2.xml and be located in the classpath.
your root level should be lower : debug or trace
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
Your root level should be "debug" and appender ref should be "Console":
<root level='debug'>
<appender-ref ref='Console'/>
</root>`

Categories

Resources