Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Log4j2.xml Tweaks

5.00/5 (1 vote)
3 Mar 2016CPOL1 min read 7.6K  
Log4j2.xml Tweaks

This is one I’ve seen recently in some un edited log4j2.xml files:

XML
<RollingFile name="RollingFile"
    fileName="/usr/local/mule-enterprise-standalone-3.6.1/logs/mule-app-myapp.log"
    filePattern="/usr/local/mule-enterprise-standalone-3.6.1/logs/
		$${date:yyyy-MM}/mule-app-myapp-%d{yyyy-MM-dd-HH}-%i.log.gz">

What’s wrong with this? So many things…

What happens if I move this server? What happens if I move the app? Especially if I have two servers on the same machine.

Use instead:

XML
<Properties>
   <Property name="logDir">${sys:mule.home}/logs</Property>
   <Property name="appName">myapp/Property>
</Properties>
<Appenders>
    <RollingFile name="diagnostic" fileName="${logDir}/mule-app-${appName}.log"
 append="true"
 filePattern="${logDir}/$${date:yyyy-MM}/mule-app-${appName}-%d{MM-dd-yyyy}-%i.log.gz">
    <PatternLayout pattern="[%d{MM-dd HH:mm:ss}] %-5p %c{1} [%t]: %m%n" />
    <TimeBasedTriggeringPolicy />
    </RollingFile>
...

Now you have a variable that you edit at the top of every log4j2.xml file that will say the app name. You can then use a basically universal config file for all your apps.

To make your logger setup complete, you also need to add the category field to every logger, the naming convention I use is:

com.mycompany.mule.myappname.flowname
  1. This gives you the ability to set the level of logging specific to your loggers in an app different from any built in Mule loggers
  2. This will log the flow name in the log file that produced a particular log line. In the above logging config, the...
    %c{1}

    ...enables this.

  3. <Logger name=”com.mycompany” level=”INFO”/> is now customizable in the log4j2.xml

One final note about this. The

${sys:mule.home}

has a variable which requires that mule home is set effectively on your server. Sometimes this is inconsistent on mule servers started in an inconsistent way. Use the Mule as a Service to make this enterprise-grade.

Full Example Log File

XML
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
	<Properties>
		<Property name="logDir">${sys:mule.home}/logs</Property>
		<Property name="appName">myapp</Property>
	</Properties>
	<Appenders>
		<RollingFile name="diagnostic" fileName="${logDir}/mule-app-${appName}.log"
			append="true"
			filePattern="${logDir}/$${date:yyyy-MM}/
				mule-app-${appName}-%d{MM-dd-yyyy}-%i.log.gz">
			<PatternLayout pattern="[%d{MM-dd HH:mm:ss}] %-5p %c{1} [%t]: %m%n" />
			<TimeBasedTriggeringPolicy />
		</RollingFile>
		<RollingFile name="monitoring" fileName="${logDir}/mule-app-${appName}-monitoring.log"
			append="true"
			filePattern="${logDir}/$${date:yyyy-MM}/
				mule-app-${appName}-monitoring-%d{MM-dd-yyyy}-%i.log.gz">
			<PatternLayout pattern="[%d{MM-dd HH:mm:ss}] %-5p %c{1} [%t]: %m%n" />
			<TimeBasedTriggeringPolicy />
		</RollingFile>
	</Appenders>

    <Loggers>

        <!-- CXF is used heavily by Mule for web services -->
        <Logger name="org.apache.cxf" level="WARN"/>

        <!-- Apache Commons tend to make a lot of noise which can clutter the log-->
        <Logger name="org.apache" level="WARN"/>

        <!-- Reduce startup noise -->
        <Logger name="org.springframework.beans.factory" level="WARN"/>
        
        <!-- Prevent Default Logger ERROR level messages (which display the payload) -->
        <Logger name="org.mule.exception.CatchMessagingExceptionStrategy" level="FATAL"/>
        <Logger name="org.mule.exception.DefaultMessagingExceptionStrategy" level="FATAL"/>

        <!-- Mule classes -->
        <Logger name="httpclient.wire" level="INFO" />
		<Logger name="org.apache.commons.httpclient" level="INFO" />

        <Logger name="org.mule" level="INFO"/>
        <Logger name="com.mulesoft" level="INFO"/>

        <!-- Reduce DM verbosity -->
        <Logger name="org.jetel" level="WARN"/>
        <Logger name="Tracking" level="WARN"/>

        <Logger name="org.glassfish.grizzly" level="INFO"/>
        <Logger name="org.asynchttpclient" level="INFO"/>

	<Logger name="org.mule.util.queue.DualRandomAccessFileQueueStoreDelegate"
		level="INFO" />
	<Logger name="org.mule.transformer" level="WARN" />
	<Logger name="org.jetel.graph" level="WARN" />
	<Logger	name="org.mule.transport.service.DefaultTransportServiceDescriptor"
		level="WARN" />
	<Logger name="com.sforce" level="INFO" />
	<Logger name="org.mule.module.launcher.DeploymentDirectoryWatcher"
		level="WARN" />
	<Logger name="org.mule.transformer.graph" level="WARN" />
	<Logger name="com.mulesoft.module.batch" level="INFO" />
	<Logger name="org.mule.transport.jms.JmsMessageUtils"
		level="ERROR" />
	<Logger name="org.mule.lifecycle.AbstractLifecycleManager"
		level="WARN" />
			
        <Root level="INFO">
            <AppenderRef ref="diagnostic"/>
        </Root>
    </Loggers>

</Configuration>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)