Hi,

I don't have a question, just some observations on using log4j in a script
that i found interesting.

In a Groovy 4.0.28 groovy script, i noticed that if i use log4j i need to
do one of the two options below to set the logging level:

1. Set the log level on the root logger
@Grab(group='org.apache.logging.log4j', module='log4j-api',
version='2.20.0'),
@Grab(group='org.apache.logging.log4j', module='log4j-core',
version='2.20.0'),
@Grab(group='org.apache.logging.log4j', module='log4j-slf4j-impl',
version='2.20.0')

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

Configurator.setRootLevel(Level.INFO)

@Field
final Logger log = LogManager.getLogger()

In this case only LogManager.getLogger() works,
LogManager.getLogger(this.class) does not work.

The downside is that the groovy script is not seen as the "owner" of the
log messages so output will be:
09:21:05.600 [main] INFO  org.codehaus.groovy.vmplugin.v8.IndyInterface -
Found 2 CSV files to process.

I.e. IndyInterface instead of my script class.

To make log output better you need to do (the script name is
createExcel.groovy)

@Field
final Logger log = LogManager.getLogger(this.class.name)
Configurator.setLevel(log.getName(), Level.INFO)
Configurator.setRootLevel(Level.INFO)
LogManager.getContext(false).updateLoggers()

09:24:29.625 [main] INFO  createExcel - Found 2 CSV files to process.

LogManager.getLogger(this.class) does NOT work (results in no output)
which is curious.

Best regards,

Per

Reply via email to