Straight to the Log4j (1)



The current version could be downloaded from


If you are using Maven for your project









Log4j looks for a file named log4j.xml and then for a file named 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 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.




All levels including custom levels


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


developing only, for debugging purpose


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.


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.


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


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


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


Logs to console


Logs to a file


Logs by email


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


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



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.


Date in Format Absolute


%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


If we rewrite the example above in log4j.xml

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


    <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"/>




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

           <appender-ref ref="stdout"/>




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"/>




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


For log4j.xml will be

    <logger name="">

       <level value="DEBUG" />



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 with DEBUG and with ERROR.

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 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[cgieigdfhhedja]

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s