aleroot
aleroot

Reputation: 72636

logback change configuration at runtime and log file creation issue

I have a logback.xml configuration file that looks like the following :

<configuration>

 <property name="defaultPattern"
        value="%d{dd/MM/yyyy HH:mm:ss.SSS} [%thread] %-5level - %msg%n" />

 <!-- Appenders Configuration -->
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <target>System.err</target>
    <filter class="com.aleroot.ErrOutFilter" />
    <encoder>
      <pattern>${defaultPattern}</pattern>
    </encoder>
  </appender>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>aleroot.log</file>
    <append>true</append>
    <encoder>
      <pattern>${defaultPattern}</pattern>
    </encoder>
  </appender>


  <!-- Output Configuration -->
  <root level="debug">
    <appender-ref ref="FILE" />
    <appender-ref ref="CONSOLE"/>
  </root>

  <logger name="mainLogger" level="debug">
    <appender-ref ref="FILE" />
    <appender-ref ref="CONSOLE"/>
</logger>

</configuration>

i want to change the file property of the FILE appender at runtime and add a DatePattern to the filename, i've tried with this code :

LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
          ch.qos.logback.classic.Logger logbackLogger = lc.getLogger("mainLogger");
          FileAppender<ILoggingEvent> fileAppender =
                  (FileAppender<ILoggingEvent>) logbackLogger.getAppender("FILE");
              if(fileAppender != null) {
                fileAppender.stop();
                fileAppender.setFile("aleroot-ddMMyyyy.log");
                fileAppender.setContext(lc);
                fileAppender.start();
              }

The problem is that the file aleroot.log is created anyway also if i have changed the filename, furthermore i get another file that is named exactly : aleroot-ddMMyyyy.log while i want to have a filename like aleroot-04122011.log . How can i achieve that ?

Is there a settings to avoid that the log file will be created at the LoggerFactory.getLogger("mainLogger") call ? I want that the log file will be created only the first time that i write into the log files, there is no need to create an empty log file . The log file should be created the first time that i log something to the log, for example at the first logger.debug("Something.log") .

Is there a settings to achieve that ?

Upvotes: 2

Views: 7037

Answers (2)

srkavin
srkavin

Reputation: 1180

Lazy log creation is not supported yet. But there is a new feature request for it http://jira.qos.ch/browse/LBCORE-184

A RollingFileAppender(link) with a TimeBasedRollingPolicy(link) may of great help here. This allows you to specify a file pattern, and also, when you want the LB to create a new log file (every minute/hour or daily, weekly or monthly).

Sample configuration from the given site.

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>aleroot-%d{yyyy-MM-dd}.log</fileNamePattern>

      <!-- keep 30 days' worth of history -->
      <maxHistory>30</maxHistory>
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender> 

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>

Upvotes: 1

Tommy
Tommy

Reputation: 4111

I would think the filename would need some kind of pattern to recognize that you want a date. If that is supportet at all in logback..

aleroot-%d{ddMMyyyy}.log

Upvotes: 0

Related Questions