Java 摆脱 derby.log
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1004327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting rid of derby.log
提问by Allan Lykke Christensen
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 guideI 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.
我在 Maven 项目中使用 Apache Derby 嵌入式数据库进行单元测试。不幸的是,每当我运行测试时,我都会derby.log
在项目的根目录中找到该文件。数据库本身是在target
目录 ( jdbc:derby:target/unittest-db;create=true
) 中创建的,因此这不是问题。在查阅参考指南后,我尝试logDevice
在 JDBC url ( jdbc:derby:target/unittest-db;create=true;logDevice=/mylogs
)上设置参数,但这似乎是针对不同的日志,因此derby.log
仍然出现。
Any help is much appreciated.
任何帮助深表感谢。
采纳答案by stevedbrown
You can get rid of derby.log
file by creating the following class
您可以derby.log
通过创建以下类来摆脱文件
public class DerbyUtil {
public static final OutputStream DEV_NULL = new OutputStream() {
public void write(int b) {}
};
}
and setting the JVM system property derby.stream.error.field
, for example, using the following JVM command-line argument:
并设置 JVM 系统属性derby.stream.error.field
,例如,使用以下 JVM 命令行参数:
-Dderby.stream.error.field=DerbyUtil.DEV_NULL
回答by Laurent Fournié
Derby lets 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'.
Derby 允许您使用 Java 系统属性指定将错误日志消息写入的文件的名称derby.stream.error.file
。默认值为“derby.log”。
To get rid of derby.log
during the Maven surefire testing phase, I just add the property definition in the plugin configuration as follows:
为了derby.log
在 Maven surefire 测试阶段摆脱,我只是在插件配置中添加属性定义如下:
<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>
回答by yuvraj
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.
我想出了另一个解决方案。试试这个;它对我有用。我在这里所做的是更改了 System.stream.error.file 路径并将其设置为我的属性文件下的属性之一。只需将下面给定的代码添加到您的 applicationContext.xml 文件中即可。
<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>
回答by SteveR
Include the following in your derby.properties file:
在 derby.properties 文件中包含以下内容:
derby.stream.error.file=/dev/null
( or
( 或者
derby.stream.error.file=\Device\Null
on Windows)
在 Windows 上)
回答by Fabian
For integration tests the situation might be a bit more tricky than the simple surefireproperty.
Specifying the property derby.stream.error.file
in the maven-failsafe-plugin
will not work as the server environment does not inherit from that plugin (obviously using maven-surefire-plugin
makes no differences).
对于集成测试,情况可能比简单的surefire属性更棘手。指定的财产derby.stream.error.file
在maven-failsafe-plugin
为服务器环境将无法正常工作不会从插件继承(显然使用maven-surefire-plugin
是没有差异)。
Instead you need to modify the actual server start plugin. The following example is for the maven-jetty-plugin
:
相反,您需要修改实际的服务器启动插件。以下示例适用于maven-jetty-plugin
:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<systemProperties>
<!-- Get rid of that missplaced derby.log. -->
<systemProperty>
<name>derby.stream.error.file</name>
<value>${project.build.directory}/derby.log</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
Note that for some reason we use systemProperty
and not just property
as in the surefire solution.
请注意,出于某种原因,我们使用systemProperty
而不仅仅是property
在万无一失的解决方案中。
回答by tashuhka
If you don't have access to the configuration, you can execute this before making the connection:
如果您无权访问配置,则可以在建立连接之前执行此操作:
System.setProperty("derby.stream.error.field", "MyApp.DEV_NULL");
回答by carlspring
This is not a solution to your derby.log
file problem, (which numerous people have already shown how to resolve), but rather -- a suggestion. Why not use the derby-maven-pluginfor your tests? It places the derby.log
file under target/derby
, hence not leaving any litter.
这不是您的derby.log
文件问题的解决方案(很多人已经展示了如何解决),而是 - 一个建议。为什么不使用derby-maven-plugin进行测试?它将derby.log
文件放在 下target/derby
,因此不会留下任何垃圾。
As described in my answer here, you can use Derby as your database via the derby-maven-plugin which I wrote and is available on GitHuband via Maven Central.
正如我在此处的回答中所述,您可以通过我编写的 derby-maven-plugin 将 Derby 用作您的数据库,该插件可在GitHub和 Maven Central 上获得。
回答by ramis
You can also just set derby home to target/derby
or target
via:
您也可以将 derby home 设置为target/derby
或target
通过:
System.setProperty("derby.system.home", new File("target/derby").getAbsolutePath());
and then use the JDBC URL jdbc:derby:unittest-db;create=true
.
Then derby.log appears in the right folder.
然后使用 JDBC URL jdbc:derby:unittest-db;create=true
。然后 derby.log 出现在正确的文件夹中。