You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Philip Denno <pd...@tsi.bc.ca> on 2006/05/12 17:27:07 UTC

RE: [BULK] how to print line number

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


RE: [BULK] how to print line number

Posted by Jacob Kjome <ho...@visi.com>.
Shiby,

please see the following thread.  It provides exactly the info you need.

http://marc.theaimsgroup.com/?t=114505791700001&r=1&w=2


Jake

Quoting Philip Denno <pd...@tsi.bc.ca>:

> 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