Skip to content

Instantly share code, notes, and snippets.

@david-hosier
Last active December 17, 2020 17:54
Show Gist options
  • Save david-hosier/df7d41bb1c19dcb9bf3f14d8147d6c17 to your computer and use it in GitHub Desktop.
Save david-hosier/df7d41bb1c19dcb9bf3f14d8147d6c17 to your computer and use it in GitHub Desktop.
This gist shows how to use an injected Log4j logger in a Groovy script. Note that the injection only works on a class. This is an example of using Log4j2 with the latest version as the time of writing. If the log4j2.xml file lives in the same directory as the script, it will automatically be used to configure logging. Inspired by https://stackov…
@Grab(group='org.apache.logging.log4j', module='log4j-core', version='2.14.0')
@Grab(group='org.apache.logging.log4j', module='log4j-api', version='2.14.0')
import org.apache.log4j.*
import groovy.util.logging.*
@Log4j2
class LoggingTest {
def execute() {
log.info "Yay, it worked!"
}
}
def test = new LoggingTest()
test.execute()
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS}: %-5level: %msg%n%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment