{"id":4388,"date":"2011-10-21T14:32:22","date_gmt":"2011-10-21T09:02:22","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4388"},"modified":"2016-12-19T15:06:18","modified_gmt":"2016-12-19T09:36:18","slug":"log-sql-in-grails-for-a-piece-of-code","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/log-sql-in-grails-for-a-piece-of-code\/","title":{"rendered":"Log Sql in grails for a piece of code"},"content":{"rendered":"<p>There are time when we need to see the sql logging statement just for a method of for a particular code. Although we already have logSql property in DataSource to do it for us but it sometimes makes difficult if we need to see the log for a small piece of code rather than for whole project.<br \/>\nSo I need something that will execute my code withing a block which automatically starts sql logging and switch it off when code is finished. Groovy closures are the solution for this problem. For doing it I created a class LogSql which have a static execute method that takes the closure as parameter.<br \/>\n[java]<br \/>\nimport org.apache.log4j.Level<br \/>\nimport org.apache.log4j.Logger<\/p>\n<p>public static def execute(Closure closure) {<br \/>\n        Logger sqlLogger = Logger.getLogger(&quot;org.hibernate.SQL&quot;);<br \/>\n        Level currentLevel = sqlLogger.level<br \/>\n        sqlLogger.setLevel(Level.TRACE)<br \/>\n        def result = closure.call()<br \/>\n        sqlLogger.setLevel(currentLevel)<br \/>\n        result<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Now when I want to see the logs I do something like following.<br \/>\n[java]<br \/>\nString name = &quot;Uday&quot;<br \/>\nPerson person<br \/>\nLogSql.execute {<br \/>\n       person = Person.findByName(name)<br \/>\n}<br \/>\n[\/java]<br \/>\nThis prints the sql statement for all the sql fired in the given piece of code block.<br \/>\n<\/p>\n<p>Hope it helps<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/uday\/\">Uday Pratap Singh<\/a><br \/>\n<a href=\"mailto:uday@intelligrape.com\">uday@intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are time when we need to see the sql logging statement just for a method of for a particular code. Although we already have logSql property in DataSource to do it for us but it sometimes makes difficult if we need to see the log for a small piece of code rather than for [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[648,645,646],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4388"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4388"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4388\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}