Quick log4j set up

<!-- pom.xml --> 


		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.7</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.7</version>
		</dependency>


<!-- src/main/resources/log4j.xml --> 
  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

	<appender name="my-file-appender" class="org.apache.log4j.FileAppender">
		<param name="file" value="some.file" />
		<param name="append" value="true" />
		<param name="encoding" value="UTF-8" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %t %-5p %c - %m%n" />
		</layout>
	</appender>

	<appender name="console" class="org.apache.log4j.ConsoleAppender">
	    <layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" value="%d %t %-5p %c - %m%n" />
	    </layout>
	</appender>

	<logger name="com.my.package">
		<level value="info" />  
		<appender-ref ref="my-file-appender" />
	</logger>	

	<root>
		<level value="warn" />
		<appender-ref ref="console" />
	</root>

</log4j:configuration>

If you don’t get expected result, add "-Dlog4j.debug" to your VM options and see the diagnotisc information

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.