如何处理hibernate打印一些无关的日志信息 如何关闭hibernate产生的大量日志
如何处理hibernate打印一些无关的日志信息
步骤
1. 在项目中创建4个包,分别是 log4.debug/error/info/warning
2. 在每个包下创建输出日志的测试代码:
Java代码
log.debug("Debug in Debug Level")
log.info("Info in Debug Level")
log.warn("Warn in Debug Level")
log.error("Error in Debug Level")
3. 在MainClass中调用日志输出:
Java代码
package lv.showcase.log4j
import lombok.extern.log4j.Log4j
import lv.showcase.log4j.debug.DebugLog
import lv.showcase.log4j.error.ErrorLog
import lv.showcase.log4j.info.InfoLog
import lv.showcase.log4j.warning.WarnLog
@Log4j
public class Main {
public static void main(String[] args) {
log.info("========== Log4j Showcase :")
log.debug("You can not see this message in info level.")
DebugLog.log()
InfoLog.log()
WarnLog.log()
ErrorLog.log()
}
}
4. 采用如下log4j配置来控制日志输出:
Java代码
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{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=log4j_showcase.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
log4j.appender.R1=org.apache.log4j.FileAppender
log4j.appender.R1.File=main.log
log4j.appender.R1.layout=org.apache.log4j.PatternLayout
log4j.appender.R1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
log4j.logger.lv.showcase.log4j.debug=debug, stdout, file
log4j.logger.lv.showcase.log4j.error=error, stdout, file
log4j.logger.lv.showcase.log4j.info=info, stdout, file
log4j.logger.lv.showcase.log4j.warning=warn, stdout, file
#Main函数的日志会输出到main.log文件中
log4j.logger.lv.showcase.log4j.Main=info, stdout, R1
上述log4j配置做了这几个事情
定义3中输出方式: stdout(标准输出), file(输出到log4_showcase.log), R1(输出到main.log)
使用log4j.logger.{package}=? 方式控制每个包的输出等级, 输出方式采用stdout, file这两种输出方式
使用log4j.logger.{package}.{className}=? 方式控制单个类的输出等级,输出方式采用stdout, main
输出结果
控制台:
Java代码
[java] 2013-08-06 10:22:01,684 INFO Main:? - ========== Log4j Showcase :
[java] 2013-08-06 10:22:01,688 DEBUG DebugLog:? - Debug in Debug Level
[java] 2013-08-06 10:22:01,689 INFO DebugLog:? - Info in Debug Level
[java] 2013-08-06 10:22:01,689 WARN DebugLog:? - Warn in Debug Level
[java] 2013-08-06 10:22:01,690 ERROR DebugLog:? - Error in Debug Level
[java] 2013-08-06 10:22:01,691 INFO InfoLog:? - Info in Info Level
[java] 2013-08-06 10:22:01,691 WARN InfoLog:? - Warn in Info Level
[java] 2013-08-06 10:22:01,692 ERROR InfoLog:? - Error in Info Level
[java] 2013-08-06 10:22:01,693 WARN WarnLog:? - Warn in Warning Level
[java] 2013-08-06 10:22:01,693 ERROR WarnLog:? - Error in Warning Level
[java] 2013-08-06 10:22:01,694 ERROR ErrorLog:? - Error in Error Level
Main.log 输出:
Java代码
2013-08-06 10:22:01,688 DEBUG DebugLog:? - Debug in Debug Level
2013-08-06 10:22:01,689 INFO DebugLog:? - Info in Debug Level
2013-08-06 10:22:01,689 WARN DebugLog:? - Warn in Debug Level
2013-08-06 10:22:01,690 ERROR DebugLog:? - Error in Debug Level
2013-08-06 10:22:01,691 INFO InfoLog:? - Info in Info Level
2013-08-06 10:22:01,691 WARN InfoLog:? - Warn in Info Level
2013-08-06 10:22:01,692 ERROR InfoLog:? - Error in Info Level
2013-08-06 10:22:01,693 WARN WarnLog:? - Warn in Warning Level
2013-08-06 10:22:01,693 ERROR WarnLog:? - Error in Warning Level
总结
实际应用中可以配置多个appender,按照模块将日志分成不同文件
Log4j的日志输出支持按包/类制定级别, 配置方式(log4j.logger.{package}.{className} )
随着项目的演进需要对日志文件进行划分,否则这样生成的日志会让后期日志分析工作变成噩梦
如何关闭hibernate产生的大量日志
您好,很高兴为您解答。
要关闭hibernate的日志,首先要把hibernate.show_sql设置为false然后设置log4j.properties。
# Control logging for other open source packages
log4j.logger.com.opensymphony.oscache=ERROR
log4j.logger.net.sf.navigator=ERROR
log4j.logger.net.sf.acegisecurity=WARN
log4j.logger.net.sf.acegisecurity.intercept.event.LoggerListener=WARN
log4j.logger.org.apache.commons=ERROR
log4j.logger.org.apache.struts=WARN
log4j.logger.org.displaytag=ERROR
log4j.logger.org.springframework=WARN
log4j.logger.com.ibatis.db=WARN
log4j.logger.org.apache.velocity=WARN
# Dont show debug logs for WebTest
log4j.logger.com.canoo.webtest=WARN
# All hibernate log output of "info" level or higher goes to stdout.
# For more verbose logging, change the "info" to "debug" on the last line.
log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN
log4j.logger.org.hibernate=WARN
# Changing the log level to DEBUG will result in Hibernate generated
# SQL to be logged.
log4j.logger.org.hibernate.SQL=ERROR
# Changing the log level to DEBUG will result in the PreparedStatement
# bound variable values to be logged.
log4j.logger.org.hibernate.type=ERROR
如若满意,请点击右侧【采纳答案】,如若还有问题,请点击【追问】
希望我的回答对您有所帮助,望采纳!
~ O(∩_∩)O~
怎么让Hibernate不打印日志
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> org.hibernate.dialect.OracleDialect true--- 这里改成false
如何将Hibernate生成的SQL语句打印到日志文件
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=e:\xxx.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change info to debug ###
log4j.rootLogger=info, file
日志路径可以自己指定