Java 在休眠中包含 log4j 属性文件以显示带有值而不是问号的查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18634892/
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
include log4j properties file in hibernate to show query with value instead of question mark
提问by commit
I have crearte log4j.propertiesfile like below:
我有如下创建log4j.properties文件:
log4j.logger.org.hibernate=INFO, hb
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.hql=debug
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug
log4j.appender.hb=org.apache.log4j.ConsoleAppender
log4j.appender.hb.layout=org.apache.log4j.PatternLayout
log4j.appender.hb.layout.ConversionPattern=HibernateLog --> %d{HH:mm:ss} %-5p %c - %m%n
log4j.appender.hb.Threshold=TRACE
Can someone help me to how to include it in hibernate.cfg.xml
file? I am sorry I actually don't know how log4j works. I create this to display my hibernate query with value
instead of ?
but still it displays ?
nothing changes so what I need to proceed further?
有人可以帮助我如何将其包含在hibernate.cfg.xml
文件中吗?对不起,我实际上不知道 log4j 是如何工作的。我创建它来显示我的休眠查询value
而不是,?
但它仍然显示?
没有任何变化,所以我需要进一步处理什么?
I took reference from here Hibernate show real SQL
我从这里参考了Hibernate show real SQL
采纳答案by GreyFairer
The 'show_sql' property in your hibernate.cfg.xml causes the queries to be printed directly to the console.
hibernate.cfg.xml 中的“show_sql”属性会导致查询直接打印到控制台。
Log4j allows output to be logged anywhere from console to file to network ports to databases. But the simple configuration that you have is supposed to print on the console as well. So first remove the show_sql property to see if Log4j puts anything on the console at all.
Log4j 允许在从控制台到文件到网络端口到数据库的任何地方记录输出。但是您拥有的简单配置也应该打印在控制台上。因此,首先删除 show_sql 属性以查看 Log4j 是否在控制台上放置了任何内容。
If this doesn't work, it shows that Log4j is not configured correctly. If you're using hibernate > 3.5, it uses the slf4j api, which uses logback by default instead of log4j. You can easily switch to log4j by removing logback jar(s) from your classpath, and adding slf4j-log4j12.jar and log4j.jar instead.
如果这不起作用,则表明 Log4j 配置不正确。如果您使用的是 hibernate > 3.5,则它使用 slf4j api,默认情况下使用 logback 而不是 log4j。您可以通过从类路径中删除 logback jar(s) 并添加 slf4j-log4j12.jar 和 log4j.jar 来轻松切换到 log4j。
The Log4j tracing also prints the queries using '?', but it also prints the parameter bindings, i.e. what the '?' will be replaced with by the database driver or server.
Log4j 跟踪也使用“?”打印查询,但它也打印参数绑定,即什么是“?” 将被数据库驱动程序或服务器替换。
回答by Vadzim
In most cases it should be sufficient to include log4j.properties
in application's classpath.
在大多数情况下,包含log4j.properties
在应用程序的类路径中就足够了。
See How to initialize log4j properly?.
请参阅如何正确初始化 log4j?.
The link in question already suggeststhat you should look for bound sql param values not in sql text by in separate log statements like this:
TRACE [BasicBinder] binding parameter [1] as [VARCHAR] - john doe
.
有问题的链接已经表明,你应该在这样单独的日志报表查找绑定SQL PARAM值不在SQL文本方式:
TRACE [BasicBinder] binding parameter [1] as [VARCHAR] - john doe
。
But I recommend to stick with log4jdbc. It's pretty simple to use. And it's able to inline query param values in printed sql text.
但我建议坚持使用log4jdbc。使用起来非常简单。它能够在打印的 sql 文本中内联查询参数值。
回答by Rahul Jain
No need to add in hibernate.cfg.xml, just place it to classpath and follow the steps mentioned in this link. refer this link :- http://makecodeeasy.blogspot.in/2013/03/logger-in-spring-web-application.html
无需添加 hibernate.cfg.xml,只需将其放入类路径并按照此链接中提到的步骤操作即可。请参阅此链接:- http://makecodeeasy.blogspot.in/2013/03/logger-in-spring-web-application.html
回答by jdev
Add this also
也添加这个
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
your problem will be fixed.
您的问题将得到解决。
回答by Coffee Monkey
You can't print the values inline. Hibernate will always print the ? when logging the queries. The output of org.hibernate.type logging at the DEBUG level shows the values and types that are used to replace those question marks.
您无法内联打印值。Hibernate 将始终打印 ? 记录查询时。DEBUG 级别的 org.hibernate.type 日志输出显示了用于替换这些问号的值和类型。
回答by Frank M.
i like sql logging with proxy of jdbcdslog much more than hibernate logging.
我更喜欢使用 jdbcdslog 代理的 sql 日志记录,而不是休眠日志记录。
回答by bsingh
Please refer hibernate reference as per your version. Here is 3.3 Link
请根据您的版本参考休眠参考。这是 3.3链接
Edit :-
编辑 :-
- In order to setup logging you will need slf4j-api.jar in your classpath together with the jar file for your preferred binding - slf4j-log4j12.jar in the case of Log4J.
- Put log4j.properties file in your classpath. An example properties file is distributed with Hibernate in the src/ directory.
Enable following log 4j categories.
org.hibernate.SQL Log all SQL DML statements as they are executed org.hibernate.type Log all JDBC parameters
- 为了设置日志记录,您需要在类路径中使用 slf4j-api.jar 以及首选绑定的 jar 文件 - 在 Log4J 的情况下为 slf4j-log4j12.jar。
- 将 log4j.properties 文件放在您的类路径中。在 src/ 目录中随 Hibernate 分发了一个示例属性文件。
启用以下日志 4j 类别。
org.hibernate.SQL Log all SQL DML statements as they are executed org.hibernate.type Log all JDBC parameters
回答by JavaDeveloper
In my case it wasn't hibernate.cfg.xml, it was log4j.xmlfile where I put the code given by @jdev
在我的情况下,它不是 hibernate.cfg.xml,而是log4j.xml文件,我将@jdev 给出的代码放在这里
< property name="show_sql">trueShows SQL queries
<property name="show_sql"> true显示 SQL 查询
< property name="format_sql">trueFormat the query shown on console
<property name="format_sql"> true格式化控制台上显示的查询
< property name="use_sql_comments">truecomments will be added to the queries
<property name="use_sql_comments">真正的评论将被添加到查询中
If your project is having 1000s of queries printing to the console, make others false: < property name="show_sql">true< property name="format_sql">false< property name="use_sql_comments">false
如果您的项目有 1000 条查询打印到控制台,请将其他查询设为 false:<property name="show_sql"> true<property name="format_sql"> false<property name="use_sql_comments"> false
OR write an appenderto the log4j.xml which I have done in my project, just to write all SQL queries on separate text file which is specified here: Configuring Hibernate logging using Log4j XML config file?
或者将appender写入我在项目中完成的 log4j.xml,只是为了在此处指定的单独文本文件上编写所有 SQL 查询: 使用 Log4j XML 配置文件配置 Hibernate 日志记录?