Re: Log4j configuration in a Groovy Script

Jochen Theodorou <[email protected]> Tue, 19 Aug 2025 13:18:12 +0200
Newsgroups gmane.comp.lang.groovy.user
Message-ID <[email protected]>

On 19.08.25 09:31, Per Nyfelt wrote:
[...]
> 1. Set the log level on the root logger
> @Grab(group=3D'org.apache.logging.log4j', module=3D'log4j-api',=20
> version=3D'2.20.0'),
> @Grab(group=3D'org.apache.logging.log4j', module=3D'log4j-core',=20
> version=3D'2.20.0'),
> @Grab(group=3D'org.apache.logging.log4j', module=3D'log4j-slf4j-impl',=
=20
> version=3D'2.20.0')
>=20
> import org.apache.logging.log4j.Logger
> import org.apache.logging.log4j.LogManager
> import org.apache.logging.log4j.Level
> import org.apache.logging.log4j.core.config.Configurator
>=20
> Configurator.setRootLevel(Level.INFO)
>=20
> @Field
> final Logger log =3D LogManager.getLogger()
>=20
> In this case=C2=A0only LogManager.getLogger() works,=20
> LogManager.getLogger(this.class) does not work.

not the reverse? getLogger should have the problem,=20
getLogger(this.class) maybe not. That is also what I can verify.

[...]
> I.e. IndyInterface instead of my script class.

yeah, we have at least one bug in the indy callsite caching code, that=20
is causing this. Would be nice if LOG4j had a way to ignore classes in=20
that discovery phase... it does not, right?

> To make log output better you need to do (the script name is=20
> createExcel.groovy)
>=20
> @Field
> final Logger log =3D LogManager.getLogger(this.class.name <http://this.c=
lass.name>)
> Configurator.setLevel(log.getName(), Level.INFO)
> Configurator.setRootLevel(Level.INFO)
> LogManager.getContext(false).updateLoggers()
>=20
> 09:24:29.625 [main] INFO =C2=A0createExcel - Found 2 CSV files to proces=
s.
>=20
> LogManager.getLogger(this.class) does NOT work (results in no output) wh=
ich is curious.

In my test it was for example Script1 if executed from a GroovyShell, or=
=20
the script name as class name if executed from the command line.

bye Jochen