Log4J change configuration runtime

81 Views Asked by At

I have to switch Log4J configuration at runtime during tomcat webapp startup, I have tried this solution:

LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
ctx.setConfigLocation(ResourceUtils.toURI(getClass().getResource("/mynewconfig.xml")));
ctx.start();
ctx.reconfigure();

It seems to work in a Windows environment, but this doesn't work for ubuntu/linux. I added a System.out to check the existing appender just after this code, and in windows env I can find my new configured appenders, for linux I can see only the default startup one.

I have already checked file path permissions.

This is the default log4j.xml file

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
   <Appenders>
      <Console name="console-log" target="SYSTEM_OUT">
         <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
      </Console>
   </Appenders>
   <Loggers>
      <Root level="warn" additivity="false">
         <AppenderRef ref="console-log" />
      </Root>
   </Loggers>
</Configuration>

And this is the new one (mynewconfig.xml)

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
   <Appenders>
      <Console name="console-log" target="SYSTEM_OUT" >
        <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
      </Console>
      <RollingFile name="file-log" fileName="${sys:home}/log/app.log" filePattern="${sys:home}/log/app.%i.log">
         <PatternLayout>
            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
         </PatternLayout>
         <Policies>
            <SizeBasedTriggeringPolicy size="10MB" />
         </Policies>
         <DefaultRolloverStrategy max="5"/>
      </RollingFile>
      <RollingFile name="file-critico-log" fileName="${sys:home}/log/critico.log" filePattern="${sys:home}/log/critico.%i.log">
         <PatternLayout>
            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
         </PatternLayout>
         <Policies>
            <SizeBasedTriggeringPolicy size="10MB" />
         </Policies>
         <DefaultRolloverStrategy max="5"/>
      </RollingFile>
   </Appenders>
   <Loggers>
      <Logger name="com.mypackage" level="info" additivity="false">
         <appender-ref ref="file-log" />
         <appender-ref ref="console-log" />
      </Logger>
      <Logger name="critico" level="info" additivity="false">
         <appender-ref ref="file-log" level="error" />
         <appender-ref ref="file-critico-log" level="error" />
          <!-- appender-ref ref="console-log" level="error" / -->
      </Logger>
      <!-- Logger name="org.springframework.jdbc.core.JdbcTemplate" level="debug">
         <appender-ref ref="console-log" />
      </Logger-->
      <Root level="warn" additivity="false">
         <AppenderRef ref="file-log" />
         <AppenderRef ref="console-log" />
      </Root>
   </Loggers>
</Configuration>
1

There are 1 best solutions below

0
On BEST ANSWER

The problem was the Sandboxing filesystem of tomcat service in Ubuntu. Take a look at https://www.freedesktop.org/software/systemd/man/systemd.exec.html

I hade to explicit set this configuration:

ReadWritePaths=/var/mypath

in /lib/systemd/system/tomcat.service file