Skip to content
Permalink
Browse files
add extra @PlatformLog doco
  • Loading branch information
paulk-asert committed Feb 16, 2022
1 parent a0265a6 commit 59b5aaf35e3b63a5e49418a55c9b82e8a4100ae1
Showing 1 changed file with 59 additions and 10 deletions.
@@ -2116,20 +2116,22 @@ Deprecated. Consider using traits instead.
==== Logging improvements
Groovy provides AST transformation that helps integrating with the most widely used logging frameworks. It's worth noting
that annotating a class with one of those annotations doesn't prevent you from adding the appropriate logging framework
on classpath.
Groovy provides a family of AST transformations that help with integration of the most widely
used logging frameworks. There is a transform and associated annotation for each of the common frameworks.
These transforms provide a streamlined declarative approach to using the logging framework.
In each case, the transform will:
All transformations work in a similar way:
* add static final `log` field corresponding to the logger
* add a static final `log` field to the annotated class corresponding to the logger
* wrap all calls to `log.level()` into the appropriate `log.isLevelEnabled` guard, depending on the underlying framework
Those transformations support two parameters:
* `value` (default `log`) corresponds to the name of the logger field
* `category` (defaults to the class name) is the name of the logger category
It's worth noting that annotating a class with one of those annotations doesn't
prevent you from using the logging framework using the normal long-hand approach.
[[xform-Log]]
===== `@groovy.util.logging.Log`
@@ -2150,7 +2152,7 @@ include::../test/LogImprovementsASTTransformsTest.groovy[tags=log_equiv,indent=0
[[xform-Commons]]
===== `@groovy.util.logging.Commons`
Groovy supports the http://commons.apache.org/proper/commons-logging/[Apache Commons Logging] framework using to the
Groovy supports the http://commons.apache.org/proper/commons-logging/[Apache Commons Logging] framework using the
`@Commons` annotation. Writing:
[source,groovy]
@@ -2165,10 +2167,12 @@ is equivalent to writing:
include::../test/LogImprovementsASTTransformsTest.groovy[tags=commons_equiv,indent=0]
----
You still need to add the appropriate commons-logging jar to your classpath.
[[xform-Log4j]]
===== `@groovy.util.logging.Log4j`
Groovy supports the http://logging.apache.org/log4j/1.2/[Apache Log4j 1.x] framework using to the
Groovy supports the http://logging.apache.org/log4j/1.2/[Apache Log4j 1.x] framework using the
`@Log4j` annotation. Writing:
[source,groovy]
@@ -2183,10 +2187,14 @@ is equivalent to writing:
include::../test/LogImprovementsASTTransformsTest.groovy[tags=log4j_equiv,indent=0]
----
You still need to add the appropriate log4j jar to your classpath.
This annotation can also be used with the compatible https://reload4j.qos.ch/[reload4j] log4j
drop-in replacement, just use the jar from that project instead of a log4j jar.
[[xform-Log4j2]]
===== `@groovy.util.logging.Log4j2`
Groovy supports the http://logging.apache.org/log4j/2.x/[Apache Log4j 2.x] framework using to the
Groovy supports the http://logging.apache.org/log4j/2.x/[Apache Log4j 2.x] framework using the
`@Log4j2` annotation. Writing:
[source,groovy]
@@ -2201,10 +2209,12 @@ is equivalent to writing:
include::../test/LogImprovementsASTTransformsTest.groovy[tags=log4j2_equiv,indent=0]
----
You still need to add the appropriate log4j2 jar to your classpath.
[[xform-Slf4j]]
===== `@groovy.util.logging.Slf4j`
Groovy supports the http://www.slf4j.org/[Simple Logging Facade for Java (SLF4J)] framework using to the
Groovy supports the http://www.slf4j.org/[Simple Logging Facade for Java (SLF4J)] framework using the
`@Slf4j` annotation. Writing:
[source,groovy]
@@ -2219,6 +2229,45 @@ is equivalent to writing:
include::../test/LogImprovementsASTTransformsTest.groovy[tags=slf4j_equiv,indent=0]
----
You still need to add the appropriate slf4j jar(s) to your classpath.
[[xform-PlatformLog]]
===== `@groovy.util.logging.PlatformLog`
Groovy supports the https://openjdk.java.net/jeps/264[Java Platform Logging API and Service]
framework using the `@PlatformLog` annotation. Writing:
[source,groovy]
----
@groovy.util.logging.PlatformLog
class Greeter {
void greet() {
log.info 'Called greeter'
println 'Hello, world!'
}
}
----
is equivalent to writing:
[source,groovy]
----
import java.lang.System.Logger
import java.lang.System.LoggerFinder
import static java.lang.System.Logger.Level.INFO
class Greeter {
private static final transient Logger log =
LoggerFinder.loggerFinder.getLogger(Greeter.class.name, Greeter.class.module)
void greet() {
log.log INFO, 'Called greeter'
println 'Hello, world!'
}
}
----
You need to be using JDK 9+ to use this capability.
==== Declarative concurrency
The Groovy language provides a set of annotations aimed at simplifying common concurrency patterns in a declarative

0 comments on commit 59b5aaf

Please sign in to comment.