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 Al...@everis.com on 2007/08/27 09:59:31 UTC
log4j doesn't recognize my appenders
Hi, I'be started usin log4j in one of my apps. In order to use it i've
made a jar file that contains the log4j itself, a Java Stub Class
(Log.java) and a log4j-config,xml inside it.
The problem is that, once i load the config file via domConfigurator and i
instantiate a log object, appears a warning indicating that the logger
doesn't have any appender configured..
The configuration part seems to work because the log file defined in the
xml is created, but it's empty.
The config file looks as this
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "dtds/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="File" class="org.apache.log4j.FileAppender">
<param name="File" value="A1.log" />
<param name="Append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%t %-5p
%c{2} - %m%n"/>
</layout>
</appender>
<appender name="Console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</layout>
</appender>
<root>
<priority value ="debug" />
<appender-ref ref="Console" />
<appender-ref ref="File" />
</root>
</log4j:configuration>
Using this, the code in wich i create log4j is:
private Logger logger;
public Log(String userName) {
if (log==null){
try {
String fichero = "/log4j-config.xml";
DOMConfigurator domLog = new DOMConfigurator();
Hierarchy hierarchy = new Hierarchy(new
RootCategory(Level.DEBUG));
InputStream is = Log.class.getResourceAsStream(fichero); //we
open the XML as an inputStream because it's inside the jar, not the
filesystem
domLog.doConfigure(is,hierarchy);
} catch(Exception e) {
e.printStackTrace();
}
logger = (Logger) Logger.getInstance(this.getClass().getName());
}
this.usuario=userName;
}
I think the problem is related with the way i get the Logger instance, but
i'm not sure
Re: log4j doesn't recognize my appenders
Posted by Jacob Kjome <ho...@visi.com>.
How are you using the Log() class? if you are saying using it the way I think
you are using it, you are probably triggering configuration for every instance
you get of Log. First, this is a bad pattern. I recommend either using
Logger directly or using a wrapper such as commons-logging or SLF4J if you are
not keen on declaring dependencies to Log4j everywhere in your code. Second,
separate your configuration code from your Logger code. In fact, you could
just name your config file log4j.xml, put it in the classpath, and let it get
picked up automagically by Log4j. You really don't have to write a single
line of configuration code unless you are trying to do something extra
special.
Oh, and every time you reconfigure, the file pointed at by your file appender
will get wiped clean because you have "Append" set to "false". Could explain
your empty log file.
Correct those issues and let us know if you still have problems.
Jake
On Mon, 27 Aug 2007 09:59:31 +0200
Alberto.Sanchez.Vera.Saiz@everis.com wrote:
> Hi, I'be started usin log4j in one of my apps. In order to use it i've
> made a jar file that contains the log4j itself, a Java Stub Class
> (Log.java) and a log4j-config,xml inside it.
> The problem is that, once i load the config file via domConfigurator and i
> instantiate a log object, appears a warning indicating that the logger
> doesn't have any appender configured..
>
> The configuration part seems to work because the log file defined in the
> xml is created, but it's empty.
>
> The config file looks as this
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE log4j:configuration SYSTEM "dtds/log4j.dtd">
> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
>
> <appender name="File" class="org.apache.log4j.FileAppender">
> <param name="File" value="A1.log" />
> <param name="Append" value="false" />
> <layout class="org.apache.log4j.PatternLayout">
> <param name="ConversionPattern" value="%t %-5p
> %c{2} - %m%n"/>
> </layout>
> </appender>
>
> <appender name="Console" class="org.apache.log4j.ConsoleAppender">
> <layout class="org.apache.log4j.PatternLayout">
> <param name="ConversionPattern"
> value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
> </layout>
> </appender>
>
> <root>
> <priority value ="debug" />
> <appender-ref ref="Console" />
> <appender-ref ref="File" />
> </root>
>
> </log4j:configuration>
>
> Using this, the code in wich i create log4j is:
>
> private Logger logger;
>
> public Log(String userName) {
> if (log==null){
> try {
> String fichero = "/log4j-config.xml";
> DOMConfigurator domLog = new DOMConfigurator();
> Hierarchy hierarchy = new Hierarchy(new
> RootCategory(Level.DEBUG));
> InputStream is = Log.class.getResourceAsStream(fichero); //we
> open the XML as an inputStream because it's inside the jar, not the
> filesystem
>
> domLog.doConfigure(is,hierarchy);
>
> } catch(Exception e) {
> e.printStackTrace();
>
> }
> logger = (Logger) Logger.getInstance(this.getClass().getName());
> }
> this.usuario=userName;
> }
>
> I think the problem is related with the way i get the Logger instance, but
> i'm not sure
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org