Solved

Log4J : How to start??

Posted on 2004-09-09
17
46,056 Views
Last Modified: 2011-08-18
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
Comment
Question by:penang67
  • 5
  • 3
  • 2
  • +4
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12023982
0
 
LVL 92

Expert Comment

by:objects
ID: 12023985
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12024235
0
 

Author Comment

by:penang67
ID: 12025464
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
 
LVL 92

Expert Comment

by:objects
ID: 12031942
Settings for what exactly?
What don't the articles I posted above cover specifically?
0
 
LVL 6

Expert Comment

by:CodingExperts
ID: 12037024
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
 
LVL 6

Expert Comment

by:CodingExperts
ID: 12037033
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
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12037861
:)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Expert Comment

by:CajunBill
ID: 12040566
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
 

Expert Comment

by:aasia
ID: 12073512
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
 

Author Comment

by:penang67
ID: 12131424
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
 

Expert Comment

by:aasia
ID: 12131559
Penang,

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

Aasia
0
 

Author Comment

by:penang67
ID: 12131661
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
 

Author Comment

by:penang67
ID: 12131677
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
 

Author Comment

by:penang67
ID: 12154032
anyone cn tell what happend?
0
 
LVL 7

Accepted Solution

by:
CajunBill earned 500 total points
ID: 12154923
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
 

Expert Comment

by:nickvw
ID: 12521013
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
handling Struts tag, JSTL tag and scriptlet in Ajax. 7 169
java cyclic graphs 15 194
EJB MDB example 4 76
Java Script nested call 3 53
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A Short Story about the Best File Recovery Software – Acronis True Image 2017
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now