Quantcast

log4j for MyBatis in a JUnit run configuration on Eclipse

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

log4j for MyBatis in a JUnit run configuration on Eclipse

maelstrom3
How can log4j be configured for MyBatis in a JUnit run configuration on Eclipse?

There are several such questions on SO and the internet. But, most of the suggestions don't seem to work for me.

Specifically, the following doesn't seem to work: (from the MyBatis logging page)
1. log4j-1.2.16.jar is on my Eclipse's Java Build Path Libraries (under Maven Dependencies).
2. log4j.properties is on my Run Configuration. It's under VM arguments as: -Dlog4j.configuration=C:\log4j.properties
3. My log4j.properties file contains the below code.

log4j.rootLogger=ERROR, stdout
log4j.logger.org.mybatis.example.BlogMapper=TRACE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
As per my understanding, things will work if I can keep the log4j.properties file on the Eclipse classpath. But, I'm not sure if the run configuration VM arguments sets the classpath.

EDIT
If I force MyBatis to use log4j,
org.apache.ibatis.logging.LogFactory.useLog4JLogging();
The following is output on the console:
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: log4j for MyBatis in a JUnit run configuration on Eclipse

Guy Rouillier-2
On 1/31/2013 1:30 PM, maelstrom3 wrote:
> How can *log4j *be configured for *MyBatis *in a *JUnit *run
> configuration on *Eclipse*?

I've done it fairly easily.

> 2. log4j.properties is on my Run Configuration. It's under VM arguments
> as: -Dlog4j.configuration=C:\log4j.properties

I don't use this.

> 3. My log4j.properties file contains the below code.
>
> log4j.rootLogger=ERROR, stdout
> log4j.logger.org.mybatis.example.BlogMapper=TRACE
> log4j.appender.stdout=org.apache.log4j.ConsoleAppender
> log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
> log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

You need more than that:

log4j.logger.com.ibatis=DEBUG
log4j.logger.org.apache.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG

> As per my understanding, things will work if I can keep the
> log4j.properties file on the Eclipse classpath. But, I'm *not sure if
> the run configuration VM arguments sets the classpath*.

I believe this is the core issue.  When you try to run or debug, look at
Run Configurations... .  Make sure your configuration is under JUnit,
and that Test runner is set to JUnit 4.  I don't have anything in
Arguments, Classpath, Source or Environment other than what is added
automatically from the project.

However, I did have to add the folder containing log4j.properties as a
Source folder to the project.  That's because I'm working with a web
project, and all the sources end up in WEB-INF/classes after being
built, and that directory is constitutes the classpath for a web app.

You don't say what type of app you are building, but make sure that
log4j.properties ends up on the classpath for JUnit, and you should be
all set.

>
> *EDIT*
> If I force MyBatis to use log4j,
> org.apache.ibatis.logging.LogFactory.useLog4JLogging();
> The following is output on the console:
> log4j:WARN No appenders could be found for logger
> (org.apache.ibatis.logging.LogFactory).
> log4j:WARN Please initialize the log4j system properly.

That's pretty convincing evidence that your JUnit runtime environment is
not finding log4j.properties.

--
Guy Rouillier

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: log4j for MyBatis in a JUnit run configuration on Eclipse

maelstrom3
Guy Rouillier-2 wrote
> 3. My log4j.properties file contains the below code.
>
> log4j.rootLogger=ERROR, stdout
> log4j.logger.org.mybatis.example.BlogMapper=TRACE
> log4j.appender.stdout=org.apache.log4j.ConsoleAppender
> log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
> log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

You need more than that:

log4j.logger.com.ibatis=DEBUG
log4j.logger.org.apache.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
This worked well. It was the missing piece.

Guy Rouillier-2 wrote
> *EDIT*
> If I force MyBatis to use log4j,
> org.apache.ibatis.logging.LogFactory.useLog4JLogging();
> The following is output on the console:
> log4j:WARN No appenders could be found for logger
> (org.apache.ibatis.logging.LogFactory).
> log4j:WARN Please initialize the log4j system properly.

That's pretty convincing evidence that your JUnit runtime environment is
not finding log4j.properties.
After putting the file in the source folder, my eclipse was probably not copying the log4j.properties file into the target/test-classes folder. So, I had to clean and rebuild the project.

Thanks Guy Rouillier-2, for the tip.
Loading...