I have a question for the list that someone has probably run into (I'm
sure!)
I have a program which has a main and then a class that it calls. In the
mainline I
public class CFSMainline {
static Logger logger = Logger.getLogger(CFSMainline.class);
public static void main(String args[]) {
PropertyConfigurator.configure("log4j.properties");
logger.debug("in CFSMainline");
Abbreviation abbv = Abbreviation.getInstance();
}
}
IN ABBREVIATION
public class Abbreviation {
{ //Static Section - Notice all commented out!
// BasicConfigurator.configure();
// PropertyConfigurator.configure("log4j.properties");
}
static Logger logger = Logger.getLogger(Abbreviation.class);
private static Abbreviation ourInstance;
public synchronized static Abbreviation getInstance() { // singleton
if (ourInstance == null) {
ourInstance = new Abbreviation();
}
logger.debug("In getInstance for " + Abbreviation.class);
return ourInstance;
/** This is the private constructor for the Singleton class
Abbreviation
*
*/
private Abbreviation() {
logger.debug("In private constructor for Abbreviation");
<< S N I P >>
logger.debug("Exiting private constructor for Abbreviation");
}
}
IN LOG4J.PROPERTIES (I'm sure you'll recognize this one :) )
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
log4j.logger.com.growlingcow.aviation.cfs.data.Abbreviation=DEBUG,A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=(%F:%L) %-5p %c %x - %m%n
So anyway - all of this works fine!! It's great! The output looks like
(CFSMainline.java:28) DEBUG com.growlingcow.aviation.cfs.CFSMainline - in
CFSMainline
(Abbreviation.java:75) DEBUG
om.growlingcow.aviation.cfs.data.Abbreviation - In private constructor for
Abbreviation
(Abbreviation.java:75) DEBUG
om.growlingcow.aviation.cfs.data.Abbreviation - In private constructor for
Abbreviation
(Abbreviation.java:78) DEBUG
om.growlingcow.aviation.cfs.data.Abbreviation - Exiting private constructor
for Abbreviation
(Abbreviation.java:78) DEBUG
om.growlingcow.aviation.cfs.data.Abbreviation - Exiting private constructor
for Abbreviation
(Abbreviation.java:66) DEBUG
om.growlingcow.aviation.cfs.data.Abbreviation - In getInstance for class
com.growlingcow.aviation.cfs.data.Abbreviation
(Abbreviation.java:66) DEBUG
om.growlingcow.aviation.cfs.data.Abbreviation - In getInstance for class
com.growlingcow.aviation.cfs.data.Abbreviation
Ok so besides the question of why I get 2 log lines for one call (I'll work
on that later) , the problem comes in when I run a JUnit test suite
public class ProjectTestSuite extends TestCase {
static Logger logger= Logger.getLogger(ProjectTestSuite.class);
public ProjectTestSuite(String name) {
super(name);
}
public static Test suite() {
TestSuite theSuite = new TestSuite();
theSuite.addTest(com.growlingcow.aviation.AviationPackageSuite.suite());
theSuite.addTest(com.growlingcow.utilities.UtilitiesPackageTestSuite.suite()
);
return theSuite;
}
public static void main(String[] argv) {
PropertyConfigurator.configure("log4j.properties");
logger.info("In main method for " +
ProjectTestSuite.class.toString());
String[] testCaseList = {ProjectTestSuite.class.getName()};
junit.swingui.TestRunner.main(testCaseList);
}
}
During these unit test, the Abbreviation class gets called through
'getInstance'. The output I get from log4j is
(ProjectTestSuite.java:44) INFO com.growlingcow.ProjectTestSuite - In main
method for class com.growlingcow.ProjectTestSuite
log4j:WARN No appenders could be found for logger
(com.growlingcow.aviation.cfs.data.Abbreviation).
log4j:WARN Please initialize the log4j system properly.
Can someone help me out here? You'll notice that in Abbreviation I tried to
re-configure the log4j based once on the properties which gives the
following output if I uncomment
// PropertyConfigurator.configure("log4j.properties");
(ProjectTestSuite.java:44) INFO com.growlingcow.ProjectTestSuite - In main
method for class com.foo.ProjectTestSuite
log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to
a "org.apache.log4j.Appender" variable.
log4j:ERROR The class "org.apache.log4j.Appender" was loaded by
log4j:ERROR [EMAIL PROTECTED] whereas object of type
log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by
[EMAIL PROTECTED]
log4j:ERROR Could not instantiate appender named "A1".
log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to
a "org.apache.log4j.Appender" variable.
log4j:ERROR The class "org.apache.log4j.Appender" was loaded by
log4j:ERROR [EMAIL PROTECTED] whereas object of type
log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by
[EMAIL PROTECTED]
log4j:ERROR Could not instantiate appender named "A1".
log4j:WARN No appenders could be found for logger
(com.growlingcow.aviation.cfs.data.Abbreviation).
log4j:WARN Please initialize the log4j system properly.
Yet if I only uncomment the line
// BasicConfigurator.configure();
I get the following
(ProjectTestSuite.java:44) INFO com.growlingcow.ProjectTestSuite - In main
method for class com.growlingcow.ProjectTestSuite
0 [TestRunner-Thread] DEBUG
om.growlingcow.aviation.cfs.data.Abbreviation - In private constructor for
Abbreviation
40 [TestRunner-Thread] DEBUG
om.growlingcow.aviation.cfs.data.Abbreviation - Exiting private constructor
for Abbreviation
40 [TestRunner-Thread] DEBUG
om.growlingcow.aviation.cfs.data.Abbreviation - In getInstance for class
com.growlingcow.aviation.cfs.data.Abbreviation
60 [TestRunner-Thread] DEBUG
om.growlingcow.aviation.cfs.data.Abbreviation - In getInstance for class
com.growlingcow.aviation.cfs.data.Abbreviation
Which seems to say that I can't load the properties when running from JUnit
because of differing class loaders?
The command I run is from the command line is something like
E:\working\aviation>java -cp
./classes;./bin/junit3.7/junit.jar;./bin/jakarta-log4j-1.2.8/dist/lib/log4j-
1.2.8.jar com.growlingcow.ProjectTestSuite
and of course I get ....
(ProjectTestSuite.java:44) INFO com.growlingcow.ProjectTestSuite - In main
method for class com.growlingcow.ProjectTestSuite
log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to
a "org.apache.log4j.Appender" variable.
log4j:ERROR The class "org.apache.log4j.Appender" was loaded by
log4j:ERROR [EMAIL PROTECTED] whereas object of type
log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by
[EMAIL PROTECTED]
log4j:ERROR Could not instantiate appender named "A1".
log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to
a "org.apache.log4j.Appender" variable.
log4j:ERROR The class "org.apache.log4j.Appender" was loaded by
log4j:ERROR [EMAIL PROTECTED] whereas object of type
log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by
[EMAIL PROTECTED]
log4j:ERROR Could not instantiate appender named "A1".
log4j:WARN No appenders could be found for logger
(com.growlingcow.aviation.cfs.data.Abbreviation).
log4j:WARN Please initialize the log4j system properly.
Does anyone run log4j with JUnit out
there???????????????????????????????????????????????????
Please let me know how!
Cheers