Last active
March 30, 2016 02:13
-
-
Save joasgarcia/80156a48b77cf9109daa to your computer and use it in GitHub Desktop.
Logging SQL em Grails para um trecho de código em específico (fonte: http://www.tothenew.com/blog/log-sql-in-grails-for-a-piece-of-code/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.log4j.Level | |
import org.apache.log4j.Logger | |
public static def logSQL(Closure closure) { | |
Logger sqlLogger = Logger.getLogger("org.hibernate.SQL") | |
Level currentLevel = sqlLogger.level | |
sqlLogger.setLevel(Level.TRACE) | |
def result | |
try { | |
result = closure.call() | |
} catch (Exception e) { | |
e.printStackTrace() | |
} finally { | |
sqlLogger.setLevel(currentLevel) | |
result | |
} | |
} | |
String name = "Uday" | |
Person person | |
logSQL { | |
person = Person.findByName(name) | |
} |
Usually the code executed inside closure is still in development and it may throws exceptions because of coding errors. For example when you test a complex criteria query that contains several logics inside. Without surround with try catch when it thrown an exception the application keeps TRACE
lavel and its necessary restart the application to back to normal level.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the exception