Удобочитаемость. Это позволит другим (или Вы позднее) определять то, что Вы пытаетесь выполнить. Если Вы позже найдете, что действительно необходимо волноваться о производительности, удобочитаемость поможет Вам достигнуть производительности.
я также думаю, что путем концентрации на удобочитаемости, Вы на самом деле закончите с более простым кодом, который, скорее всего, достигнет лучшей производительности, чем более сложный код.
You'd add it just like a regular Java class:
Logger log = Logger.getLogger(getClass()) // log4j
or
Log log = LogFactory.getLog(getClass()) // commons logging
A more Groovy way to do the same thing is:
private static log = LogFactory.getLog(this)
This takes advantage of the fact that this
in a static context refers to the Class
object, so the line above can be copy-pasted from one class to another without modification.
Use the @Log4j annotation introduced in Groovy 1.8. This will add a log
property
import groovy.util.logging.Log4j
@Log4j
class MyClass {
def doIt() {
log.info 'hello'
}
}
A nice benefit of using the annotation is that the logger automatically converts calls such as this:
log.info 'hello'
to:
if (log.isInfoEnabled() {
log.info 'hello'
}
Используя стандартный Log4j, новый регистратор (getClass ()) работал, но для меня зарегистрировал 'java.lang.Class' (используя Grails 1.2.1) . Эта проблема исчезла с плагином Sublog и @WithLog. См. http://www.grails.org/plugin/sublog . Этот плагин также не превращает Trace в Debug и Fatal Error ...!