休眠查询日志:需要当前时间戳 [英] Hibernate query logging : need current timestamp

查看:88
本文介绍了休眠查询日志:需要当前时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在休眠配置中,我提到了show_sql = true.

In hibernate configuration I have mentioned show_sql=true.

在日志中,我看到查询正在打印,它是由休眠模式执行的.

In the logs I can see that queries are printing which is executed by hibernate.

像下面的

Hibernate : <insert query> Hibernate : <select query>

Hibernate : <insert query> Hibernate : <select query>

我还希望打印当前时间戳以及查询.像

I want to print the current timestamp as well along with query. Like

<current time > Hibernate : <insert query> <current time > Hibernate : <select query>

<current time > Hibernate : <insert query> <current time > Hibernate : <select query>

我正在使用Hibernate 4. 我已经使用过hibernate.generate_statistics=true,但是这没有时间插入.而且,我只需要当前时间.

I am using Hibernate 4. I have used hibernate.generate_statistics=true but this does not give time for insertion. Moreover, I just need current time.

推荐答案

show_sql=true选项仅设置对系统控制台的写入,它不是真正的记录器,因此无法对其进行配置.如果要从Hibernate获取有意义的日志记录,则应配置一个日志记录框架,并使用它来控制Hibernate的日志记录.

The show_sql=true option only sets up writing to the system console, it is not a true logger, so you cannot configure it. If you want to get meaningful logging from Hibernate you should configure a logging framework, and use it to control the logging of Hibernate.

我已经使用了以下日志记录框架,所以我可以告诉你,它们与Hibernate一起运行良好: log4j slf4j logback log4j2 .

I have used the following logging frameworks already, so I can tell you, that these are working well with Hibernate: log4j, slf4j, logback, log4j2.

例如,在log4j.properties文件中的这一行将 log4j 配置为保留相同的信息,该信息由show_sql=true发出:

For example, this line in a log4j.properties file configures log4j to keep the same info, which is put out with show_sql=true:

log4j.category.org.hibernate.SQL=DEBUG

但是在这种情况下,您可以配置输出.就我而言,格式是在console附加程序中定义的:

But in this case you can configure the output. In my case, the format is defined in the console appender:

log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %F:%L - %m%n

这样,输出为:

<current time> DEBUG SqlStatementLogger.java:104 - <insert query>


完整的log4j.properties文件

为了完整起见,我包含了我的配置文件:


A complete log4j.properties file

For the sake of completeness, I have included my configuration file:

# root logger
 log4j.rootLogger = INFO, stdout

# category filters
log4j.category.org.hibernate.SQL=TRACE

# appenders
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %F:%L - %m%n

这篇关于休眠查询日志:需要当前时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆