Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback - delete the log file on startup

Tags:

java

logback

I want to delete the log file each time the program is started as opposed to it being appended. I've tried using the cleanHistoryOnStart property but that seems to have no effect whatsoever. I'm probably missing something here.

I am on Linux and using Eclipse if that matters.

<?xml version="1.0" encoding="utf-8"?>
<configuration scan="true" scanPeriod="10 seconds">
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{dd.MM.yyyy. HH:mm:ss} %level [%thread] %logger{20} - %msg%n</pattern>
    </encoder>
  </appender>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>chat.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>chat.log.%d{yyyy-MM-dd}</fileNamePattern>
      <cleanHistoryOnStart>true</cleanHistoryOnStart>
    </rollingPolicy>
    <encoder>
      <pattern>%d{dd.MM.yyyy. HH:mm:ss} %level [%thread] %logger{20} - %msg%n</pattern>
    </encoder>
    <Encoding>utf-8</Encoding>
  </appender>
  <logger name="src" additivity="false" level="ALL">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="FILE"/>
  </logger>
  <root level="OFF">
    <appender-ref ref="STDOUT"/>
  </root>
</configuration>
like image 602
Venom Avatar asked Dec 27 '13 01:12

Venom


People also ask

How do I disable Logback log?

Logback does not allow logging to be disabled from the command line. However, if the configuration file allows it, you can set the level of loggers on the command line via a Java system property.

Where does Logback xml save to?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.

What is rolling log file?

It defines an interval over which log analysis can be performed. It keeps any single log file from becoming too large and assists in keeping the logging system within the specified space limits.


1 Answers

Include <param name="Append" value="false" /> in your appender to clean the log file on start up.

Otherwise try this one

http://veerasundar.com/blog/2009/08/how-to-create-a-new-log-file-for-each-time-the-application-runs/

like image 98
SANN3 Avatar answered Oct 16 '22 20:10

SANN3