Different level of logs in different log files - log4j2

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.

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.

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

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.

Log4j2 daily log files

I want my log files created by log4j2 to have the date pattern in their file name, including the current active file. That is, if todays date is 2016-12-15, I want the current log-file to be lager-2016-12-15.log. When the date changes, I want a new file to be created named lager-2016-12-16.log.
With RollingFileAppender I am not able to get the current active log file to have date pattern in the filename. My Log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="layoutPattern">%d{ISO8601} [%t] %-5p [%X{REQUEST_ID}] [%X{CLIENT_ID}] [%X{USER_ID}] %c- %m%n</Property>
<Property name="logDir">${sys:catalina.home}/logs/</Property>
<Property name="fileName">${logDir}lager-${date:yyyy-MM-dd-HHmm}.log</Property>
<Property name="filePattern">${logDir}lager-%d{yyyy-MM-dd-HHmm}.log</Property>
</Properties>
<Appenders>
<RollingFile name="LAGER" append="true"
fileName="${fileName}"
filePattern="${filePattern}">
<PatternLayout pattern="${layoutPattern}" charset="UTF-8"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<loggers>
<Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]" level="WARN" additivity="false">
<AppenderRef ref="LAGER" />
<AppenderRef ref="CONSOLE"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="LAGER"/>
<AppenderRef ref="CONSOLE"/>
</Root>
</loggers>
</Configuration>
With this config the copying when it rolls over is messed up. Removing the date pattern from the fileName property fixes this, but the the current file does not have the date in its name.
I am running this on a tomee 7.0.1.
There may already be an outstanding feature request for this. Would this match your requirements? https://issues.apache.org/jira/browse/LOG4J2-1101
If so, please comment on that JIRA ticket. If you're able to contribute a patch (ideally with unit test) it is likely to get resolved quickly.

Resources