• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 46135
  • Last Modified:

Log4J : How to start??

Hi all, i wonder the Log4J is a commercial product or a open source. Not much documentation for those whowan to adopt this technology.
The short mannual doesnt seem help much on how to help people like me to step out the first step.

Anyone of here mind to share your experience with me?
0
penang67
Asked:
penang67
  • 5
  • 3
  • 2
  • +4
1 Solution
 
objectsCommented:
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
penang67Author Commented:
None of the link above tell me how to do a step by step configuration. Note Even a sample programs.

What i awnt to know how is the setting after i have put the .jar into my classpath. What should i do next?
0
 
objectsCommented:
Settings for what exactly?
What don't the articles I posted above cover specifically?
0
 
CodingExpertsCommented:
Hi penang67

I think this link should suffice..

http://logging.apache.org/log4j/docs/manual.html
and for the FAQ's please see this link ...
http://logging.apache.org/log4j/docs/faq.html#1.5

Hope this helps!!! :-)
Coding Experts
0
 
CodingExpertsCommented:
Go thru this tutorial too this gives a step by step procedure ...

http://www.developer.com/open/article.php/10930_3097221_1

Coding Experts
0
 
sompol_kiatkamolchaiCommented:
:)
0
 
CajunBillCommented:
Hi Penang,
To use log4j, you need to put some statements in your application code.  It is a tool for programmers.

At the start of a java module you would put a few statements to import the log4j classes and define the logger.
In the body of the module you would put a logger call at each point where you want a message to come out.
With a little more work you can change the configuration of log4j on the fly, in other words without restarting your system.

Here is a sample:
===========
import com.foo.Bar;

 // Import log4j classes.
 import org.apache.log4j.Logger;
 import org.apache.log4j.BasicConfigurator;

 public class MyApp {

   // Define a Logger instance named "MyApp".
   static Logger logger = Logger.getLogger("MyApp");

   public static void main(String[] args) {

     // Set up a simple configuration that logs on the console.
     BasicConfigurator.configure();
     logger.info("Entering application.");

... Do some application stuff here ...

     logger.info("Exiting application.");
   }
 }
0
 
aasiaCommented:
Penang,

The Log4j logging framework is simple and easy to use. Here is a brief and elementary primer on Log4j. There are three simple steps

1. Set up your Log4j configuration
2. Load the Log4j configuration in your start up class
3. Insert Log statements were required in your application classes

1. Setting up Log4j configuration

The main entity of Log4j configuration is the type of logging. Log4j supports logging onto screen, file, rolling file, and remotely onto a central location. Log4j configuration can be specified using an XML file or a simple Java properties file. Let me illustrate an example of a Rolling file configuration using a properties file.

# Log4j configuration file.

log4j.rootCategory=DEBUG, A2

# Available levels are DEBUG, INFO, WARN, ERROR, FATAL
#
#
# A2 is a DailyRollingFileAppender
#

log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.file=log/log4j.log
log4j.appender.A2.datePattern='.'yyyy-MM-dd
log4j.appender.A2.append=true
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-5p %d{ISO8601} [%t] - %m%n
 
2. Loading the Log4j configuration

Before you can start using Log4j for logging, you must provide Log4j with the configuration file (as described in step 1) so it knows how you want the logging to be done.

In your startup class, import the following class: org.apache.log4j.PropertyConfigurator

Add, the following line in your startup method

    if(logFile != null)
    {
        PropertyConfigurator.configure(logFile);
    }

where logfile is the variable holding the path to the configuration file.

3. Insert Log statements were required in your application classes

You are now ready to use the logger for logging in your application classes. Import the org.apache.log4j.Logger and get an instance of the Logger class

public static final Logger log = Logger.getLogger(com.mycompany.myapplication.myclass.class);

in your application class. The advantage of passing the class is to allow Log4j configurations per class.

Finally, to log an event, based on the priority of the event, call one of the debug, info, warn, error or the fatal methods of the log object. Debug is the lowest priority, while fatal is the highest.

The following strand of code logs a low priority event.

log.debug("Entering com.mycompany.myapplication.myclass.init" );
0
 
penang67Author Commented:
Dear all, i am trying it out. And lot of problems. Can you all help??

I have downloaded the log4j1.2.8.jar file and put it into the classes path, WEB-INF/lib folder.
I also put the log.properties file in the WEB-INF/lib folder.

And i am trying to include the logger in the LoginServlet.java

......
.....
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
......
....
if (action.equals("login"))
{
      Logger logger = Logger.getLogger("LoginServelt");
      logger.info("Testing for Loging");
..
..
...

And my log.properties is below :


# A sample log4j configuration file
log4j.rootLogger=DEBUG, stdout
# Configure the stdout appender to go to the Console
# Configure the stdout appender to use the PatternLayout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%5p] {%d{dd/MM/yy HH:mm:ss}} (%C{3}.%M()-Line %L) - %m%n

# Configure the rolling appender to be a RollingFileAppender
# Configure the name of the logout for the rolling appender
# Set up the maximum size of the rolling log file
log4j.appender.rolling=org.apache.log4j.RollingFileAppender
log4j.appender.rolling.File=C:/foong.log
log4j.appender.rolling.MaxFileSize=1024KB
log4j.appender.rolling.MaxBackupIndex=50
log4j.appender.rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.rolling.layout.ConversionPattern=[%5p] (%d{ABSOLUTE}) (%C{3}.%M()-Line %L) - %m%n

********

I also have created a log file in the C:\ directory with the name of foong.log

But i get this error
[9/23/04 17:14:46:406 SGT] 428acc31 WebGroup      I SRVE0180I: [PowerLink] [/PowerLink] [Servlet.LOG]: /vlidr/login.jsp: init
[9/23/04 17:14:52:250 SGT] 428acc31 SystemErr     R log4j:WARN No appenders could be found for logger (LoginServelt).
[9/23/04 17:14:52:250 SGT] 428acc31 SystemErr     R log4j:WARN Please initialize the log4j system properly.


log4j:WARN No appenders could be found for logger (LoginServelt).
log4j:WARN Please initialize the log4j system properly.



0
 
aasiaCommented:
Penang,

You have missed point number 2 in my mail. Try again after setting up according to point 2.

Aasia
0
 
penang67Author Commented:
is that mean by each servlet or file i have to include these lines??


    if(logFile != null)
    {
        PropertyConfigurator.configure(logFile);
    }

I have >1000 files and servlet. Is that what you mean by startup class?

***
In your startup class, import the following class: org.apache.log4j.PropertyConfigurator
where logfile is the variable holding the path to the configuration file.
***
0
 
penang67Author Commented:
and when i added this into my servlet, i have this error:

logFile cannot be resoveld.??
which startup class you are referring?


********
      if (action.equals("login"))
            {
                  if(logFile != null)
                     {
                           PropertyConfigurator.configure(logFile);
                     }

                  Logger logger = Logger.getLogger("LoginServelt");
                  logger.info("Testing for Loging");
**********
0
 
penang67Author Commented:
anyone cn tell what happend?
0
 
CajunBillCommented:
Sure, the problem is that logFile is a variable. You don't need it right now.  For now, change the code to use logger.error, and coment out the lines for the PropertyConfigurator, like this:

********
     if (action.equals("login"))
          {
//               if(logFile != null)
//                  {
//                       PropertyConfigurator.configure(logFile);
//                  }

               Logger logger = Logger.getLogger("LoginServelt");
               logger.error("Testing for Loging");
**********
0
 
nickvwCommented:
If I recall, Log4j will look for the file log4j.properties in the classpath and use it if no PropertyConfigurator is used.

Thus, all you need is

static Logger logger= Logger.getLogger(...);


and start logging, making changes as necessary to log4j.properties.

logger.debug(..);
etc.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now