views:

757

answers:

3

I'm using the Apache Derby embedded database for unit testing in a Maven project. Unfortunately whenever I run the test I end up with the derby.log file in the root of the project. The database itself is created in the target directory (jdbc:derby:target/unittest-db;create=true) so that is not a problem. After consulting the reference guide I tried setting the logDevice parameter on the JDBC url (jdbc:derby:target/unittest-db;create=true;logDevice=/mylogs) but that seems to be for a different log, hence derby.log still appears.

Any help is much appreciated.

+2  A: 

You can get rid of derby.log file by creating a class DerbyUtil with the static

public static final OutputStream DEV_NULL = new OutputStream() {         
    public void write(int b) { }     
};

and setting

derby.stream.error.field to "DerbyUtil.DEV_NULL".

Credit to

http://davidvancouvering.blogspot.com/2007/10/quiet-time-and-how-to-suppress-derbylog.html
stevedbrown
What if I'm using Derby through Hibernate? Where to I put the line "derby.stream.error.field"?
pek
In a webapp it's a pain to set system props. Any way around it?
bmargulies
I don't think so, but you can set things up in your web.xml or spring config to run at startup and do it there.
stevedbrown
+2  A: 

Derby let you specify the name of the file to which the error log messages are written using the Java system property 'derby.stream.error.file'

The default value is 'derby.log'. See http://db.apache.org/derby/docs/10.5/ref/rrefproper18151.html

To get rid of the derby.log during the Maven surefire testing phase, I just add the property definition in the plugin configuration as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <systemProperties>
            <property>
                <name>derby.stream.error.file</name>
                <value>target/derby.log</value>
            </property>
        </systemProperties>
    </configuration>
</plugin>
Laurent Fournié
A: 

I have came up with another solution. Try this out it worked for me. What I am doing here is I have changed the System.stream.error.file path and set it to one of the properties present under my property file. just adding the below given code to your applicationContext.xml file will work.

<bean id="setDerbyLog" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  <property name="targetClass"><value>java.lang.System</value></property>
  <property name="targetMethod"><value>setProperty</value></property>
  <property name="arguments">
    <list>
      <value>derby.stream.error.file</value>
      <value>${derby.stream.error.file}</value>
    </list>
  </property>
</bean>

-Yuvraj

yuvraj