Skip to content

Instantly share code, notes, and snippets.

@joasgarcia
Last active March 30, 2016 02:13
Show Gist options
  • Save joasgarcia/80156a48b77cf9109daa to your computer and use it in GitHub Desktop.
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/)
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)
}
@meudaypratap
Copy link

What's the exception

@joasgarcia
Copy link
Author

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