Mule Log4j2 not working - log4j

I have a log4j2 XML file in my src/main/resources as below. But Still I do not see the log file created. Any idea what could I be missing.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<File name="file" fileName="C:\\Users\\Guest\\Desktop\\Mule.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="file" level="DEBUG"/>
<AppenderRef ref="STDOUT" level="INFO"/>
</Root>
</Loggers>
</Configuration>

Log4j2 configuration works only in Mule-3.6 version+, check your Mule version and give file name to C:/Users/Guest/Desktop/Mule.log format.
Sample file:
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<Properties>
<Property name="log-path">${env:MULE_HOME}/logs/test</Property>
<Property name="log-name">test</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="info-log" fileName="${log-path}/${log-name}-info.log"
filePattern="${log-path}/${log-name}-info-%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>
<SizeBasedTriggeringPolicy size="10 MB" />
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="debug-log" fileName="${log-path}/${log-name}-debug.log"
filePattern="${log-path}/${log-name}-debug-%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>
<SizeBasedTriggeringPolicy size="10 MB" />
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="trace-log" fileName="${log-path}/${log-name}-trace.log"
filePattern="${log-path}/${log-name}-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>
<SizeBasedTriggeringPolicy size="10 MB" />
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/${log-name}-error.log"
filePattern="${log-path}/${log-name}-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>
<SizeBasedTriggeringPolicy size="10 MB" />
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<!-- CXF is used heavily by Mule for web services -->
<AsyncLogger name="org.apache.cxf" level="WARN"/>
<!-- Apache Commons tend to make a lot of noise which can clutter the log-->
<AsyncLogger name="org.apache" level="WARN"/>
<!-- Reduce startup noise -->
<AsyncLogger name="org.springframework.beans.factory" level="WARN"/>
<!-- Mule classes -->
<AsyncLogger name="org.mule" level="INFO"/>
<AsyncLogger name="com.mulesoft" level="INFO"/>
<!-- Reduce DM verbosity -->
<AsyncLogger name="org.jetel" level="WARN"/>
<AsyncLogger name="Tracking" level="WARN"/>
<AsyncRoot level="DEBUG">
<appender-ref ref="info-log" level="info" />
<appender-ref ref="trace-log" level="trace" />
<appender-ref ref="debug-log" level="debug" />
<appender-ref ref="error-log" level="error" />
<appender-ref ref="console-log" level="debug" />
</AsyncRoot>
</Loggers>
</Configuration>
For studio, specify location of MULE_HOME in Run Configuration->Environment->new->Name->MULE_HOME and Value->c:/test

Try changing fileName to fileName="C:/Users/Guest/Desktop/Mule.log"

Related

Block due to log4j - org.apache.logging.log4j.core.layout.TextEncoderHelper.copyDataToDestination

I am finding the below issue when I ran my profiler
BLOCKED CPU usage on sample: 62ms
org.apache.logging.log4j.core.layout.TextEncoderHelper.copyDataToDestination(ByteBuffer, ByteBufferDestination) TextEncoderHelper.java:61
org.apache.logging.log4j.core.layout.TextEncoderHelper.encodeTextWithCopy(CharsetEncoder, CharBuffer, ByteBuffer, StringBuilder, ByteBufferDestination) TextEncoderHelper.java:57
(or)
State: BLOCKED CPU usage on sample: 5ms
org.apache.logging.log4j.core.layout.StringBuilderEncoder.encodeSynchronized(CharsetEncoder, CharBuffer, StringBuilder, ByteBufferDestination) StringBuilderEncoder.java:83
org.apache.logging.log4j.core.layout.StringBuilderEncoder.encode(StringBuilder, ByteBufferDestination) StringBuilderEncoder.java:61
org.apache.logging.log4j.core.layout.StringBuilderEncoder.encode(Object, ByteBufferDestination) StringBuilderEncoder.java:32
org.apache.logging.log4j.core.layout.PatternLayout.encode(LogEvent, ByteBufferDestination) PatternLayout.java:219
org.apache.logging.log4j.core.layout.PatternLayout.encode(Object, ByteBufferDestination) PatternLayout.java:57
org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.directEncodeEvent(LogEvent) AbstractOutputStreamAppender.java:177
org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(LogEvent) AbstractOutputStreamAppender.java:170
org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(LogEvent) AbstractOutputStreamAppender.java:161
org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(LogEvent) AppenderControl.java:156
org.apache.logging.log4j.core.config.AppenderControl.callAppender0(LogEvent) AppenderControl.java:129
org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(LogEvent) AppenderControl.java:120
org.apache.logging.log4j.core.config.AppenderControl.callAppender(LogEvent) AppenderControl.java:84
org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LogEvent) LoggerConfig.java:448
org.apache.logging.log4j.core.async.AsyncLoggerConfig.callAppendersInCurrentThread(LogEvent) AsyncLoggerConfig.java:106
org.apache.logging.log4j.core.async.EventRoute$2.logMessage(AsyncLoggerConfig, LogEvent) EventRoute.java:65
org.apache.logging.log4j.core.async.AsyncLoggerConfig.callAppenders(LogEvent) AsyncLoggerConfig.java:96
org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LogEvent) LoggerConfig.java:433
org.apache.logging.log4j.core.config.LoggerConfig.log(LogEvent) LoggerConfig.java:417
org.apache.logging.log4j.core.config.LoggerConfig.log(String, String, Marker, Level, Message, Throwable) LoggerConfig.java:403
org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(Supplier, String, String, Marker, Level, Message, Throwable) AwaitCompletionReliabilityStrategy.java:63
org.apache.logging.log4j.core.Logger.logMessage(String, Level, Marker, Message, Throwable) Logger.java:146
org.mule.module.launcher.log4j2.DispatchingLogger.logMessage(String, Level, Marker, Message, Throwable) DispatchingLogger.java:157 <2 recursive calls>
org.apache.logging.slf4j.Log4jLogger.log(Marker, String, int, String, Object[], Throwable) Log4jLogger.java:376
org.apache.commons.logging.impl.SLF4JLocationAwareLog.warn(Object) SLF4JLocationAwareLog.java:179
org.mule.api.processor.LoggerMessageProcessor$LogLevel$2.log(Log, Object) LoggerMessageProcessor.java:137
org.mule.api.processor.LoggerMessageProcessor.log(MuleEvent) LoggerMessageProcessor.java:81
Most of my blocked threads are pointing to this. Can any one help me on this? I am running mule application and when I tried to dig in the delay in mule processing, I found it landed into blocks due to log4j
What things can I try to resolve this?
log4j.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="debug">
<!-- ================================================================================== -->
<!-- Appenders -->
<!-- -->
<!-- Loggers can be associated with 1 or more appenders. -->
<!-- rolling_file_appender is the default appender in this file. -->
<!-- STDOUT appender can be added to Loggers below if needed. -->
<!-- ================================================================================== -->
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%p] %c{3} | %m%n" />
</Console>
<RollingFile name="application-log" fileName="${env:LOG_HOME}/test-esb-bsns/application.log" filePattern="${env:LOG_HOME}/test-esb-bsns/application-%i.log" append="true" bufferedIO="true">
<PatternLayout pattern="%d [%p] %c{3} | %X{mule.flow} | %X{mule.messageId}#%X{mule.correlationId} | %X{mule.incomingMessageId} | %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="mule_Logger" fileName="${env:LOG_HOME}/test-esb-bsns/mule-server.log" filePattern="${env:LOG_HOME}/test-esb-bsns/mule-server-%i.log" append="true" bufferedIO="true">
<PatternLayout pattern="%d [%p] %c{3} | %X{mule.flow} | %X{mule.messageId}#%X{mule.correlationId} | %X{mule.incomingMessageId} | %m%n "/>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<!-- ========================================================================================== -->
<!-- possible Logger threshold level values: all < debug < info < warn < error < fatal < off -->
<!-- ========================================================================================== -->
<!-- <Logger name="central.Logger"> -->
<!-- <level value="off" /> -->
<!-- <appender-ref ref="STDOUT" /> -->
<!-- </Logger> -->
<!-- ===================================================================================== -->
<!-- com.comverse.* will inherit these value unless overridden below -->
<!-- Allows for logging of info, warn, error and fatal messages to the specified appenders -->
<!-- ===================================================================================== -->
<Loggers>
<AsyncLogger name="org.mule" level="INFO" additivity="false">
<appender-ref ref="mule_Logger" />
<appender-ref ref="STDOUT" />
</AsyncLogger>
<!-- <AsyncLogger name="com.test.esb" level="DEBUG" additivity="false">
<appender-ref ref="application-log" />
<appender-ref ref="application-log" />
</AsyncLogger> -->
<AsyncRoot level="INFO">
<appender-ref ref="application-log" />
<appender-ref ref="STDOUT" />
</AsyncRoot>
<!-- CXF Loggers -->
<AsyncLogger name="org.apache.cxf" level="INFO" additivity="false">
<appender-ref ref="application-log" />
<appender-ref ref="STDOUT" />
</AsyncLogger>
<AsyncLogger name="com.ars.test" level="info" additivity="false">
<appender-ref ref="application-log" />
<appender-ref ref="STDOUT" />
</AsyncLogger>
<AsyncLogger name="com.test" level="INFO" additivity="false">
<appender-ref ref="application-log" />
<appender-ref ref="STDOUT" />
</AsyncLogger>
<!-- <AsyncLogger name="org.hibernate" level="INFO" type="trace">
<appender-ref ref="application-log" />
</AsyncLogger> -->
<AsyncLogger name="org.hibernate.SQL" level="INFO" additivity="false">
<appender-ref ref="application-log" />
<appender-ref ref="STDOUT" />
</AsyncLogger>
<AsyncLogger name="org.hibernate.type" level="INFO" additivity="false">
<appender-ref ref="application-log" />
<appender-ref ref="STDOUT" />
</AsyncLogger>
<!-- Reduce startup noise -->
<AsyncLogger name="org.springframework.beans.factory" level="WARN">
<appender-ref ref="mule_Logger" />
<appender-ref ref="STDOUT" />
</AsyncLogger>
<AsyncLogger name="com.mulesoft" level="INFO">
<appender-ref ref="mule_Logger" />
</AsyncLogger>
</Loggers>
</Configuration>
Without further information, I would say that the application is doing a log of logging, or using a pattern that requires a lot of processing, or a combination of both.

Max log file size in log4j 1.2.17 with properties

I'm configuring the loggers on our servers to zip logs every day. I also need to make sure that individual log files don't get too large (say 10MB per file).
Neither has worked yet, but first I'd like to tackle the problem of file size.
Here is my log4j.properties file:
log4j.rootLogger=DEBUG, file
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.rollingPolicy.fileNamePattern=./log/logging.%d.log.zip
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=1000000
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
We use log4j 1.2.17, and now I added log4j extras of the same version.
When I test the logging, it creates arbitrary large files. I tried maximumFileSize and MaxFileSize instead of maxFileSize, "10MB" instead of a number, but nothing worked.
I also tried setting the property programmatically, although it would make things difficult, as we have lots of classes that use the logger.
But this also didn't work. It turns out, there are no Appenders in my Logger instance (?). Here is my code:
logger = Logger.getLogger(LogTest.class);
// RollingFileAppender app = (RollingFileAppender) logger.getAppender("file");
// app.setMaxFileSize("1000000");
Enumeration allAppenders = logger.getAllAppenders();
System.out.println(allAppenders);
for (Enumeration<Object> iter = allAppenders; iter.hasMoreElements();) {
Object elem = iter.nextElement();
System.out.println(elem);
}
The output is a null Enumeration:
org.apache.log4j.helpers.NullEnumeration#1161fded
I did not find another post that was useful for my problem. Any ideas?
#GregT
As you have known, log4j has three major constructs - Appenders, priority (logger) and pattern layout. By default, the root Logger is set when we instantiate the log4j such as Logger log = Logger.getInstance(Somclass.class) -> it adds the addiviity of the rootlogger (means that the priority and level set in the rootlogger is inherited by this child logger by default). Now you can choose to define your own priority and level in the lo4j configuration (either in xml or properties file).
Here is what I have configured for the same. Eliminating the triggering policy and dynamic naming, here is a sample configuration using the .properties file
log4j.threshold = INFO
#Specify the appender and the priority
log4j.rootLogger = DEBUG, Appender1, Appender2
log4j.appender.Appender1 = org.apache.log4j.ConsoleAppender
log4j.appender.Appender2 = org.apache.log4j.RollingFileAppender
log4j.appender.Appender2.File = app/log1.log
log4j.appender.Appender1.layout = org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout = org.apache.log4j.PatternLayout
# Print the date in ISO 8601 format
log4j.appender.Appender1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.Appender2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
Note: Here if you set the threshold to INFO and even if the priority is set to DEBUG level, it just prints out the statements complying upto INFO level in the "log" instance.
Note we could also set different loggings for different modules
log4j.rootLogger=DEBUG
# AdminFileAppender - used to log messages in the admin.log file.
log4j.appender.AdminFileAppender=org.apache.log4j.FileAppender
log4j.appender.AdminFileAppender.File=app/admin.log
log4j.appender.AdminFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.AdminFileAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# ReportFileAppender - used to log messages in the report.log file.
log4j.appender.ReportFileAppender=org.apache.log4j.FileAppender
log4j.appender.ReportFileAppender.File=app/report.log
log4j.appender.ReportFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ReportFileAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
**log4j.logger.com.vaannila.admin=,AdminFileAppender
log4j.logger.com.vaannila.report=,ReportFileAppender**
You could use RollingFileAppender if incase you wanted to roll out new files if you reach the threashold size configured. Here Admin module logs are written to "admin.log" file and report module are written "report.log" file
Hope this helps
EDIT: I fear that Timebased/Sizebased triggering policy works well and only with xml configuration rather than property file configuration. Please check on that. And also you dont have to include apache-log4j-extras dependency. 1.2.17 comes shipped with it. here is an XML based config that I use
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="info" filepattern="apps/logs/${hostName}-info-%i.log"
filename="apps/logs/${hostName}-info.log">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] %X{userName} [%C] %F:%L - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="1" min="1" />
</RollingFile>
<RollingFile name="debug" filepattern="apps/logs/${hostName}-debug-%i.log"
filename="apps/logs/${hostName}-debug.log">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] %X{userName} [%C] %F:%L - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="1" min="1" />
</RollingFile>
<Routing name="RoutingAppender">
<Routes pattern="${ctx:logFileName}">
<Route>
<RollingFile name="${ctx:logFileName}-logfile"
fileName="apps/logs/${hostName}-${ctx:logFileName}.log"
filePattern="apps/logs/${hostName}-${ctx:logFileName}-%i.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS}[%X{logTrackingId}] [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="1" min="1" />
</RollingFile>
</Route>
</Routes>
</Routing>
<RollingFile name="RollingFileMonitor" filepattern="apps/logs/${hostName}-monitor-%i.log"
filename="apps/logs/${hostName}-monitor.log">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] %X{userName} [%C] %F:%L - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
<RollingFile name="RollingFileAPM" filename="apps/logs/${hostName}-apm.log"
filepattern="apps/logs/${hostName}-apm-%i.log">
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] %X{userName} [%C] %F:%L - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy min="1" max="1" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
<Logger name="com.XXXX.app.service" additivity="false">
<AppenderRef ref="RollingFileMonitor" />
</Logger>
<Logger name="com.XXXX.apm" additivity="false">
<AppenderRef ref="RollingFileAPM" />
</Logger>
<Logger name="com.XXXX,config" level="info" additivity="false">
<AppenderRef ref="info" />
</Logger>
<Logger name="com.XXXXXX.controller" level="info" additivity="false">
<AppenderRef ref="RoutingAppender" />
</Logger>
<Logger name="com.XXXXX.impl" level="info" additivity="false">
<AppenderRef ref="RoutingAppender" />
</Logger>
<Logger name="com.XXXXX.impl" level="info" additivity="false">
<AppenderRef ref="RoutingAppender" />
</Logger>
<Logger name="com.XXXX.logging" level="info" additivity="false">
<AppenderRef ref="RoutingAppender" />
</Logger>
<Logger name="com.zaxxer.hikari" level="debug" additivity="false">
<AppenderRef ref="debug" />
</Logger>
</Loggers>
Hope this answers your question.

UTF-8 is not working Even after charater set is configured in slf4j logback.xml

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<encoder>
<charset>UTF-8</charset>
<pattern>%X{akkaTimestamp} %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/akka.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>log/tests.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>250</maxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>20MB</maxFileSize>
</triggeringPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="akka" level="INFO" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
Arabic characters are not coming in log file. Characters are coming as
**************************************************message: {"text":"???? ??? ?? ???? ?? ????? ?????? ??."}***********************************
It is working after adding
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<outputPatternAsHeader>true</outputPatternAsHeader>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n </pattern>
</encoder>
Full File
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<outputPatternAsHeader>true</outputPatternAsHeader>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/akka.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>log/tests.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>250</maxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>20MB</maxFileSize>
</triggeringPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<outputPatternAsHeader>true</outputPatternAsHeader>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{35} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="akka" level="INFO" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>

RootLogger to output different levels to different files

I want to have log4j output INFO level to info.log and DEBUG level to debug.log, so that with this piece of code:
logger.info("This is an info message");
logger.debug("This is a debug message");
only the first would go to info.log, while both would go to debug.log.
This doesn't work:
log4j.rootLogger=INFO, R, T
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%r %-5p %c %x - %m%n
log4j.appender.R=org.apache.log4j.FileAppender
log4j.appender.R.File=info.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.R.Threshold = INFO
log4j.appender.T=org.apache.log4j.FileAppender
log4j.appender.T.File=debug.log
log4j.appender.T.layout=org.apache.log4j.PatternLayout
log4j.appender.T.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.T.Threshold = DEBUG
Neither does this:
log4j.rootLogger=INFO, R
log4j.rootLogger=DEBUG, T
log4j.appender.R=org.apache.log4j.FileAppender
log4j.appender.R.File=info.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.R.Threshold = INFO
log4j.appender.T=org.apache.log4j.FileAppender
log4j.appender.T.File=debug.log
log4j.appender.T.layout=org.apache.log4j.PatternLayout
log4j.appender.T.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.T.Threshold = DEBUG
You can use a filter, org.apache.log4j.varia.LevelMatchFilter. e.g.:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN"
"log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- INFO appender -->
<appender name="INFOFILE" class="org.apache.log4j.FileAppender">
<param name="file" value="info.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern"
value="%d{yyyyMMdd-HHmmss.SSS} %-5p (%c.java:%L).%M - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="levelToMatch" value="INFO" />
</filter>
</appender>
<!-- DEBUG appender -->
<appender name="DEBUGFILE" class="org.apache.log4j.FileAppender">
<param name="file" value="debug.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern"
value="%d{yyyyMMdd-HHmmss.SSS} %-5p (%c.java:%L).%M - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="levelToMatch" value="DEBUG" />
</filter>
</appender>
<!-- root -->
<root>
<priority value="ALL" />
<appender-ref ref="INFOFILE" />
<appender-ref ref="DEBUGFILE" />
</root>
</log4j:configuration>
Put all this in one file in the default package: log4j.xml.

tomcat is logging all the tomcat related Debug logs

I have changed my tomcat logging to use logback.xml instead of util.Logger. I have changed using mentioned steps on https://github.com/grgrzybek/tomcat-slf4j-logback.
But now on starting tomcat it is logging every Debug log of tomcat.
My logback.xml looks like this:
<configuration>
<appender name="CONSOLE" class="org.apache.juli.logging.ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{HH:mm:ss.SSS} %-5level {%thread} [%logger{20}] : %msg%n
</pattern>
</encoder>
</appender>
<appender name="FILE-CATALINA" class="org.apache.juli.logging.ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/catalina.log</file>
<append>true</append>
<encoder>
<charset>utf-8</charset>
<pattern>
%d{HH:mm:ss.SSS} %-5level {%thread} [%logger{40}] : %msg%n
</pattern>
</encoder>
<rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${catalina.base}/logs/catalina-%d{yyyyMMdd}-%i.log.zip
</fileNamePattern>
<maxHistory>
60
<!-- days -->
</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>20MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<appender name="FILE-LOCALHOST" class="org.apache.juli.logging.ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/localhost.log</file>
<append>true</append>
<encoder>
<charset>utf-8</charset>
<pattern>
%d{HH:mm:ss.SSS} %logger{0} {%thread} %level : %msg%n
</pattern>
</encoder>
<rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${catalina.base}/logs/localhost-%d{yyyyMMdd}-%i.log.zip
</fileNamePattern>
<maxHistory>
60
<!-- days -->
</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>20MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<appender name="FILE-MANAGER" class="org.apache.juli.logging.ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/manager.log</file>
<append>true</append>
<encoder>
<charset>utf-8</charset>
<pattern>
%d{HH:mm:ss.SSS} %logger{0} {%thread} %level : %msg%n
</pattern>
</encoder>
<rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${catalina.base}/logs/manager-%d{yyyyMMdd}-%i.log.zip
</fileNamePattern>
<maxHistory>
60
<!-- days -->
</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>20MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<appender name="FILE-HOST-MANAGER" class="org.apache.juli.logging.ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/host-manager.log</file>
<append>true</append>
<encoder>
<charset>utf-8</charset>
<pattern>
%d{HH:mm:ss.SSS} %logger{0} {%thread} %level : %msg%n
</pattern>
</encoder>
<rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${catalina.base}/logs/host-manager-%d{yyyyMMdd}-%i.log.zip
</fileNamePattern>
<maxHistory>
60
<!-- days -->
</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>20MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="org.apache.catalina" level="INFO" additivity="false">
<appender-ref ref="FILE-CATALINA"/>
</logger>
<logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]" level="INFO" additivity="false">
<appender-ref ref="FILE-LOCALHOST"/>
</logger>
<logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]" level="INFO" additivity="false">
<appender-ref ref="FILE-MANAGER"/>
</logger>
<logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]" level="INFO" additivity="false">
<appender-ref ref="FILE-HOST-MANAGER"/>
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
Logs are like this
07:16:09.218 [main] DEBUG o.a.tomcat.util.digester.Digester - Fire end() for SetNextRule[methodName=addLifecycleListener, paramType=org.apache.catalina.LifecycleListener]
07:16:09.218 [main] DEBUG o.a.tomcat.util.digester.Digester - [SetNextRule]{Server/Listener} Call org.apache.catalina.core.StandardServer.addLifecycleListener(org.apache.catalina.core.AprLifecycleListener#277b8705)
This is line coming when I am starting tomcat :
Using CATALINA_BASE: /mnt/production/apache-tomcat
Using CATALINA_HOME: /mnt/production/apache-tomcat
Using CATALINA_TMPDIR: /mnt/production/apache-tomcat/temp
Using JRE_HOME: /opt/bitnami/java
Using CLASSPATH: /mnt/production/apache-tomcat/bin/bootstrap.jar:/mnt/production/apache-tomcat/bin/tomcat-juli.jar
So classpath looks correct to me.

Resources