How to configure log4j2 with two root levels? - log4j2

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.

Related

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.

AppenderRef level lower than root level?

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

log4j2 config file not logging to console/file

I have the following log4j2.xml config that I am using.
I have 2 issues :-
When my application runs up, I have nothing logged to either the
console or to mylog.log file by either of apache or springframework or
hibernate classes.
Classes within the package "com.foo.bar", I am able change the log
LEVEL via "level" attribute but the messages are only logged to
mylog.log file but nothing to the console
Any suggestions to above would be appreciated ?
thank you
Pete
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{DEFAULT} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="File" fileName="mylog.log"
filePattern="mylog-%d{yyyy-MM-dd}.%i.log">
<PatternLayout pattern="%d{DEFAULT} [%t] %-5level %logger{36} - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="30 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.foo.bar" level="info" additivity="false">
<AppenderRef ref="File" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="org.apache" level="debug" >
<AppenderRef ref="File" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="org.hibernate" level="debug" additivity="false">
<AppenderRef ref="File" />
</Logger>
<Logger name="org.springframework" level="debug" additivity="false">
<AppenderRef ref="File" />
</Logger>
<Root level="all">
<AppenderRef ref="Console" />
<AppenderRef ref="File" />
</Root>
</Loggers>
Please try replacing
<Root level="all">
With
<Root level="trace">
It sounds like you have a misconfigured log.
Can you debug remotely and where you find use the logger to log something place a breakpoint. Inspect the logger and then you will find a location of where its picking up the log file from.
Also try providing a VM argument for log4j configuration (-Dlog4j.configuration)on start-up of your application.

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