Discussion:
[maven-user] Getting log4j output during unit tests
John Prystash
2009-03-04 22:26:29 UTC
Permalink
Hi, I have some logging statements in my classes under test that would I like to have run during my unit tests.

I put a simple log4j.properties file under src/test/resources:

log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%t][%d{ISO8601}] [%C.%M] - %m%n

When I run mvn test, the file ends up in target/test-classes like I'd expect, but I see no output from the logging statements when my tests run.

There is no src/main/resources/log4j.properties file to conflict with, but I then explicitly configured the surefire plugin to use log4j.properties:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<systemProperties>
<property>
<name>log4j.configuration</name>
<value>file:target/test-classes/log4j.properties</value>
</property>
</systemProperties>
</configuration>
</plugin>

With no change. The *output.txt files for my tests were empty.

I added the following statements to my test setup, I get some output:

myObjectUnderTest.logger = new SimpleLog(this.class.name)
myObjectUnderTest.logger.level = SimpleLog.LOG_LEVEL_DEBUG

My loggers are defined with:

private Log logger = LogFactory.getLog(this.getClass());

I'm using commons-logging-1.1.1.jar.

Thanks in advance for any insight.
Dennis Lundberg
2009-03-04 22:50:41 UTC
Permalink
Do you have both commons-logging and log4j as dependencies in your
project with the scope test?

You could try to add the file src/test/resources/commons-logging.properties
with the following single line in it:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

This tells commons-logging to explicitly use log4j as the logging
implementation.
Post by John Prystash
Hi, I have some logging statements in my classes under test that would I like to have run during my unit tests.
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%t][%d{ISO8601}] [%C.%M] - %m%n
When I run mvn test, the file ends up in target/test-classes like I'd expect, but I see no output from the logging statements when my tests run.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<systemProperties>
<property>
<name>log4j.configuration</name>
<value>file:target/test-classes/log4j.properties</value>
</property>
</systemProperties>
</configuration>
</plugin>
With no change. The *output.txt files for my tests were empty.
myObjectUnderTest.logger = new SimpleLog(this.class.name)
myObjectUnderTest.logger.level = SimpleLog.LOG_LEVEL_DEBUG
private Log logger = LogFactory.getLog(this.getClass());
I'm using commons-logging-1.1.1.jar.
Thanks in advance for any insight.
---------------------------------------------------------------------
--
Dennis Lundberg
John Prystash
2009-03-04 23:28:31 UTC
Permalink
I only depend on commons-logging, not log4j. log4j is provided by my container, which is included in the same assembly, so at runtime logging works.

I have commons-logging at scope compile. Another module that depends on the module in question packages up an assembly that includes all runtime dependencies, which implicitly includes commons-logging.

I added src/test/resources/commons-logging.properties as you suggested and then log4j in test scope:

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>test</scope>
</dependency>

With or without the additional surefire configuration referencing log4j.properties, I unfortunately don't see any output.
I could be missing something fundamental here with how these libraries work by chance.



----- Original Message ----
From: Dennis Lundberg <***@apache.org>
To: Maven Users List <***@maven.apache.org>
Sent: Wednesday, March 4, 2009 5:50:41 PM
Subject: Re: [maven-user] Getting log4j output during unit tests

Do you have both commons-logging and log4j as dependencies in your
project with the scope test?

You could try to add the file src/test/resources/commons-logging.properties
with the following single line in it:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

This tells commons-logging to explicitly use log4j as the logging
implementation.
Post by John Prystash
Hi, I have some logging statements in my classes under test that would I like to have run during my unit tests.
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%t][%d{ISO8601}] [%C.%M] - %m%n
When I run mvn test, the file ends up in target/test-classes like I'd expect, but I see no output from the logging statements when my tests run.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<systemProperties>
<property>
<name>log4j.configuration</name>
<value>file:target/test-classes/log4j.properties</value>
</property>
</systemProperties>
</configuration>
</plugin>
With no change. The *output.txt files for my tests were empty.
myObjectUnderTest.logger = new SimpleLog(this.class.name)
myObjectUnderTest.logger.level = SimpleLog.LOG_LEVEL_DEBUG
private Log logger = LogFactory.getLog(this.getClass());
I'm using commons-logging-1.1.1.jar.
Thanks in advance for any insight.
---------------------------------------------------------------------
--
Dennis Lundberg

---------------------------------------------------------------------
To unsubscribe, e-mail: users-***@maven.apache.org
For additional commands, e-mail: users-***@maven.apache.org
Dennis Lundberg
2009-03-05 21:53:34 UTC
Permalink
Post by John Prystash
I only depend on commons-logging, not log4j. log4j is provided by my container, which is included in the same assembly, so at runtime logging works.
Yes, but your container is not available when surefire runs the tests,
so you need to add log4j as a test scope dependency in Maven, like you
have specified a few lines down.
Post by John Prystash
I have commons-logging at scope compile. Another module that depends on the module in question packages up an assembly that includes all runtime dependencies, which implicitly includes commons-logging.
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>test</scope>
</dependency>
With or without the additional surefire configuration referencing log4j.properties, I unfortunately don't see any output.
I could be missing something fundamental here with how these libraries work by chance.
Just to make sure commons-logging is working for you, try this.

Replace the line in commons-logging.properties with this one, to use the
internal simple logging implementation available in commons-logging:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog

If that gives you output, then the problem lies with the log4j
configuration.
Post by John Prystash
----- Original Message ----
Sent: Wednesday, March 4, 2009 5:50:41 PM
Subject: Re: [maven-user] Getting log4j output during unit tests
Do you have both commons-logging and log4j as dependencies in your
project with the scope test?
You could try to add the file src/test/resources/commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
This tells commons-logging to explicitly use log4j as the logging
implementation.
Post by John Prystash
Hi, I have some logging statements in my classes under test that would I like to have run during my unit tests.
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%t][%d{ISO8601}] [%C.%M] - %m%n
When I run mvn test, the file ends up in target/test-classes like I'd expect, but I see no output from the logging statements when my tests run.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<systemProperties>
<property>
<name>log4j.configuration</name>
<value>file:target/test-classes/log4j.properties</value>
</property>
</systemProperties>
</configuration>
</plugin>
With no change. The *output.txt files for my tests were empty.
myObjectUnderTest.logger = new SimpleLog(this.class.name)
myObjectUnderTest.logger.level = SimpleLog.LOG_LEVEL_DEBUG
private Log logger = LogFactory.getLog(this.getClass());
I'm using commons-logging-1.1.1.jar.
Thanks in advance for any insight.
---------------------------------------------------------------------
--
Dennis Lundberg
John Prystash
2009-03-05 23:00:07 UTC
Permalink
Thanks for getting back to me Dennis. I got to play around with your suggestions (but not for as long as I would of liked). log4j is in test scope, commons-logging still compile.

Changing commons-logging.properties to use SimpleLog as you suggested does not produce any output for me.
Post by John Prystash
Post by John Prystash
myObjectUnderTest.logger = new SimpleLog(this.class.name)
myObjectUnderTest.logger.level = SimpleLog.LOG_LEVEL_DEBUG
I simplified my src/test/resources/log4j.properties to:

log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender

I might be worth mentioning that my test classes are Groovy classes and the classes under test are Java?
Thanks


----- Original Message ----
From: Dennis Lundberg <***@apache.org>
To: Maven Users List <***@maven.apache.org>
Sent: Thursday, March 5, 2009 4:53:34 PM
Subject: Re: [maven-user] Getting log4j output during unit tests
Post by John Prystash
I only depend on commons-logging, not log4j. log4j is provided by my container, which is included in the same assembly, so at runtime logging works.
Yes, but your container is not available when surefire runs the tests,
so you need to add log4j as a test scope dependency in Maven, like you
have specified a few lines down.
Post by John Prystash
I have commons-logging at scope compile. Another module that depends on the module in question packages up an assembly that includes all runtime dependencies, which implicitly includes commons-logging.
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>test</scope>
</dependency>
With or without the additional surefire configuration referencing log4j.properties, I unfortunately don't see any output.
I could be missing something fundamental here with how these libraries work by chance.
Just to make sure commons-logging is working for you, try this.

Replace the line in commons-logging.properties with this one, to use the
internal simple logging implementation available in commons-logging:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog

If that gives you output, then the problem lies with the log4j
configuration.
Post by John Prystash
----- Original Message ----
Sent: Wednesday, March 4, 2009 5:50:41 PM
Subject: Re: [maven-user] Getting log4j output during unit tests
Do you have both commons-logging and log4j as dependencies in your
project with the scope test?
You could try to add the file src/test/resources/commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
This tells commons-logging to explicitly use log4j as the logging
implementation.
Post by John Prystash
Hi, I have some logging statements in my classes under test that would I like to have run during my unit tests.
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%t][%d{ISO8601}] [%C.%M] - %m%n
When I run mvn test, the file ends up in target/test-classes like I'd expect, but I see no output from the logging statements when my tests run.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<systemProperties>
<property>
<name>log4j.configuration</name>
<value>file:target/test-classes/log4j.properties</value>
</property>
</systemProperties>
</configuration>
</plugin>
With no change. The *output.txt files for my tests were empty.
myObjectUnderTest.logger = new SimpleLog(this.class.name)
myObjectUnderTest.logger.level = SimpleLog.LOG_LEVEL_DEBUG
private Log logger = LogFactory.getLog(this.getClass());
I'm using commons-logging-1.1.1.jar.
Thanks in advance for any insight.
---------------------------------------------------------------------
--
Dennis Lundberg

---------------------------------------------------------------------
To unsubscribe, e-mail: users-***@maven.apache.org
For additional commands, e-mail: users-***@maven.apache.org
stash1071
2009-04-30 15:36:58 UTC
Permalink
With a simpler project, using Groovy, I was able to get this work.

Turns out I only need commons-logging at compile scope, and I don't need
log4j at test scope, but it works either way.

My src/test/resources/log4j.properties:
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%t][%d{ISO8601}]
[%C.%M] - %m%n

Not sure why I was having problems before, but thanks for all the
suggestions.
Post by John Prystash
Thanks for getting back to me Dennis. I got to play around with your
suggestions (but not for as long as I would of liked). log4j is in test
scope, commons-logging still compile.
Changing commons-logging.properties to use SimpleLog as you suggested does
not produce any output for me.
Also, my Cobertura report reports I've hit my logging statements only when
Post by John Prystash
Post by John Prystash
myObjectUnderTest.logger = new SimpleLog(this.class.name)
myObjectUnderTest.logger.level = SimpleLog.LOG_LEVEL_DEBUG
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
I might be worth mentioning that my test classes are Groovy classes and
the classes under test are Java?
Thanks
----- Original Message ----
Sent: Thursday, March 5, 2009 4:53:34 PM
Subject: Re: [maven-user] Getting log4j output during unit tests
Post by John Prystash
I only depend on commons-logging, not log4j. log4j is provided by my
container, which is included in the same assembly, so at runtime logging
works.
Yes, but your container is not available when surefire runs the tests,
so you need to add log4j as a test scope dependency in Maven, like you
have specified a few lines down.
Post by John Prystash
I have commons-logging at scope compile. Another module that depends on
the module in question packages up an assembly that includes all runtime
dependencies, which implicitly includes commons-logging.
I added src/test/resources/commons-logging.properties as you suggested
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>test</scope>
</dependency>
With or without the additional surefire configuration referencing
log4j.properties, I unfortunately don't see any output.
I could be missing something fundamental here with how these libraries work by chance.
Just to make sure commons-logging is working for you, try this.
Replace the line in commons-logging.properties with this one, to use the
org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
If that gives you output, then the problem lies with the log4j
configuration.
Post by John Prystash
----- Original Message ----
Sent: Wednesday, March 4, 2009 5:50:41 PM
Subject: Re: [maven-user] Getting log4j output during unit tests
Do you have both commons-logging and log4j as dependencies in your
project with the scope test?
You could try to add the file
src/test/resources/commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
This tells commons-logging to explicitly use log4j as the logging
implementation.
Post by John Prystash
Hi, I have some logging statements in my classes under test that would I
like to have run during my unit tests.
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p
[%t][%d{ISO8601}] [%C.%M] - %m%n
When I run mvn test, the file ends up in target/test-classes like I'd
expect, but I see no output from the logging statements when my tests
run.
There is no src/main/resources/log4j.properties file to conflict with,
but I then explicitly configured the surefire plugin to use
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<systemProperties>
<property>
<name>log4j.configuration</name>
<value>file:target/test-classes/log4j.properties</value>
</property>
</systemProperties>
</configuration>
</plugin>
With no change. The *output.txt files for my tests were empty.
myObjectUnderTest.logger = new SimpleLog(this.class.name)
myObjectUnderTest.logger.level = SimpleLog.LOG_LEVEL_DEBUG
private Log logger = LogFactory.getLog(this.getClass());
I'm using commons-logging-1.1.1.jar.
Thanks in advance for any insight.
---------------------------------------------------------------------
--
Dennis Lundberg
---------------------------------------------------------------------
---------------------------------------------------------------------
--
View this message in context: http://www.nabble.com/-maven-user--Getting-log4j-output-during-unit-tests-tp22340946p23318807.html
Sent from the Maven - Users mailing list archive at Nabble.com.
Loading...