Java OpenJPA:为什么SQL跟踪会转到System.err?
问题描述: 我已决定将System.err重定向到一个文件:Java OpenJPA:为什么SQL跟踪会转到System.err?,java,openjpa,Java,Openjpa,问题描述: 我已决定将System.err重定向到一个文件: System.setErr(new PrintStream(error.log)); OpenJPA日志设置: connectionProperties.put("openjpa.Log", "SQL=TRACE, DefaultLevel=INFO, Runtime=INFO, Tool=INFO"); 现在,由于某种原因,不仅异常,我的SQL跟踪也会转到该文件。 知道如何将SQL跟踪重定向回控制台吗 更新: 根据建议,我尝试
System.setErr(new PrintStream(error.log));
OpenJPA日志设置:
connectionProperties.put("openjpa.Log", "SQL=TRACE, DefaultLevel=INFO, Runtime=INFO, Tool=INFO");
现在,由于某种原因,不仅异常,我的SQL跟踪也会转到该文件。
知道如何将SQL跟踪重定向回控制台吗
更新:
根据建议,我尝试将OpenJPA设置为在persistence.xml中使用log4j-line:
<property name="openjpa.Log" value="log4j"/>
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">
<persistence-unit name="XXX" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>...</class>
<properties>
<!-- Recommended to disable this property, since we do JPA Enhancement during the build. See an ant script: openJPA_enhance.xml -->
<property name="openjpa.RuntimeUnenhancedClasses" value="unsupported"/>
<!-- Caching. TODO: Optimize if needed during the testing phase -->
<!-- We shouldn't use Data cache data at this moment. -->
<property name="openjpa.DataCache" value="false"/>
<!-- The QueryStatistics can be accessed via PreparedQueryCache.getStatistics(). -->
<property name="openjpa.jdbc.QuerySQLCache" value="true(EnableStatistics=true)" />
<property name="openjpa.QueryCompilationCache" value="true" />
<!-- TODO: Enable for OpenJPA 4.0 - Prevents caching the sequences -->
<!-- <property name="openjpa.jdbc.DBDictionary" value="disableAlterSeqenceIncrementBy=true" /> -->
<!-- enforce DBCP (build in) connection pool -->
<property name="openjpa.jdbc.DriverDataSource" value="dbcp" />
<!-- Use LOG4J -->
<property name="openjpa.Log" value="log4j" />
</properties>
</persistence-unit>
</persistence>
默认openJPA_enhance.xml文件:
<!DOCTYPE project>
<!-- see details: http://openjpa.apache.org/enhancement-with-eclipse.html -->
<!-- pass params: -Dopenjpa.libs=../lib -Dbuild.dir=./build/classes -->
<project name="jpa_enhance_builder">
<!--path id="openjpa.libs">
<pathelement location="./lib/openjpa-all-2.3.0.jar"/>
</path-->
<path id="enhance.cp">
<pathelement location="${basedir}${file.separator}${build.dir}" />
<fileset dir="${basedir}${file.separator}${openjpa.libs}">
<include name="**/*.jar" />
</fileset>
</path>
<property name="cp" refid="enhance.cp" />
<target name="openjpa.libs.check" unless="openjpa.libs">
<fail message="Please set -Dopenjpa.libs in your builder configuration!" />
</target>
<target name="build.dir.check" unless="build.dir">
<fail message="Please set -Dbuild.dir in your builder configuration!" />
</target>
<target name="enhance" depends="openjpa.libs.check, build.dir.check">
<echo message="${cp}" />
<taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask">
<classpath refid="enhance.cp" />
</taskdef>
<openjpac>
<classpath refid="enhance.cp" />
<config propertiesFile = "${basedir}/src/META-INF/persistence.xml"/>
</openjpac>
</target>
</project>
persistence.xml:
<property name="openjpa.Log" value="log4j"/>
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">
<persistence-unit name="XXX" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>...</class>
<properties>
<!-- Recommended to disable this property, since we do JPA Enhancement during the build. See an ant script: openJPA_enhance.xml -->
<property name="openjpa.RuntimeUnenhancedClasses" value="unsupported"/>
<!-- Caching. TODO: Optimize if needed during the testing phase -->
<!-- We shouldn't use Data cache data at this moment. -->
<property name="openjpa.DataCache" value="false"/>
<!-- The QueryStatistics can be accessed via PreparedQueryCache.getStatistics(). -->
<property name="openjpa.jdbc.QuerySQLCache" value="true(EnableStatistics=true)" />
<property name="openjpa.QueryCompilationCache" value="true" />
<!-- TODO: Enable for OpenJPA 4.0 - Prevents caching the sequences -->
<!-- <property name="openjpa.jdbc.DBDictionary" value="disableAlterSeqenceIncrementBy=true" /> -->
<!-- enforce DBCP (build in) connection pool -->
<property name="openjpa.jdbc.DriverDataSource" value="dbcp" />
<!-- Use LOG4J -->
<property name="openjpa.Log" value="log4j" />
</properties>
</persistence-unit>
</persistence>
org.apache.openjpa.persistence.PersistenceProviderImpl
...
我强烈建议使用log4j或其他日志库来处理堆栈跟踪和输出。我曾尝试使用log4j,但它破坏了openJPA增强过程(请参阅问题更新)。我可能应该开始一个与log4j相关的新线程,但我想保留我原来的问题。我发现这个问题已经解决了,所以我希望您能够控制异常。这个问题怎么了?问题解决了。1) 我使用Log4j将SQL跟踪输出重定向到控制台。2) OpenJPA增强器问题-我缺少Log4J的类路径