Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rolling log Files & removing old log files

Tags:

I am working on a Java SOAP based webservice application where I am writing stdout to a text file as log for our reference. That file is growing enormously, so I need to check for the size of the file... For example if the file size crosses 10 Mb, I have to create another file.

Like this, I have to create 10 files, rotating one after the other until ten files. After reaching ten files, I have to delete the starting files and start creating again...

How can I delete files after the no. of files will become 10?

like image 847
user2377755 Avatar asked May 13 '13 12:05

user2377755


People also ask

What is a 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.

What is rolling policy in logging?

Size-based rolling policy allows to rollover based on file on each log file. For example, we can rollover to a new file when the log file reaches 10 MB in size. The maxFileSize is used to specify the size of each file when it gets rolled over.

Why is it important to rotate log files overwrite old log files )?

Rotating log files is important for several reasons. First, you probably don't want older log files eating up too much of your disk space. Second, when you need to analyze log data, you probably don't want those log files to be extremely large and cumbersome.


2 Answers

I use logback to do this. The example below is a time based rolling policy. Depending upon how much data your outputting during your logs, this may work for you as-is.

Also, as a bonus, my config file tosses the log into HTML to make it easy to view for management types who want to look though the log file.

Relevant part of the config file:

 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">     <file>logs\logFile.html</file>     <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">         <!-- daily rollover -- >         <fileNamePattern>logs\logFile.%d{yyyy-MM-dd}.%i.html</fileNamePattern>         <timeBasedFileNamingAndTriggeringPolicy             class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">             <!-- or whenever the file size reaches 10MB -- >             <maxFileSize>10MB</maxFileSize>         </timeBasedFileNamingAndTriggeringPolicy>         <!-- keep 10 days worth of history -->         <maxHistory>10</maxHistory>     </rollingPolicy>      <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">         <charset>UTF-8</charset>         <layout class="ch.qos.logback.classic.html.HTMLLayout">             <pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>         </layout>                </encoder> </appender>   <root level="DEBUG">     <appender-ref ref="STDOUT" />     <appender-ref ref="FILE" /> </root> 

relevant Maven dependencies:

    <dependency>         <groupId>ch.qos.logback</groupId>         <artifactId>logback-core</artifactId>         <version>1.0.12</version>     </dependency>      <dependency>         <groupId>ch.qos.logback</groupId>         <artifactId>logback-classic</artifactId>         <version>1.0.12</version>     </dependency> 
like image 94
Robert H Avatar answered Sep 18 '22 04:09

Robert H


I see a lot of answers telling you to use Log4J, but you can use Java's own logger to do this by simply creating a FileHandler:

Handler handler =     new FileHandler("%h/MyService-%g.log", 10 * 1024 * 1024, 10); handler.setLevel(Level.ALL); Logger.getLogger("").addHandler(handler); 
like image 43
VGR Avatar answered Sep 19 '22 04:09

VGR