Reputation: 158051
I wanted to add some simple methods to the Log4j logger for use in my application.
Tried doing so like this:
public class Logger extends org.apache.log4j.Logger
{
public Logger(Class type)
{
super(type.getName());
}
public void info(String format, Object... o)
{
info(String.format(format, o));
}
public void debug(String format, Object... o)
{
debug(String.format(format, o));
}
public void warn(String format, Object... o)
{
warn(String.format(format, o));
}
public void error(String format, Object... o)
{
error(String.format(format, o));
}
}
Figured I should be able to use this like so:
public class TmpTest
{
public static void main(String[] args) throws Exception
{
// Note I'm not even using the special methods here
new Logger(TmpTest.class).warn("Test");
}
}
But I get a NullPointerException
.
Exception in thread "main" java.lang.NullPointerException
at org.apache.log4j.Category.warn(Category.java:1004)
at no.nwn.productconfigurator.TmpTest.main(TmpTest.java:16)
Can I not do it like this? Is there some missing initialization that is done only if I use the getLogger
factory methods of the Logger
class?
If I can't extend the Logger class, how should I do it?
Could it be something about the log4j.xml? Never used this myself before and haven't written it myself.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration debug="true">
<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>
<appender name="ProductConfigurator" class="org.apache.log4j.DailyRollingFileAppender">
<!--
<param name="File" value="logs/ProductConfigurator.log" />
-->
<param name="File" value="/var/tomcat/logs/ProductConfigurator.log" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss,SSS}. %5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<logger name="org.hibernate" additivity="false">
<level value="warn" />
<appender-ref ref="ProductConfigurator"/>
</logger>
<logger name="org.hibernate.type" additivity="false">
<level value="warn" />
<appender-ref ref="ProductConfigurator"/>
</logger>
<logger name="org.hibernate.SQL" additivity="false">
<level value="warn" />
<appender-ref ref="ProductConfigurator"/>
</logger>
<root>
<!-- all|trace|debug|info|warn|error|fatal|no -->
<priority value="info" />
<appender-ref ref="ProductConfigurator"/>
</root>
</log4j:configuration>
Upvotes: 2
Views: 6169
Reputation: 29520
The category.repository
field is null
. Look at the source code :
public void warn(Object message) {
if(repository.isDisabled( Level.WARN_INT))
return;
if(Level.WARN.isGreaterOrEqual(this.getEffectiveLevel()))
forcedLog(FQCN, Level.WARN, message, null);
}
To properly initialize a Logger
, you should use the LogManager
:
public static void main(String[] args) throws Exception
{
LogManager.getLogger(TmpTest.class.getName(), new LoggerFactory() {
@Override
public Logger makeNewLoggerInstance(String name) {
return new Logger(name);
}
}).warn("Test");
}
I use a custom factory implementation to instantiate your logger. Not that the classic method to create a logger (Logger.getLogger(Class clazz)
) use the LogManager
:
static public Logger getLogger(Class clazz) {
return LogManager.getLogger(clazz.getName());
}
If there isn't specific constraint in your project, i recommend you to use slf4j. It provides the methods that you are expecting :
logger.debug("Temperature set to {}. Old temperature was {}.", t, oldT);
Upvotes: 3
Reputation: 6149
Just add super.
in front of all your calls to info, warn, etc.
in the following code :
public void info(String format, Object... o) {
info(String.format(format, o));
}
When you call info(String.format(format, o))
, you call the very same method in a recursive way : the first param is the formatted string, and the second (var-args) param is null, so when String.format
is called again, you get a NullPointerException.
Therefore you should write this instead :
public void info(String format, Object... o) {
super.info(String.format(format, o));
}
Upvotes: -1