logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jacob Kjome" <h...@visi.com>
Subject Re: xml config
Date Thu, 24 Feb 2011 17:06:30 GMT

"I place the xml file in my dist folder but it just does not work."

Is your "dist" folder in the running application's classpath?  If not, then 
you can't expect it to possibly get picked up.

1.  Log4j looks for config files in the default package.  For instance, in a 
webapp, this might be "WEB-INF/classes".  Or, it could be the root directory 
of a jar file.  As long as it isn't in a named package like "com.mycompany", 
then you are golden.  If you place the directory "/path/to/my/app" in the 
classpath, then you would place your config file in the directory 
"/path/to/my/app".

2.  Log4j look for 2 different types of config files: XML files and Properties 
files.  Because there are 2 possibilities, it has to prefer one over the 
other.  That is, it looks for one type and, if it doesn't find it, then looks 
for the other.  Log4j prefers XML files over Properties files.  That is, if 
log4j.xml is found, it doesn't bother looking for log4j.properties.  However, 
if log4j.xml is not found, then it looks for log4j.properties.  If you have 
both in the classpath, only log4j.xml will be used.  In order to use 
log4j.properties, ensure that log4j.xml is NOT in the classpath.

3.  Just to emphasize the points in #1, your config file HAS to be in a 
directory or jar file that is in the running application's classpath.  If it 
is not, then it won't be found by Log4j.  And if it is not in the default 
package, it also won't be found.


Jake


On Thu, 24 Feb 2011 11:38:38 -0500
 Bobby Richards <bobby.richards@gmail.com> wrote:
> I guess I just do not understand log4j very well.  I have spent the
> past two days searching and reading the mailing list but just cannot
> come up with an answer.
> 
> I am using Netbeans for development and I cannot understand where to
> put my log4j.xml file, nor can I seem to figure out the concept.
> 
> I have created the simplest project, I have tried the Properties
> loader but from what I have read it seems that log4j looks for the
> log4j.xml file:
> 
> public class Main {
>   static Logger logger = Logger.getLogger(Main.class);
>   static Properties properties = new Properties();
>   static FileInputStream fis;
>   /**
>    * @param args the command line arguments
>    */
>   public static void main(String[] args) {
> 
>       logger.info("info");
>       logger.debug("debug");
>       logger.warn("warn");
>   }
> }
> 
> I added log4j.xml into the src folder:
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
> <!--
>   Document   : logging.xml
>   Created on : February 23, 2011, 12:34 PM
>   Author     : bobby
>   Description:
>       Purpose of the document follows.
> -->
> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
>       <appender name="console" 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="file" class="org.apache.log4j.FileAppender">
>               <param name="File" value="/home/bobby/test.log"/>
>               <param name="Append" value="false"/>
>               <layout class="org.apache.log4j.PatternLayout">
>                           <param name="ConversionPattern"
value="%t
> %-5p %c{2} - %m%n"/>
>               </layout>
>       </appender>
>       <root>
>               <priority value="info"/>
>               <appender-ref ref="console"/>
>       </root>
> </log4j:configuration>
> 
> I can only seem to get this to act properly when I "clean and build".
> Which makes no sense to me, the whole point of a config file is to
> change before run, but I am only getting the desired result if the
> code is essentially burned in.
> 
>Further more, when I try to distribute my jar I place the xml file in
> my dist folder but it just does not work.
> 
> At this point I am just beyond frustration, any help would be
> appreciated.  I realize that this is the most basic of problems and I
> apologize.
> 
> ---------------------------------------------------------------------
> 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


Mime
View raw message