AppenderRef level lower than root level? - log4j2

why can a the level of a appender ref not be lower than the root logger level?
From log4j2.xml:
...
<Loggers>
<Root level="ERROR">
<AppenderRef ref="RollingFile" level="INFO" />
</Root>
</Loggers>
...
INFO is not shown in the rollingfile, just error or higher.
The opposite way is working, when root logger has higher level and the referenced appender reduce the level:
...
<Loggers>
<Root level="DEBUG">
<AppenderRef ref="RollingFile" level="ERROR" />
</Root>
</Loggers>
...
Additinally I have the following problem: the root level=ERROR is default setting in a commercial product.
So I wan't change the level here due to support/update reasons.
(Finally, I will manage the appender ref (add/remove) programmatically.)
Any hint?
Uwe

Related

Apache James Log4j2 duplicate appender definitions

After configuring Apache James 3.7.2 with spring wiring, I noticed that the log4j2 xml configuration file defines the MAILETCONTAINER appender 4 times.
https://github.com/apache/james-project/blob/3.7.x/server/apps/spring-app/src/main/resources/log4j2.xml
<RollingFile name="MAILETCONTAINER" fileName="${logDir}/james_mailetcontainer.log"
<RollingFile name="MAILETCONTAINER" fileName="${logDir}/james_mailetcontainer.log"
<RollingFile name="MAILETCONTAINER" fileName="${logDir}/james_mailetcontainer.log"
<RollingFile name="MAILETCONTAINER" fileName="${logDir}/james_mailetcontainer.log"
<Logger name="org.apache.james.mailetcontainer" additivity="false">
<Logger name="org.apache.james.mailetcontainer" additivity="false">
<Logger name="org.apache.james.mailetcontainer" additivity="false">
<Logger name="org.apache.james.mailetcontainer" additivity="false">
Is this simply a misconfiguration?
I have deleted the duplicate entries on my local server and the logging behaviour appears to not have been adversely affected.

log4j:WARN No appenders could be found for logger (org.apache.hc.client5.http.impl.classic.InternalHttpClient)

I've implemented a simple Java app that uses httpclient 5.1.3. I'm also using log4j2 (2.1.9) and have configured it emit log entries at the wire level. I know the log4j2 config is loaded because I can see my file appender working when I emit log messages from my app.
I don't understand why httpclient logging reports no appenders for org.apache.hc.client5.http.impl.classic.InternalHttpClient or for the asynch class if I change my app to use asynch protocol. Please help.
My log4j2 config:
<Appenders>
<Console name="Console">
<PatternLayout pattern="%d %-5level [%logger] %msg%n%xThrowable" />
</Console>
<File name="MyFile" fileName="c:/a/app.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="org.apache.hc.client5.http" level="DEBUG">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="org.apache.hc.client5.http.wire" level="DEBUG">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="org.apache.hc.client5.http.impl.classic.InternalHttpClient" level="DEBUG">
<AppenderRef ref="Console"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="MyFile" />
</Root>
</Loggers>
</Configuration>
I could sure use some help. Thanks.

How to configure log4j2 with two root levels?

I would like to configure log4j2 the following way:
Report ERROR events to Sentry.
Report INFO events to log file.
Something like:
<Loggers>
<!-- ERROR events are reported to Sentry. -->
<Root level="error">
<AppenderRef ref="Sentry" />
</Root>
<!-- INFO events are reported to log file. -->
<Root level="info">
<AppenderRef ref="myLog" />
</Root>
But of course I get error "Configuration has multiple root loggers. There can be only one.".
You can specify log level for appender as well -
<Loggers>
<Root>
<AppenderRef ref="Sentry" level="error "/>
<AppenderRef ref="myLog" level="info" />
</Root>
</Loggers>
With this configuration, error and above level logs will be sent to Sentry appender. And info and above level logs (including error) will be sent to myLog
Workaround that works for me:
<!-- ERROR events are reported to Sentry. -->
<Root level="error">
<AppenderRef ref="Sentry" />
</Root>
<!-- INFO events are reported to myLog. -->
<Logger name="com" level="info">
<AppenderRef ref="myLog" />
</Logger>
<Logger name="org" level="info">
<AppenderRef ref="myLog" />
</Logger>
I approached similar issue.
I've tried both suggested solutions and any of those were working for me. After few hours of trying to figure it out I have found out on logging.apache.org solution very similar to suggested one by Vikas Sachdeva.
<Loggers>
<Root level="info">
<AppenderRef ref="Sentry" level="error "/>
<AppenderRef ref="myLog" level="info" />
</Root>
</Loggers>
Without setting up level info directly in root my app was logging nothing. Adding this small change everything works great.

Log4j 2 console output stop working

I've been using log4j 2 (2.7) and it worked very well until I had to reinstall the IDE (Eclipse Neon J2EE edition).
Now I only works loggingto a log file but not to the console.
This is my log4j2.xml file:
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="log" fileName="target/test.log" append="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M %msg%xEx%n"/>
</File>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
<Root level="TRACE">
<AppenderRef ref="log" />
</Root>
</Loggers>
The output to the log works perfectly, as I said.
Try declaring Root only once.
Example:
<Loggers>
<Root>
<AppenderRef ref="Console" level="error"/>
<AppenderRef ref="log" level="trace"/>
</Root>
</Loggers>
I agree with Sazzad 's answer: you can only have one root logger. I would suggest specifying a level on the root logger and override the level only on the AppenderRef that need it. So:
<Loggers>
<Root level="trace">
<AppenderRef ref="Console" level="error"/>
<AppenderRef ref="log"/>
</Root>
</Loggers>
Also, not sure if this was omitted just from the question or from the actual configuration file, but the configuration really should start with a <Configuration> and end with a </Configuration> element.

Different level of logs in different log files

How can we write a simple log4j2.xml file with different levels of logs going into different files?
For example we have error logs any info logs I need to push all error log messages into one log file and all info log messages into another file.
I want that info message in InfoController.log file and error message in LoginController.log file
How can i do it?
Finally got the answer by doing this i got the logs in different files.
<Loggers>
<logger name="com.mvc.login" level="info" additivity="false">
<AppenderRef ref="LoginController" level="error"/>
<AppenderRef ref="InfoController" level="info"/>
</logger>
</Loggers>
Loggers must have unique names. The second logger in your config will replace the first one, which explains why you don't see any output for the appender referenced by the first logger.
Normally, a configuration has a root logger that will receive all events. I would recommend that, so that part of the config becomes:
<Loggers>
<logger name="com.mvc.login" level="error" additivity="false">
<AppenderRef ref="LoginController"/>
</logger>
<root level="trace">
<AppenderRef ref="InfoController"/>
</root>
</Loggers>
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="ACCEPT" />
</Filters>
This will print only Info log.If you assign level error then it will print only warn message etc.

Resources