Posted In: Apache Log4J, Hibernate, Logback, Logging, SLF4J

Hibernate 5 – print a query string with parameter values

Logging frameworks used

1. Using LOG4J slf4j-log4j12

2. Using LOGBACK

3. Using LOG4J 2X

 

1. Using LOG4J slf4j-log4j12

Maven slf4j-log4j12

<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>1.7.12</version>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-log4j12</artifactId>
	<version>1.7.12</version>
</dependency>

log4j.properties

Will print both BasicTypeRegistry and BasicBinder (parameter values) logs.


log4j.logger.org.hibernate.type=ALL

Will print only BasicBinder (parameter values) logs.


log4j.logger.org.hibernate.type.descriptor.sql=trace

Parameter values logs


12:52:11,376 TRACE BasicBinder:65 - binding parameter [1] as [VARCHAR] - [Abhijit]
12:52:11,376 TRACE BasicBinder:65 - binding parameter [2] as [VARCHAR] - [Pednekar]
12:52:11,377 TRACE BasicBinder:53 - binding parameter [3] as [VARCHAR] - [null]

 

2. Using LOGBACK

Maven logback-core

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.2.3</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

logback.xml

Will print both BasicTypeRegistry and BasicBinder (parameter values) logs.

<logger name="org.hibernate" level="INFO" />

Will print only BasicBinder (parameter values) logs.

<logger name="org.hibernate" level="INFO" />
<logger name="org.hibernate.type.BasicTypeRegistry" level="INFO" />
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" />

Parameter values logs


12:52:11,376 TRACE BasicBinder:65 - binding parameter [1] as [VARCHAR] - [Abhijit]
12:52:11,376 TRACE BasicBinder:65 - binding parameter [2] as [VARCHAR] - [Pednekar]
12:52:11,377 TRACE BasicBinder:53 - binding parameter [3] as [VARCHAR] - [null]

 

3. Using LOG4J 2X

Maven log4j-core 2X

<dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-api</artifactId>
   <version>2.9.0</version>
</dependency>
<dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-core</artifactId>
   <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.9.0</version>
</dependency>

log4j2.xml

Will print both BasicTypeRegistry and BasicBinder (parameter values) logs.

<Loggers>
	<Logger name="org.hibernate" level="ALL" />
	<Root level="DEBUG">
		<AppenderRef ref="Console" />
		<AppenderRef ref="MyFile" />
	</Root>
</Loggers>

Will print only BasicBinder (parameter values) logs.

<Loggers>
	<Logger name="org.hibernate" level="INFO" />
	<Logger name="org.hibernate.type.BasicTypeRegistry" level="INFO" />
	<Logger name="org.hibernate.type.descriptor.sql.BasicBinder"
		level="TRACE" />
	<Root level="DEBUG">
		<AppenderRef ref="Console" />
		<AppenderRef ref="MyFile" />
	</Root>
</Loggers>

Parameter values logs


12:52:11,376 TRACE BasicBinder:65 - binding parameter [1] as [VARCHAR] - [Abhijit]
12:52:11,376 TRACE BasicBinder:65 - binding parameter [2] as [VARCHAR] - [Pednekar]
12:52:11,377 TRACE BasicBinder:53 - binding parameter [3] as [VARCHAR] - [null]

by , on September 4th, 2017

  • Categories