首页 > 其他分享 >搭建Hibernate日志-log4jdbc

搭建Hibernate日志-log4jdbc

时间:2023-06-04 14:34:57浏览次数:37  
标签:Hibernate color log4j connection jdbc org log4jdbc 日志 appender


官方: [url]https://code.google.com/p/log4jdbc/[/url]
log4jdbc分析sql性能 [url]http://hongliangpan.iteye.com/blog/1088398[/url]
xml配置方法: log4jdbc日志框架介绍 [url]http://blog.sina.com.cn/s/blog_57769b7b0101m1il.html[/url]

[url]http://xiaominghimi.blog.51cto.com/2614927/969781[/url]

[color=red]log4j不能使用1.2.15版本,否则丢失包,浪费时间[/color]

<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>org.lazyluke</groupId>
			<artifactId>log4jdbc-remix</artifactId>
			<version>0.2.7</version>
		</dependency>



启动程序改为:


[color=blue]jdbc.driver=net.sf.log4jdbc.DriverSpy


jdbc.url=jdbc:log4jdbc:sqlserver//localhost:1433;DatabaseName=Test[/color]


在jdbc和sqlserver之间加上log4jdbc,就变成了[color=red][b]jdbc:log4jdbc:sqlserver[/b][/color]


比如mysql就应该写成: [color=red][b]jdbc:log4jdbc:mysql[/b][/color]://localhost:3306/jbpmtest


[color=darkblue]Easy to configure, in most cases all you need to do is change the driver class name to [b]net.sf.log4jdbc.DriverSpy[/b] and prepend "[b]jdbc:log4[/b]" to your existing jdbc url, set up your logging categories and you're ready to go![/color]



[color=red][b]log4j.properties[/b][/color]



log4j.rootLogger=warn, stdout


### direct log messages to stdout ###


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{ABSOLUTE} %5p %c{1}:%L - %m%n



# log4jdbc


log4j.logger.jdbc.sqlonly=INFO


log4j.logger.jdbc.sqltiming=INFO


log4j.logger.jdbc.audit=OFF


log4j.logger.jdbc.resultset=OFF


log4j.logger.jdbc.connection=OFF




两种配置参考


[color=red][b]log4j.xml[/b][/color]


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- An example log4j configuration xml file for log4jdbc -->
<!-- Logging levels are:                                  -->
<!-- DEBUG < INFO < WARN < ERROR < FATAL                  -->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="stdout-appender" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1}: %m%n"/>
    </layout>
  </appender>
  <appender name="sql-appender" class="org.apache.log4j.FileAppender">
    <param name="File" value="./logs/sql.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="-----> %d{yyyy-MM-dd HH:mm:ss.SSS} <%t> %m%n%n"/>
    </layout>
  </appender>
  <appender name="sql-timing-appender" class="org.apache.log4j.FileAppender">
    <param name="File" value="./logs/sqltiming.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n"/>
    </layout>
  </appender>
  <appender name="jdbc-appender" class="org.apache.log4j.FileAppender">
    <param name="File" value="./logs/jdbc.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n"/>
    </layout>
  </appender>
  <appender name="jdbc-connection" class="org.apache.log4j.FileAppender">
    <param name="File" value="./logs/connection.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n"/>
    </layout>
  </appender>
  <!--
       The Following 5 logs can be turned on and off while the server is running
       LIVE in order to trace the SQL and/or all JDBC coming out of the application.
       To turn a log on, set the level value to INFO or DEBUG (to see class name and
       line number information in the log)  The DEBUG setting is much more inefficient
       but the output is much more useful.
       To turn off JDBC logging completely, you must set all 5 logs to a level higher
       than ERROR (FATAL is suggested.)
  -->
  <!-- log SQL (pre-execution) plus exceptions caused by SQL -->
  <logger name="jdbc.sqlonly" additivity="false">
    <level value="debug"/>
    <appender-ref ref="sql-appender"/>
  </logger>
  <!-- log SQL with timing information, post execution -->
  <logger name="jdbc.sqltiming" additivity="false">
    <level value="fatal"/>
    <appender-ref ref="sql-timing-appender"/>
  </logger>
  <!-- only use the two logs below to trace ALL JDBC information,
       NOTE:  This can be very voluminous!  -->
  <!-- log all jdbc calls except ResultSet calls -->
  <logger name="jdbc.audit" additivity="false">
    <level value="fatal"/>
    <appender-ref ref="jdbc-appender"/>
  </logger>
  <!-- log the jdbc ResultSet calls -->
  <logger name="jdbc.resultset" additivity="false">
    <level value="fatal"/>
    <appender-ref ref="jdbc-appender"/>
  </logger>

  <!-- log connection open/close events and dump of all open connection numbers -->
  <logger name="jdbc.connection" additivity="false">
    <level value="fatal"/>
    <appender-ref ref="connection-appender"/>
  </logger>
  <!-- this log is for internal debugging of log4jdbc, itself -->
  <!-- debug logging for log4jdbc itself -->
  <logger name="log4jdbc.debug" additivity="false">
    <level value="debug"/>
    <appender-ref ref="stdout-appender"/>
  </logger>
  <!-- by default, log everything to the console with a level of WARN or higher -->
  <root>
    <level value="warn"/>
    <appender-ref ref="stdout-appender"/>
  </root>
</log4j:configuration>



[color=red][b]log4j.properties[/b][/color]


[color=darkblue]!==============================================================================


! log4j.properties - An example configuration properties file for log4j.


!


! Logging levels are:


! DEBUG < INFO < WARN < ERROR < FATAL


!==============================================================================


! turn on the internal log4j debugging flag so we can see what it is doing


log4j.debug=true


!==============================================================================


! JDBC API layer call logging :


! INFO shows logging, DEBUG also shows where in code the jdbc calls were made,


! setting DEBUG to true might cause minor slow-down in some environments.


! If you experience too much slowness, use INFO instead.


! Log all JDBC calls except for ResultSet calls


log4j.logger.jdbc.audit=INFO,jdbc


log4j.additivity.jdbc.audit=false


! Log only JDBC calls to ResultSet objects


log4j.logger.jdbc.resultset=INFO,jdbc


log4j.additivity.jdbc.resultset=false


! Log only the SQL that is executed.


log4j.logger.jdbc.sqlonly=DEBUG,sql


log4j.additivity.jdbc.sqlonly=false


! Log timing information about the SQL that is executed.


log4j.logger.jdbc.sqltiming=DEBUG,sqltiming


log4j.additivity.jdbc.sqltiming=false


! Log connection open/close events and connection number dump


log4j.logger.jdbc.connection=FATAL,connection


log4j.additivity.jdbc.connection=false



! the appender used for the JDBC API layer call logging above, sql only


log4j.appender.sql=org.apache.log4j.FileAppender


log4j.appender.sql.File=./logs/sql.log


log4j.appender.sql.Append=false


log4j.appender.sql.layout=org.apache.log4j.PatternLayout


log4j.appender.sql.layout.ConversionPattern=-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n


! the appender used for the JDBC API layer call logging above, sql timing


log4j.appender.sqltiming=org.apache.log4j.FileAppender


log4j.appender.sqltiming.File=./logs/sqltiming.log


log4j.appender.sqltiming.Append=false


log4j.appender.sqltiming.layout=org.apache.log4j.PatternLayout


log4j.appender.sqltiming.layout.ConversionPattern=-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n


! the appender used for the JDBC API layer call logging above


log4j.appender.jdbc=org.apache.log4j.FileAppender


log4j.appender.jdbc.File=./logs/jdbc.log


log4j.appender.jdbc.Append=false


log4j.appender.jdbc.layout=org.apache.log4j.PatternLayout


log4j.appender.jdbc.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n


! the appender used for the JDBC Connection open and close events


log4j.appender.connection=org.apache.log4j.FileAppender


log4j.appender.connection.File=./logs/connection.log


log4j.appender.connection.Append=false


log4j.appender.connection.layout=org.apache.log4j.PatternLayout


log4j.appender.connection.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n[/color]



标签:Hibernate,color,log4j,connection,jdbc,org,log4jdbc,日志,appender
From: https://blog.51cto.com/u_3871599/6410598

相关文章

  • Hibernate和 OsCache的使用
    下载地址:[url]http://java.net/downloads/oscache/[/url]oscache这个jar里面的jms架包已经无法下载了。那么我就在网上自己下载了一个jms.jar安装到本地的仓库中去,就ok了。进入安装maven的目录bin中,执行如下命令:[i][color=blue]mvninstall:install-file......
  • hibernate中自定义主键生成器
    自定义hibernate主键生成机制[url]http://walle1027.iteye.com/blog/1114824[/url]org.hibernate.id.MultipleHiLoPerTableGenerator主键生成器[url]http://suzefeng8806.iteye.com/blog/923511[/url][url]http://zhongrf.iteye.com/blog/972303[/url]......
  • hibernate------hql总结
    第14章HQL:Hibernate查询语言[url]http://www.redsaga.com/hibernate-ref/3.x/zh-cn/html/queryhql.html[/url][url]http://kuangbaoxu.iteye.com/blog/193076[/url][color=red][b]1.查询整个映射对象所有字段[/b][/color]//直接from查询出来的是一......
  • Hibernate性能调优,优化
    Hibernate优化_Hibernate性能优化_Hibernate优化方案(上):[url]http://xiexiejiao.cn/hibernate/hibernate-performance-optimization-a.html[/url]Hibernate优化_Hibernate性能优化_Hibernate优化方案(下):[url]http://xiexiejiao.cn/hibernate/hibernate-performance-optimizati......
  • Hibernate Tools生成Hibernate Mapping文件及PO类
    [color=red][b]本文参考[/b][/color]:HowToGenerateHibernateMappingFiles&AnnotationWithHibernateTools[url]http://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/[/url]eclipse利用HibernateTools生成HibernateMap......
  • Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring) part4 (完)
     Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring)part4----addcairngorm1.添加在libs下添加Cairngorm.swc,此时,具备了cairngorm框架能力2.运用cairngorm框架2.1在flex_src下创建如下文件夹和文件   AdminVO.aspackagevo.AdminVO{ [Bindable] publicclassAdm......
  • Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring) part3
    Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring)part3                                   ----addspring &hibernate1.在WEB-INF\lib下添加spring、hibernate常用jar,包括mysql数据库的连接驱动  2.在mysql数据库中创建数据库......
  • MySQL 8错误日志出现"The table /home/work/mysql_3306/tmp/#sqla2b_298b06_4d is fu
    ##############    了解MySQL8.0.26的错误日志出现"Thetable /home/work/mysql_3306/tmp/#sqla2b_298b06_4disfu11!"的bug,暂时通过修改临时表的存储引擎为内存引擎解决  MySQL8.0.13开始引入新的临时内存表引擎TempTable,并将其作为内存中创建临时表的默认存储引擎。T......
  • 【Python】如何在FastAPI中使用UUID标记日志,以跟踪一个请求的完整生命周期
    为什么要使用uuid标记日志?在分布式系统中,一个请求可能会经过多个服务,每个服务都会生成自己的日志。如果我们只使用普通的日志记录,那么很难将这些日志串联在一起,以至难以跟踪一个请求的完整生命周期。如果能够使用uuid标记日志,为每个请求生成一个唯一的uuid,且这个日志可以在不同......
  • 【云原生•监控】mtail轻量级日志监控
    【云原生•监控】mtail轻量级日志监控前言「笔者已经在公有云上搭建了一套临时环境,可以先登录体验下:」http://124.222.45.207:17000/login账号:root/root.2020简介「可观测性平台三大支柱:日志监控、调用链监控和度量指标监控,其中最为大家熟知的是日志监控,因为我们开发系统基本都离......