Straight to the Log4j (1)

Installation

 

The current version could be downloaded from http://logging.apache.org/log4j/

 

If you are using Maven for your project

           <dependency>

                  <groupId>log4j</groupId>

                  <artifactId>log4j</artifactId>

                  <version>1.2.14</version>

           </dependency>

 

Configuration

 

Log4j looks for a file named log4j.xml and then for a file named log4j.properties. Both must be placed in the src folder. (in the resource folder for maven structure) You may also use both of them at the same time, but the property one will overwrite the xml one, if there is an overlap. Let’s start with log4j.property example

 

log4j.rootLogger = ERROR, stdout

 

Stdout is the associated appendor – i.e. it instructs all logging sent to the rootlogger how to log/what logging method to use e.g. print to the console/write to a file etc. The ERROR bit tells the logger what level of logging it should capuring to

 

The following Levels are available. But you can define custom levels as well. Examples are provided with the log4j download.

Level

Description

All

All levels including custom levels

Trace

developing only, can be used to follow the program execution.

Debug

developing only, for debugging purpose

Info

Production optionally, Course grained (rarely written informations), I use it to print that a configuration is initialized, a long running import job is starting and ending.

Warn

Production, simple application error or unexpected behaviour. Application can continue. I warn for example in case of bad login attemps, unexpected data during import jobs.

Error

Production, application error/exception but application can continue. Part of the application is probably not working.

Fatal

Production, fatal application error, application cannot continue, for example database is down.

No

Do not log at all.

 

 

log4j.appender.stdout = org.apache.log4j.ConsoleAppender

 

Tells the log4j which appender should be applied for stdout. Other common appenders include

ConsoleAppender

Logs to console

FileAppender

Logs to a file

SMTPAppender

Logs by email

RollingFileAppender

Logs to a file, starts a new file once the max size is reached. (An alternative is the DailyRollingFileAppender which creates on file per day)

More details of the available appenders could be found at http://logging.apache.org/log4j/docs/api/org/apache/log4j/AppenderSkeleton.html

 

log4j.appender.stdout.Threshold = ERROR

Level applied to stdout

 

log4j.appender.stdout.Target   = System.out

If you would like to log to a file you can use the following code instead

log4j.appender.file=org.apache.log4j.FileAppender

log4j.appender.file.File=struts.log

This assume that you create new reference to file instead of studout

 

log4j.appender.stdout.layout = org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern = %d{ISO8601} %-5p [%F:%L] : %m%n

 

The layout of the log message could be formatted as above and the explanation is listed below.

%d{ABSOLUTE}

Date in Format Absolute

%5p

%5 defines a right justified print with 5 characters, p prints the priority of the log message

 %c{1}:%L – %m%n

And the other settings. Very simple. They are all explained in the API.

 

 

More details about layout could be found at http://logging.apache.org/log4j/docs/api/org/apache/log4j/Layout.html

 

If we rewrite the example above in log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >

<log4j:configuration>

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

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

                  <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>

                   </layout>

    </appender>

    <root>

           <priority value="ERROR"></priority>

           <appender-ref ref="stdout"/>

    </root>

</log4j:configuration>

 

The log4j.xml file for FileAppender will be something like

    <appender name="FILE" class="org.apache.log4j.FileAppender">

       <param name="file" value="struts.log"/>

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

           <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>

       </layout>

   </appender>

 

If you want to apply different log level with particular package or classes, you could insert something like

log4j.logger.hongliang.com.firstHibernate = DEBUG

 

For log4j.xml will be

    <logger name="hongliang.com.firstHibernate">

       <level value="DEBUG" />

   </logger>

 

Not like the Appender name, you have to make sure the logger name is match to the existing package or class that you want to log.

 

Another thing you should be aware of is the log level. It is wise that you start your root with higher level (such as ERROR) and specifies particular logger with lower level (such as DEBUG). Otherwise, while you run the log4j you may get too much detailed information with your log message. (DEBUG log could get you massive information). Besides, you need to make sure that the more detailed in the project hierarchy should apply with lower log level for propagating purpose, ie hongliang.com.firstHibernate with DEBUG and hongliang.com with ERROR.

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

9 Responses to Straight to the Log4j (1)

  1. Unknown says:

    Hi,Do you need digital signages, advertising displays, digital sign, advertisement displays and advertising players? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.
    amberdigital Contact Us
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[cgieigdfhhedja]

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