log4j2 slf4j logging only error - java

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>`

Related

Turning off Log4j2 by setting Root level to "off" doesnt work

I have a problem with logging within Junit tests.
In log4j2.xml, I have changed <Root level="info"> to <Root level="off">:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %-5p %c{1.} - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="off">
<AppenderRef ref="STDOUT"/>
</Root>
<!-- hibernate debugging -->
<Logger name="org.hibernate.SQL" level="DEBUG">
<AppenderRef ref="STDOUT" />
</Logger>
<Logger name="org.hibernate.type" level="TRACE">
<AppenderRef ref="STDOUT" />
</Logger>
</Loggers>
</Configuration>
But it doesn't help, I still get the same amount of logs. I need to make the logs dissapear to make progress in an test performance investigation, to be able to run my own allways despised System.out.println() in some of the tests. How can I achive that?

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.

Elasticsearch config with log4j2.xml

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

No logs created unless it tests

I'm using log4j2 for logging in my app. Basically it's logging uncatched exceptions (Error.log) and logging changes of my data at service layer (journal.log; journalJSON.log).
And here's the thing, when I'm starting service layer tests every log appears in the file and console, but when I'm using application deployed in tomcat they're only in console. What's happening?
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="Error" fileName="logs/error.log"
immediateFlush="true" append="true">
<PatternLayout pattern=" \n\n %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<RandomAccessFile name="Journal" fileName="logs/journal.log" immediateFlush="true" append="true">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] %msg%n"/>
</RandomAccessFile>
<File name="JournalJSON" fileName="logs/journalJSON.log" immediateFlush="true" append="false">
<JSONLayout complete="true" charset="UTF-8" compact="false" eventEol="false"/>
</File>
</appenders>
<Loggers>
<Logger name="com.user.controller" level="error" additivity="false">
<AppenderRef ref="Error" level="error"/>
<AppenderRef ref="Console" level="error"/>
</Logger>
<Logger name="com.user.service" level="info" additivity="false">
<appender-ref ref="JournalJSON" level="info"/>
<appender-ref ref="Journal" level="info"/>
<appender-ref ref="Console" level="info"/>
</Logger>
<Root level="warn">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
Well, there are a couple of possibilities I can think of:
Your configuration file isn't being found and it is using the default configuration.
The logger for your application isn't com.user.controller or com.user.service and the root logger is being used.

log4j2 issue . Unable to log any thing either in console or in a file

I just updated from log4j 1.x to log4j2. Every thing was working fine in 1st version. Now after updating to log4j2 its not logging any thing . i am pasting my code below. any one please help me out
This are my dependencies
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.0-beta9</version>
<type> jar</type>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0-beta9</version>
<type> jar</type>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0-beta9</version>
<type> jar</type>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.0-beta9</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.0-beta9</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-taglib</artifactId>
<version>2.0-beta9</version>
</dependency>
this is my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<console name="Console" target="SYSTEM_OUT" includeLocation="true">
<PatternLayout pattern="%d %-5p [%t] [%c:%L] %m%n"/>
</console>
<RollingRandomAccessFile name="Rolling" fileName="${sys:DOC_ROOT}/logs/UILogs-${sys:SERVER_NAME}-${sys:SERVER_ID}-${sys:SERVER_TYPE}.log"
filePattern="${sys:DOC_ROOT}/logs/UILogs-${sys:SERVER_NAME}-${sys:SERVER_ID}-${sys:SERVER_TYPE}-%d{yyyy-MM-dd}.log.gz">
<PatternLayout pattern="%d %-5p [%t] [%c:%L] %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
<DefaultRolloverStrategy max="180" compressionLevel="9"/>
</RollingRandomAccessFile>
<Async name="dev">
<AppenderRef ref="DevSMTP" level="fatal"/>
<AppenderRef ref="Console" level="debug"/>
<AppenderRef ref="Rolling" level="debug"/>
</Async>
<Async name="test">
<AppenderRef ref="TestSMTP" level="fatal"/>
<AppenderRef ref="Console" level="debug"/>
<AppenderRef ref="Rolling" level="debug"/>
</Async>
<Async name="prod">
<AppenderRef ref="ProdSMTP" level="fatal"/>
<AppenderRef ref="Console" level="info"/>
<AppenderRef ref="Rolling" level="info"/>
</Async>
<Async name="local">
<AppenderRef ref="DevSMTP" level="fatal"/>
<AppenderRef ref="Console" level="debug"/>
<AppenderRef ref="Rolling" level="debug"/>
</Async>
<Async name="">
<AppenderRef ref="ProdSMTP" level="fatal"/>
<AppenderRef ref="Console" level="info"/>
<AppenderRef ref="Rolling" level="info"/>
</Async>
<Async name="">
<AppenderRef ref="TestSMTP" level="fatal"/>
<AppenderRef ref="Console" level="debug"/>
<AppenderRef ref="Rolling" level="debug"/>
</Async>
<Async name="">
<AppenderRef ref="ProdSMTP" level="fatal"/>
<AppenderRef ref="Console" level="info"/>
<AppenderRef ref="Rolling" level="info"/>
</Async>
<Async name="lib">
<AppenderRef ref="Console" level="info"/>
<AppenderRef ref="Rolling" level="info"/>
</Async>
</Appenders>
<Loggers>
<Root level="debug" additivity="false">
<AppenderRef ref="${sys:SERVER_TYPE}"/>
</Root>
<root level="error">
<appender-ref ref="Console"/>
</root>
<Logger name="org.apache.ibatis" additivity="false">
<AppenderRef ref="lib"/>
</Logger>
<Logger name="java.sql" additivity="false">
<AppenderRef ref="lib"/>
</Logger>
<Logger name="RollingRandomAccessFile" additivity="true">
<AppenderRef ref="Rolling"/>
</Logger>
<logger name="com.foo.Bar" level="trace">
<appender-ref ref="Console"/>
</logger>
</Configuration>
There are a couple of things to fix in the configuration:
Dependencies:
There is a space ' ' character between <type> and jar. (Not sure if this causes any problem).
Log4j2.xml configuration:
There are three Async appenders without a name. They MUST have names.
There are two root appenders. There can be only ONE root appender in a configuration, so you need to remove one of them.
Several Loggers do not have a level defined. If your intention is for the log level filtering to be done by the appenders, I recommend that you ensure that ALL events are passed to the appenders: I recommend you specify level TRACE on these loggers.
The <Loggers> element does not have a closing </Loggers> tag. This may be the biggest problem.
Are you receiving any related error? for example if it's the error: "ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogge" try this link to solve your logging issues.

Categories

Resources