When you use ORM for developing an application, SQLs are generated and executed by the underlying ORM engine (i.e Hibernate for ColdFusion ORM). However, for both troubleshooting and performance optimization, it is crucial to monitor what queries are getting generated. It can help you find out if there is any error in mapping that you have provided as well as it can help you decide various tuning strategies.
ColdFusion can log the SQLs generated by ORM either onto the console or a file. At the same time it leaves enough hook for you to log it anywhere you want.
ColdFusion ORM provides two ways to log the SQLs.
- Using application setting to log to console : This is a quick and simple way to log the sql to console. This is enabled by setting “logsql” in ormsettings.
<cfset this.ormsettings.logsql="true">This setting is self sufficient and it will log all the sqls executed by hibernate to the console (or a file where the server output goes). However this is not a very flexible option. The sqls are always written to the console and it will be combined with any other output that goes to console. Also this option will not show the DDL queries used for creating or updating tables. It only logs the SQL for the entity operations.
- Using log4J.properties: Hibernate uses log4j for its logging and you can completely control its logging (including SQL) by modifying the log4j.properties. log4j.properties is present under <cf_home>/lib directory. Please note that you don’t need to do any application specific setting for this.
I will go in details about using log4j.properties for SQL logging. Here is a snippet from log4j.properties file that is shipped with ColdFusion.
###--------------- Hibernate Log Settings ------ ### Set Hibernate log log4j.logger.org.hibernate=ERROR, HIBERNATECONSOLE ### log just the SQL #log4j.logger.org.hibernate.SQL=DEBUG, HIBERNATECONSOLE #log4j.additivity.org.hibernate.SQL=false ### Also log the parameter binding to the prepared statements. #log4j.logger.org.hibernate.type=DEBUG ### log schema export/update ### log4j.logger.org.hibernate.tool.hbm2ddl=DEBUG, HIBERNATECONSOLE ### log cache activity ### log4j.logger.org.hibernate.cache=ERROR, HIBERNATECONSOLE # HibernateConsole is set to be a ColsoleAppender for Hibernate message using a PatternLayout. log4j.appender.HIBERNATECONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.HIBERNATECONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.HIBERNATECONSOLE.layout.ConversionPattern=%d{MM/dd HH:mm:ss} [%t] HIBERNATE %-5p - %m%n%n #---------------------------------------------
First we will see the relevant settings for SQL logging:
- log4j.logger.org.hibernate.SQL : Defines whether the SQL executed for entity operations will be logged and where it will be logged. The second value for this i.e ‘HIBERNATECONSOLE’ is a appender that controls where the SQLs will be logged. In the above example HIBERNATECONSOLE is a ‘console’ appender which means it will log the sql to console.
- log4j.logger.org.hibernate.type : Defines whether parameter values used for parametrized query will be logged.
- log4j.logger.org.hibernate.tool.hbm2ddl : Defines whether DDL sql statements will be logged.
To enable the SQL logging for console, we just need to uncomment the settings mentioned above. Here is how the hibernate log settings in log4j.properties file would look like
###--------------- Hibernate Log Settings ------ ### Set Hibernate log log4j.logger.org.hibernate=ERROR, HIBERNATECONSOLE ### log just the SQL log4j.logger.org.hibernate.SQL=DEBUG, HIBERNATECONSOLE log4j.additivity.org.hibernate.SQL=false ### Also log the parameter binding to the prepared statements. #log4j.logger.org.hibernate.type=DEBUG ### log schema export/update ### log4j.logger.org.hibernate.tool.hbm2ddl=DEBUG, HIBERNATECONSOLE ### log cache activity ### log4j.logger.org.hibernate.cache=ERROR, HIBERNATECONSOLE # HibernateConsole is set to be a ColsoleAppender for Hibernate message using a PatternLayout. log4j.appender.HIBERNATECONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.HIBERNATECONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.HIBERNATECONSOLE.layout.ConversionPattern=%d{MM/dd HH:mm:ss} [%t] HIBERNATE %-5p - %m%n%n #---------------------------------------------
Here is the complete log4j.properties for logging SQL for console. Ofcourse after changing this you need to restart the server. If you need to log the parameter values used for queries, you need to uncomment ‘#log4j.logger.org.hibernate.type=DEBUG’ as well.
What if you want to log the SQL to a file and not to console? That is pretty easy. You just need to change the ‘Appender’ used here (HIBERNATECONSOLE) to point to a ‘FileAppender’ instead of a ConsoleAppender. Here is how the configuration for HIBERNATECONSOLE should look like after you point it to a File Appender.
log4j.appender.HIBERNATECONSOLE=org.apache.log4j.FileAppender log4j.appender.HIBERNATECONSOLE.File=../hibernatesql.log log4j.appender.HIBERNATECONSOLE.Append=true log4j.appender.HIBERNATECONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.HIBERNATECONSOLE.layout.ConversionPattern=%d{MM/dd HH:mm:ss} [%t] HIBERNATE %-5p - %m%n%n
For standalone ColdFusion installation, the file ‘hibernatesql.log’ will be created in the
That was easy. Isn’t it? What if you want a rolling log file where you dont want the log file size to grow infinitely. That is fairly easy too. All you need to do is to use an appropriate appender. The appender definition for that will look like
log4j.appender.HIBERNATECONSOLE=org.apache.log4j.RollingFileAppender log4j.appender.HIBERNATECONSOLE.File=../hibernatesql.log log4j.appender.HIBERNATECONSOLE.Append=true log4j.appender.HIBERNATECONSOLE.MaxFileSize=500KB log4j.appender.HIBERNATECONSOLE.MaxBackupIndex=3 log4j.appender.HIBERNATECONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.HIBERNATECONSOLE.layout.ConversionPattern=%d{MM/dd HH:mm:ss} [%t] HIBERNATE %-5p - %m%n%n
Here is the complete log4j.properties for logging SQL to a file that will be rolled automatically once it reaches 500KB.
Now that you have seen how easy it is to change one ‘Appender’ to another, you can pretty much log it anywhich way you want. Here are some of the interesting ‘Appender’s that come with log4j which you can easily use.
DailyRollingFileAppender, ExternallyRolledFileAppender, JMSAppender, SMTPAppender, SocketAppender, SyslogAppender
See log4J for more details on log4j settings.

#1 by Saravanamuthu on August 17th, 2009
| Quote
Nice post. broken link – “complete log4j.properties”
#2 by Rupesh Kumar on August 18th, 2009
| Quote
Thats so weird. Looks like something has changed on this server that prevents *.properties file from being served. I have changed the extension of this file as ‘.txt’. Please change it back to ‘.properties’ before using.
Thanks a lot Sarvan for pointing this!
#3 by Rob Sherman on February 5th, 2010
| Quote
@Rupesh, I set up my log4j.properties but I’m not seeing any of the log files being written. I’m using the CF9 sample ORM Example app. Am I missing something? Does this log all hibernate SQL or just SQL that is run through HQL?
#4 by Henry Ho on March 12th, 2010
| Quote
Is it possible to show what’re the values behind the “?”s in out log when this.ormsettings.logSQL = true?
#5 by Henry Ho on March 15th, 2010
| Quote
og4j.logger.org.hibernate.type=DEBUG
found it, i should read more carefully… thx.
#6 by Dan Vega on August 4th, 2010
| Quote
Rupesh – I have done this before so Its really confusing to me why this is not working. I uncomment everything and I just want to log to the console. I start the server from ColdFusion Builder and I see the server startup in the console. I set logsql = true in my orm settings and run my app. I see the following logged to my console but no sql. What am I missing?
[localhost]:SLF4J: This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version
#7 by Rupesh Kumar on August 5th, 2010
| Quote
I am not sure why that would happen. can you send me the log4j.properties file so that I can take a look?
Btw, setting this.ormsettings.logsql to true will always log the sql to console. It will not use the log4j framework to do that which means that sql will be logged to the console irrespective of the settings in log4j.properties.
Are you sure that your app is running fine?
#8 by muji on August 8th, 2010
| Quote
Dan – I get that same error logged upon initializing Hibernate as SLF4J is the underlying logger in it. However logging works perfectly so I haven’t worried about updating SLF4J.
I don’t think it’s related to your issues as Rupesh hinted. Have you looked directly at the cfserver.log to assure no Hibernate messages appear once you put logsql=true? Also, did you reset Hibernate via ORMReload() or a CF restart after the adjustment?
#9 by prims on November 19th, 2010
| Quote
Hi, Is is it possible to do same using ColdFusion 8. If so how and what is the procedure. Thanks
#10 by Adam on August 11th, 2011
| Quote
“You just need to change the ‘Appender’ used here (HIBERNATECONSOLE) to point to a ‘FileAppender’ instead of a ConsoleAppender.” – I try to log SQL to a file but it gives me errors, I have tried to do the given steps.
#11 by Christopher Jazinski on August 26th, 2011
| Quote
Thank you for this.
#12 by Naushad Malik on September 21st, 2011
| Quote
/lib directory
I dont understand the exact path to keep the config file. Running eclipse
C:\Users\blabla\Desktop\CFEclipse\plugins\org.cfeclipse.cfml_1.4.3.201010230020.
Is it the correct working folder.
Pingback: Console Logging of ColdFusion ORM SQL « Chris Tierney
#13 by Josh on July 24th, 2012
| Quote
Might be helpful, using the settings described here, my logfile turned up in c:\Coldfusion9\runtime\
#14 by Upendra on September 22nd, 2012
| Quote
Hi Rupesh,
Your Post is very informative.
I have a question:
Can we log the SQL Queries in database instead of any log file?
#15 by click here on May 20th, 2013
| Quote
The dinners at these holidays parties were often the most charming.
Normally, this is important.