2

following is my configration of log4j2:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" name="MyApp" packages="com.swimap.base.launcher.log">
  <Appenders>
    <RollingFile name="RollingFile" fileName="logs/app-${date:MM-dd-yyyy-HH-mm-ss-SSS}.log"
                 filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      <Policies>
        <SizeBasedTriggeringPolicy size="1 KB"/>
      </Policies>
      <DefaultRolloverStrategy max="3"/>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="trace">
      <AppenderRef ref="RollingFile"/>
    </Root>
  </Loggers>
</Configuration>

the issue is that each time when start up my service, a new log will be created even the old one has not reached the specific size. If the program restart frequently, i will got many log files end with ‘.log’ which never be compressed.

the logs i got like this:

/log4j2/logs
/log4j2/logs/2017-07
/log4j2/logs/2017-07/app-07-18-2017-1.log.gz
/log4j2/logs/2017-07/app-07-18-2017-2.log.gz
/log4j2/logs/2017-07/app-07-18-2017-3.log.gz
/log4j2/logs/app-07-18-2017-20-42-06-173.log
/log4j2/logs/app-07-18-2017-20-42-12-284.log
/log4j2/logs/app-07-18-2017-20-42-16-797.log
/log4j2/logs/app-07-18-2017-20-42-21-269.log

someone can tell me how can i append log to the exists log file when i start up my program? much thanks whether u can help me closer to the answer!!

Z.min
  • 21
  • 2

2 Answers2

0

I suppose that your problem it that you have fileName="logs/app-${date:MM-dd-yyyy-HH-mm-ss-SSS}.log in your log4j2 configuration file.

This fileName template means that log4j2 will create log file with name that contains current date + hours + minutes + seconds + milliseconds in its name.

You should probably remove HH-mm-ss-SSS section and this will allow you to have daily rolling file and to not create new file every app restart.

You can play with template and choose format that you need.

If you want only one log file forever - then create constant fileName, like fileName=app.log

rxn1d
  • 1,236
  • 6
  • 18
  • Much thanks, buddy. Your suggestions seems like useful. However,i can not change the format of the log file name because of business not permitting. – Z.min Jul 19 '17 at 00:35
0

It's not hard to implement this. There is a interface DirectFileRolloverStrategy, implement below method:

public String getCurrentFileName(RollingFileManager manager)

Mybe someone met same problem and this can help him.

Z.min
  • 21
  • 2