Introduction
In this post, we will learn how to print Hibernate Query parameters using log4j.Requirments
- log4j-api-2.13.1.jar
- log4j-core-2.13.1.jar
Step 1
Place the above two log4j jars inside the lib directory. For web applications, the lib directory is located inside the WEB-INF folder.
Step 2
Create a file by the name of log4j2.xml and place it inside your src folder.
Step 3
Place the following XML code inside the log4j2.xml file
<configuration status="WARN">
<appenders>
<console name="LogToConsole" target="SYSTEM_OUT">
<patternlayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n">
</patternlayout></console>
</appenders>
<loggers>
<logger additivity="false" level="debug" name="org.myapp">
<appenderref ref="LogToConsole">
</appenderref></logger>
<logger level="debug" name="org.hibernate.SQL">
<logger level="trace" name="org.hibernate.type.descriptor.sql.BasicBinder">
<root level="error">
<appenderref ref="LogToConsole">
</appenderref></root>
</logger></logger></loggers>
</configuration>
Output
Hibernate: select * from PERSON person0_ where person0_.ID=? and lower(person0_.TYPE)<>? order by person0_.id desc
14:05:08.308 [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [82]
14:05:08.308 [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [4] as [VARCHAR] - [r]
Conclusion
In this post, we learned how to print Hibernate Query parameters using log4j2. Happy learning :)
Comments
Post a Comment