Log4j configuration in a Groovy Script
Per Nyfelt <[email protected]> Tue, 19 Aug 2025 09:31:14 +0200
| Newsgroups | gmane.comp.lang.groovy.user |
|---|---|
| Message-ID | <CAOAt0KQv0oXLpyWASXLuSn2m5sPMiqbs0uA599LssFyby8ZQCw@mail.gmail.com> |
--000000000000f58bd1063cb2d73e Content-Type: text/plain; charset="UTF-8" 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 --000000000000f58bd1063cb2d73e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">Hi,<div><br></div><div>I don't have a question, just s= ome observations on using log4j in a script that i found interesting.</div>= <div><br></div><div>In a Groovy 4.0.28 groovy script, i noticed that if i u= se log4j i need to do one of the two options below to set the logging level= :</div><div><br></div><div>1. Set the log level on the root logger</div><fo= nt face=3D"monospace">@Grab(group=3D'org.apache.logging.log4j', mod= ule=3D'log4j-api', version=3D'2.20.0'),<br>@Grab(group=3D&#= 39;org.apache.logging.log4j', module=3D'log4j-core', version=3D= '2.20.0'),<br>@Grab(group=3D'org.apache.logging.log4j', mod= ule=3D'log4j-slf4j-impl', version=3D'2.20.0')<br><br>import= org.apache.logging.log4j.Logger<br>import org.apache.logging.log4j.LogMana= ger<br>import org.apache.logging.log4j.Level<br>import org.apache.logging.l= og4j.core.config.Configurator<br><br>Configurator.setRootLevel(Level.INFO)<= br><br>@Field<br>final Logger log =3D LogManager.getLogger()</font><div><br= ><div>In this case=C2=A0only LogManager.getLogger() works, LogManager.getLo= gger(this.class) does not work. </div><div><br></div><div>The downside is t= hat the groovy script is not seen as the "owner" of the log messa= ges so output will be:</div><div><font face=3D"monospace">09:21:05.600 [mai= n] INFO =C2=A0org.codehaus.groovy.vmplugin.v8.IndyInterface - Found 2 CSV f= iles to process.</font></div><div><font face=3D"monospace"><br></font></div= ><div><font face=3D"arial, sans-serif">I.e. IndyInterface instead of my scr= ipt class.</font></div><div><br></div><div>To make log output better you ne= ed to do (the script name is createExcel.groovy)</div><div><div style=3D"">= <pre style=3D"">@Field<br>final Logger log =3D LogManager.getLogger(<a href= =3D"http://this.class.name">this.class.name</a>)<br>Configurator.setLevel(l= og.getName(), Level.INFO)<br>Configurator.setRootLevel(Level.INFO) LogManager.getContext(false).updateLoggers()</pre><pre style=3D""><span sty= le=3D"color:rgb(34,34,34);background-color:rgb(255,255,255)">09:24:29.625 [= main] INFO =C2=A0createExcel - Found 2 CSV files to process.</span></pre></= div></div><div><pre>LogManager.getLogger(this.class) does NOT work (results= in no output) which is curious.</pre><pre>Best regards,</pre><pre>Per</pre= ></div></div></div> --000000000000f58bd1063cb2d73e--