Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

484
Views
¿Cómo puedo cambiar el nivel AppenderRef en log4j2 mediante programación?

¿Cómo puedo cambiar el Level AppenderRef en log4j2?

Hay una pregunta de stackoverflow (con respuesta) donde esto se resolvió sin programación. Quiero lo mismo pero programáticamente en su lugar. Solo hay un método get en AppenderRef para recuperar el Level , pero no hay ningún método para configurarlo.

Entonces, ¿hay alguna forma de establecer el Level en AppenderRef en log4j2 mediante programación?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Tienes que hacerlo eliminando el appender y luego volviéndolo a agregar con el nivel deseado.

Ejemplo log4j2.xml:

 <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <File name="logFile" fileName="log.txt" immediateFlush="false" append="true"> <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </File> </Appenders> <Loggers> <Root level="trace"> <AppenderRef ref="logFile" level="info" /> </Root> </Loggers> </Configuration>

Código de ejemplo:

 package example; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.LoggerConfig; public class Log4j2SetAppenderRefLvlMain { private static final Logger log = LogManager.getLogger(); public static void main(String[] args) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Configuration config = ctx.getConfiguration(); log.info("Before altering the appender!"); LoggerConfig rootLoggerConfig = config.getLoggers().get(""); rootLoggerConfig.removeAppender("logFile"); rootLoggerConfig.addAppender(config.getAppender("logFile"), Level.WARN, null); ctx.updateLoggers(); log.info("After altering the appender!"); log.warn("After altering the appender!"); } }

Producción:

 2017-04-13 21:04:20.892 [main] INFO example.Log4j2SetAppenderRefLvlMain - Before altering the appender! 2017-04-13 21:04:20.895 [main] WARN example.Log4j2SetAppenderRefLvlMain - After altering the appender!

Observe cómo solo se imprime el mensaje de nivel WARN en el registro después de que cambiamos el nivel del agregador a WARN. Esto prueba que funciona.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!