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 wleana <wl...@yahoo.com> on 2003/04/01 02:49:12 UTC

no log output when invoked from ANT/JUnit

Hi,
I have a LogTest program that can successfully write to syslog using 
Log4J *when I invoke it standalone*, ie. java ***.**.LogTest. But 
when I do it from ANT/JUnit, there is no log message written. 

I tried around, and found that if I add the call 
PropertyConfigurator.configure("log4j.properties") in my code, then 
it will write the log message when I run ANT/JUnit.

But why do I need to do it just for ANT/Junit env, while it is not 
needed when I run it standalone? I don't want to hard code that way 
in the code.

For the standalone way, I didn't specify log4j.configuration or 
invoke PropertyConfigurator.configure(..), all I do is to put 
log4j.properties file in my classpath and it works. 

Thanks for your help,
-wleana




---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org


Re: no log output when invoked from ANT/JUnit

Posted by Erik Price <ep...@ptc.com>.

wleana wrote:
> You are right, I don't have the directory in the ANT's class path, 
> but nevertheless, ANT can find those classes under it...I have two 
> things that need to be clarified -

If I am not mistaken, does not Ant inherit your $CLASSPATH environment 
variable as the Ant property named "java.class.path"?  So it /might/ 
have been in Ant's classpath without you having known about it, but I 
haven't seen your ant script so I'm not sure.

> 1. You said you need to specify log4j.configuration..But I didn't 
> have to specify it, and it works fine...how come?

If you don't specify a System property named "log4j.configuration", then 
by default Log4J initializes its "resource" variable to the string 
"log4j.properties".  Log4J then creates a URL from this string and 
attempts to resolve it, and uses the file at that URL as the 
configuration file.  If it cannot create a URL from it, then Log4J 
searches through the current class loader's class path for a file with 
that name.  Perhaps it was somehow finding your log4j.properties file -- 
try renaming it to something else and see if it still works.

> 2. Is there a way to specify a default configuration programmatically 
> (ie. in the code, specify the Appender, Layout, etc...) and make it 
> picked-up if the log4j.properties is not found?  Since i don't even 
> explicitly call PropertyConfigurator.configure in my code, how can 
> this kind of fall-back logic be implemented?

Well, you could always put that programmatic configuration inside of an 
if-block which checks to see if "log4j.configuration" system property is 
set... but this sounds an awful lot like what Log4J does by default.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org


Re: no log output when invoked from ANT/JUnit

Posted by wleana <wl...@yahoo.com>.
You are right, I don't have the directory in the ANT's class path, 
but nevertheless, ANT can find those classes under it...I have two 
things that need to be clarified -
1. You said you need to specify log4j.configuration..But I didn't 
have to specify it, and it works fine...how come?
2. Is there a way to specify a default configuration programmatically 
(ie. in the code, specify the Appender, Layout, etc...) and make it 
picked-up if the log4j.properties is not found?  Since i don't even 
explicitly call PropertyConfigurator.configure in my code, how can 
this kind of fall-back logic be implemented?
Thanks a lot.
wleana



--- In Log4J@yahoogroups.com, Erik Price <ep...@p...> wrote:
> 
> 
> wleana wrote:
> > Hi,
> > I have a LogTest program that can successfully write to syslog 
using 
> > Log4J *when I invoke it standalone*, ie. java ***.**.LogTest. But 
> > when I do it from ANT/JUnit, there is no log message written. 
> > 
> > I tried around, and found that if I add the call 
> > PropertyConfigurator.configure("log4j.properties") in my code, 
then 
> > it will write the log message when I run ANT/JUnit.
> > 
> > But why do I need to do it just for ANT/Junit env, while it is 
not 
> > needed when I run it standalone? I don't want to hard code that 
way 
> > in the code.
> > 
> > For the standalone way, I didn't specify log4j.configuration or 
> > invoke PropertyConfigurator.configure(..), all I do is to put 
> > log4j.properties file in my classpath and it works. 
> 
> I do not know for certain but is it possible that you did not have 
the 
> file in your Ant script's <classpath> ?
> 
> I have been using Ant and Log4J with JUnit successfully for the 
past 
> couple of days by doing this:
> 
> 1. I have a file named "testlogconfig.properties" located at some 
> specified point in my source tree.
> 
> 2. I have Ant set a system property with <sysproperty> named 
> "log4j.configuration" whose value is the path to my properties 
file, 
> prepended by the characters: "file://" (so it looks like this:
>      <property name="test.log4j.config"
>            location="path/to/properties/file"/>
>      <junit
>          ...(other attributes go here)...
>            >
>        <sysproperty name="log4j.configuration"
>                    value="file://${test.log4j.config}"/>
>      </junit>
> ).
> 
> 3. Log4J automatically checks for this system property and 
evaluates the 
> value to a URL and searches for the file at that URL.  In my case, 
on 
> Win2k, I had to use the "file://" prefix but I have heard that you 
don't 
> necessarily have to use that on Unix systems.
> 
> Maybe that will help?
> 
> 
> Erik
> 
> 
> --------------------------------------------------------------------
-
> To unsubscribe, e-mail: log4j-user-unsubscribe@j...
> For additional commands, e-mail: log4j-user-help@j...


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org


Re: no log output when invoked from ANT/JUnit

Posted by Erik Price <ep...@ptc.com>.

wleana wrote:
> Hi,
> I have a LogTest program that can successfully write to syslog using 
> Log4J *when I invoke it standalone*, ie. java ***.**.LogTest. But 
> when I do it from ANT/JUnit, there is no log message written. 
> 
> I tried around, and found that if I add the call 
> PropertyConfigurator.configure("log4j.properties") in my code, then 
> it will write the log message when I run ANT/JUnit.
> 
> But why do I need to do it just for ANT/Junit env, while it is not 
> needed when I run it standalone? I don't want to hard code that way 
> in the code.
> 
> For the standalone way, I didn't specify log4j.configuration or 
> invoke PropertyConfigurator.configure(..), all I do is to put 
> log4j.properties file in my classpath and it works. 

I do not know for certain but is it possible that you did not have the 
file in your Ant script's <classpath> ?

I have been using Ant and Log4J with JUnit successfully for the past 
couple of days by doing this:

1. I have a file named "testlogconfig.properties" located at some 
specified point in my source tree.

2. I have Ant set a system property with <sysproperty> named 
"log4j.configuration" whose value is the path to my properties file, 
prepended by the characters: "file://" (so it looks like this:
     <property name="test.log4j.config"
           location="path/to/properties/file"/>
     <junit
         ...(other attributes go here)...
           >
       <sysproperty name="log4j.configuration"
                   value="file://${test.log4j.config}"/>
     </junit>
).

3. Log4J automatically checks for this system property and evaluates the 
value to a URL and searches for the file at that URL.  In my case, on 
Win2k, I had to use the "file://" prefix but I have heard that you don't 
necessarily have to use that on Unix systems.

Maybe that will help?


Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org