I am using Custom Logger for my project eg. class MyLogger.
With that i am able to log the message correctly but Its not logging
the Class & Method name correctly from where log initiated.
So I think may we need to specify MyLoggger in log4j.xml but I don't know
where & how ?
All suggestions are appreciated.
Thanks.
If you have a class MyClass:
package com.acando.norolnes;
public class MyClass {
public String foo;
public int bar;
}
That you want to custom format when logging, you can create a custom ObjectRenderer:
package com.acando.norolnes;
import org.apache.log4j.or.ObjectRenderer;
public class MyClassRenderer implements ObjectRenderer {
@Override
public String doRender(Object arg0) {
if(arg0 instanceof MyClass) {
MyClass m = (MyClass)arg0;
return "("+m.foo+" "+m.bar+")";
}
return arg0.toString();
}
}
And link this up in the config like this, with a -element:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<renderer renderedClass="com.acando.norolnes.MyClass"
renderingClass="com.acando.norolnes.MyClassRenderer"/>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<root>
<priority value="debug"></priority>
<appender-ref ref="stdout"/>
</root>
</log4j:configuration>
For a more complete example, take a look at http://www.java2s.com/Code/Java/Language-Basics/log4jAcompleteexample.htm