My log happens in getParam()
method. but it is logged as main
method. And I have three objects, but I do not see any information as to which object is logging. I want that information too.
Here is the output:
INFO [main]: info
INFO [main]: debug
INFO [main]: trace
here is the code:
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
class Log4jTest {
private static Logger log=Logger.getLogger(Log4jTest.class);
private String param;
public Log4jTest(String param){
this.param=param;
}
public String getParam(){
log.info(param);
return param;
}
}
public class Log4j_testing {
public static void main(String[] args) throws FileNotFoundException{
Log4jTest l1= new Log4jTest("info");
l1.getParam();
Log4jTest l2= new Log4jTest("debug");
l2.getParam();
Log4jTest l3= new Log4jTest("trace");
l3.getParam();
}
}
my log4j.properties:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.stdout.layout.ConversionPattern=%-5p [%t]: %m%n
On a different note: if I have the above commented line uncommented and the last line commented out, I get
11:19:41,586 INFO Log4jTest:19 - info
11:19:41,589 INFO Log4jTest:19 - debug
11:19:41,589 INFO Log4jTest:19 - trace
It gives the correct line number(19) that is being logged, but not the correct method.
Add %M
to your pattern. However, as it is stated in the JavaDoc: "Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue. "