Straight to the Log4j (2)

The following part will help you to find out how to write your logging info into different file.

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

 

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;

<appender name="console_appender" class="org.apache.log4j.ConsoleAppender">

    <param name="Threshold" value="INFO"/>

    <layout class="org.apache.log4j.PatternLayout">

            <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) – %m%n"/>

    </layout>

</appender>

 

<appender name="login_appender" class="org.apache.log4j.DailyRollingFileAppender">

    <param name="File" value="${catalina.base}/logs/myapplication_login.log"/>

    <param name="Threshold" value="DEBUG"/>

    <layout class="org.apache.log4j.PatternLayout">

            <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) – %m%n"/>

    </layout>

 </appender>

 

<logger name="java.sql.PreparedStatement" >

            <appender-ref ref="console_appender"/>

</logger>

 

<category name="myapplication.login">

            <appender-ref ref="login_appender"/>

</category>

</log4j:configuration>

 

The yellow color just highlight the path to the log file which will be used for this appender. The appender login_appender will be called by a category referrence myapplication.login. You will use this reference in your java code as shown below.   

 

private static final Logger LOGGER = Logger.getLogger("myapplication.login");

LOGGER.debug(“This is my debug stament that will be shown on the log file”);

Advertisements
This entry was posted in Java Utilities. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s