logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shiby Maria John" <Shi...@ibsplc.com>
Subject RE: [BULK] how to print line number
Date Tue, 16 May 2006 03:30:14 GMT

Thanks to all ..
I got it working ;o)


                                                                                         
                 
                      "Philip Denno"                                                     
                 
                      <pdenno@tsi.bc.ca        To:       "Log4J Users List"           
                    
                      >                         <log4j-user@logging.apache.org> 
                          
                                               cc:                                       
                 
                      05/15/2006 08:57         Subject:  RE: [BULK]  how to print line number
             
                      PM                                                                 
                 
                      Please respond to                                                  
                 
                      "Log4J Users                                                       
                 
                      List"                                                              
                 
                                                                                         
                 
                                                                                         
                 




The javadoc I referred to is at:

http://logging.apache.org/log4j/docs/api/index.html

Look at the API for the Logger class.

Jakes e-mail also pointed you to an archive discussion on this topic.

Basically you need to change the logger calls inside your wrapper to:

             Logger.log(this.getClass(), message, level, throwable);

Which will do the backtrace to find the calling class and line number.

Cheers,
Philip.

-----Original Message-----
From: Shiby Maria John [mailto:ShibyM@ibsplc.com]
Sent: Sunday, May 14, 2006 8:29 PM
To: Log4J Users List
Subject: RE: [BULK] how to print line number
Importance: Low


Thanks for the response.

Can you be more specific??
What are the "generic log" methods that you referring to ???

I am aware that it is slower than the simple log. But I need to do
that
in some conditions.

Please help me with the details.

Regards,
Shiby




                      "Philip Denno"

                      <pdenno@tsi.bc.ca        To:       "Log4J Users
List"
                      >
<log4j-user@logging.apache.org>
                                               cc:

                      05/12/2006 08:57         Subject:  RE: [BULK]
how
to print line number
                      PM

                      Please respond to

                      "Log4J Users

                      List"









The approach you are taking is called using a "wrapper class".

You need to use the generic "log" methods on Logger. See the Javadoc.

Basically, what these methods do is do a backtrace on the call - stack
to find the method which called your wrapper method.

One thing to keep in mind is that these methods are slower than the
equivalent level methods and may affect performance if doing
signifigant
amounts of logging.

Cheers,
Philip.

-----Original Message-----
From: Shiby Maria John [mailto:ShibyM@ibsplc.com]
Sent: Thursday, May 11, 2006 11:54 PM
To: log4j-user@logging.apache.org
Subject: [BULK] how to print line number
Importance: Low

Hi,

I have a framework that implements the logging by log4j.
Now my classes need to log using this and I need to print the line
number from where the log originally originated from.

I will explain what I want with an example.
I have three classes which i am enclosing here.

TestLogging.java (Test class for Logger)
1 : import framework.logging.Logger;
2 : import framework.logging.Log;
3 : public class TestIflyLogging {
4 :   private static Logger log = Log.getLogger("TestLogging");
5 :   public static void main(String[] args) {
6 :         log.debug("Start of main");
7 :         log.info("Information");
8 :         ...
9 :   }
10 : }

Log.java
1 : package framework.logging;
2 : public class Log {
3 :   public static Logger getLogger(String className) {
4 :         return new Logger(className);
5 :   }
6 : }

Logger.java
1 : public class Logger {
2 :   org.apache.log4j.Logger cat;
3 :   Logger(String classname) {
4 :         if (classname == null) {
5 :               cat = org.apache.log4j.Logger.getRootLogger();
6 :         } else {
7 :               cat = org.apache.log4j.Logger.getLogger(classname);
8 :         }
9 :   }
10 :
11 :  public void debug(String message) {
12 :        cat.debug(message);
13 :  }
14 :
15 :  public void warn(String message) {
16 :        cat.warn(message);
17 :  }
18 :
19 :  public void error(String message) {
20 :        cat.error(message);
21 :  }
22 :
23 :  public void fatal(String message) {
24 :        cat.fatal(message);
25 :  }
26 :
27 :  public void info(String message) {
28 :        cat.info(message);
29 :  }
30 : }

This is my log4j.properties file.
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%p: %l - %m%n

This is the output that I get when I run TestLogging.java :
DEBUG: framework.logging.Logger.debug(Logger.java:12) - Start of main
INFO: framework.logging.Logger.info(Logger.java:28) - Information

What I want is this output :
DEBUG: TestLogging.main(TestLogging.java:6) - Start of main
INFO: TestLogging.main(TestLogging.java:7) - Information

Is there any way to acheive this??

Thanks in advance,
Shiby





---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org









---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org









---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message