Create a new log file every time my program starts

I use the Apache community logging library and log4j to generate log files.

Now I want to create a new file every time I run my program. A current account must be added to the log file name.

For example: program_1.log program_2.log program_3.log

Do you know how I could achieve this?

+3
source share
7 answers

Turning my comment into a response at the request of:

Any reason you don't want to use a timestamp? I found them more meaningful as suffixes when I have to look back at the log files.

+1

, log4j, , FileAppender:

public class CustomAppender extends FileAppender {

 ...

  public
  CustomAppender(Layout layout, String filename, boolean append, boolean bufferedIO,
               int bufferSize) throws IOException {
    this.layout = layout;     
    // file name will be the basis; you should calculate the suffix yourself
    customname = filename + ...
    this.setFile(customname, append, bufferedIO, bufferSize);
  }

 ...
+1

. , , .

- , . , , - - log n, n - . 1 , 20 .

+1

, Log4J . , Log4J Extras Companion, RollingFileAppender, , , RollingPolicy TriggeringPolicy. .

0

, JVM. , ( , ), FileAppender. DatedFileAppender .

0

In a less elegant way, every time your program starts, it can create a log_num.dat file where it will track one number (the next log file to create).

0
source

it works for me

<appender name="fileAppender" class="org.apache.log4j.DailyRollingFileAppender">
  <param name="Threshold" value="TRACE" />
  <param name="File" value="amLog.log"/>
  <param name="DatePattern" value="'.'YYYY-MM-dd-HH-mm"/>
  <layout class="org.apache.log4j.PatternLayout">
     <param name="ConversionPattern" value="%d %-5p  [%c{1}] %m %n" />
  </layout>

0
source

Source: https://habr.com/ru/post/1747988/


All Articles