在h2.sh中加上 -trace
原文:
Using the Trace Options
To find problems in an application, it is sometimes good to see what database
operations where executed. This database offers the following trace features:
• Trace to System.out and/or to a file
• Support for trace levels OFF, ERROR, INFO, DEBUG
• The maximum size of the trace file can be set
• It is possible to generate Java source code from the trace file
• Trace can be enabled at runtime by manually creating a file
Trace Options
The simplest way to enable the trace option is setting it in the database URL.
There are two settings, one for System.out (TRACE_LEVEL_SYSTEM_OUT)
tracing, and one for file tracing (TRACE_LEVEL_FILE). The trace levels are 0 for
OFF, 1 for ERROR (the default), 2 for INFO, and 3 for DEBUG. A database URL
with both levels set to DEBUG is:
jdbc:h2:~/test;TRACE_LEVEL_FILE=3;TRACE_LEVEL_SYSTEM_OUT=3
The trace level can be changed at runtime by executing the SQL command SET
TRACE_LEVEL_SYSTEM_OUT level (for System.out tracing) or SET
TRACE_LEVEL_FILE level (for file tracing). Example:
SET TRACE_LEVEL_SYSTEM_OUT 3
Setting the Maximum Size of the Trace File
When using a high trace level, the trace file can get very big quickly. The default
size limit is 16 MB, if the trace file exceeds this limit, it is renamed to .old and a
new file is created. If another such file exists, it is deleted. To limit the size to a
certain number of megabytes, use SET TRACE_MAX_FILE_SIZE mb. Example:
SET TRACE_MAX_FILE_SIZE 1
Java Code Generation
When setting the trace level to INFO or DEBUG, Java source code is generated as
well. This simplifies reproducing problems. The trace file looks like this:
...
12-20 20:58:09 jdbc[0]:
/**/dbMeta3.getURL();
12-20 20:58:09 jdbc[0]:
/**/dbMeta3.getTables(null, "", null, new String[]{"TABLE", "VIEW"});
...
To filter the Java source code, use the ConvertTraceFile tool as follows:
java -cp h2*.jar org.h2.tools.ConvertTraceFile
-traceFile "~/test.trace.db" -javaClass "Test"
The generated file Test.java will contain the Java source code. The generated
source code may be too large to compile (the size of a Java method is limited). If
this is the case, the source code needs to be split in multiple methods. The
password is not listed in the trace file and therefore not included in the source
code.
Using Other Logging APIs
By default, this database uses its own native 'trace' facility. This facility is called
'trace' and not 'log' within this database to avoid confusion with the transaction
log. Trace messages can be written to both file and System.out. In most cases,
this is sufficient, however sometimes it is better to use the same facility as the
application, for example Log4j. To do that, this database support SLF4J.
SLF4J is a simple facade for various logging APIs and allows to plug in the desired
implementation at deployment time. SLF4J supports implementations such as
Logback, Log4j, Jakarta Commons Logging (JCL), Java logging, x4juli, and Simple
Log.
To enable SLF4J, set the file trace level to 4 in the database URL:
jdbc:h2:~/test;TRACE_LEVEL_FILE=4
Changing the log mechanism is not possible after the database is open, that
means executing the SQL statement SET TRACE_LEVEL_FILE 4 when the
database is already open will not have the desired effect. To use SLF4J, all
required jar files need to be in the classpath. The logger name is h2database. If it
does not work, check the file <database>.trace.db for error messages.