13

I want to create day dependent logfiles with log4j2:

<RollingFile name="APP" fileName="application-%d{yyyy-MM-dd}.log" />

Resulting logfile name: application-%d{yyyy-MM-dd}.log, the timestamp is not replaced. Why?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

6 Answers6

34

To append the filename with date, replace %d with below format, i was having the same problem and but got by doing so :

<RollingFile name="APP" fileName="application-${date:yyyy-MM-dd}.log" />
Bishal Gupta
  • 375
  • 4
  • 9
  • 1
    It is overwriting old logs with chunk of current date logs. Example: https://stackoverflow.com/questions/49432150/log4j2-overwrites-past-day-log-file – JackTheKnife Mar 23 '18 at 13:30
9

The pattern should not be given in the attribute "fileName" rather you have to specify the pattern in the attribute "filePattern" as like below.

<RollingFile name="RollingFile" fileName="${log-path}/filename.log" 
filePattern="${log-path}/filename-%d{yyyy-MM-dd}-%i.log" >
...
...
</RollingFile>

The "%i" is the counter that will be automatically incremented in rollover.

Hope this will help you.

Loganathan Mohanraj
  • 1,736
  • 1
  • 13
  • 22
3

Try this:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">

    <Properties>
        <Property name="log-path">D:/logs/</Property>
    </Properties>

    <Appenders>

        <RollingFile name="DebuggerLogger" fileName="${log-path}CMSAutomation.${date:yyyy-MM-dd_hh-mm-ss}.log" filePattern="${log-path}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
        </RollingFile>

    </Appenders>

    <Loggers>
        <Root level="ALL">
            <AppenderRef ref="DebuggerLogger"/>
        </Root>
    </Loggers>

</Configuration>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • 1
    You'll want to use `${date:yyyy-MM-dd_HH-mm-ss}` for 24 hour time. Other 5am and 5pm both appear as 05. – Will Sep 26 '19 at 16:16
1

In yaml check the Property filePattern defined as "${date:yyyy-MM-dd}", which helps to tag the date. If you are interested to tag the HOSTNAME environment variable to date then : "${env:HOST}-${date:yyyy-MM-dd}"

Properties:
    Property:
      - name: log-path
        value: "logs"
      - name:  filePattern
        value: "${date:yyyy-MM-dd}"

  Appenders:

    Console:
      name: Console_Appender
      target: SYSTEM_OUT
      PatternLayout:
        pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"

    File:
      name: File_Appender
      fileName: "${log-path}/filelog-${filePattern}.log"

      PatternLayout:
        pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"

    RollingFile:
      - name: RollingFile_Appender
        fileName: "${log-path}/rollingfile-${filePattern}.log"
        filePattern: "logs/archive/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz"
        PatternLayout:
          pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
Alferd Nobel
  • 3,185
  • 2
  • 30
  • 35
1

To show the date with the time timestamp to log4j2 file then add in log4j2.property file like: appender.file.fileName=${filename}/app-${date:yyyy-MM-dd-hh-mm-ss}.log

0

Use ASizeBasedTriggeringPolicy class in appender. You can even append the current second to the logfile name. %d{yyyy_MM_dd HH.mm.ss}

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.rolling.*;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;

@Plugin(name = "ASizeBasedTriggeringPolicy",
        category = "Core",
        printObject = true
)
public class ASizeBasedTriggeringPolicy extends AbstractTriggeringPolicy {
    private SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy;
    private RollingFileManager aManager;

    protected ASizeBasedTriggeringPolicy(String maxFileSize) {
        sizeBasedTriggeringPolicy = SizeBasedTriggeringPolicy.createPolicy(maxFileSize);

    }

    public void initialize(RollingFileManager aManager) {
        sizeBasedTriggeringPolicy.initialize(aManager);
        this.aManager = aManager;
    }

    public boolean isTriggeringEvent(LogEvent event) {
        if (sizeBasedTriggeringPolicy.isTriggeringEvent(event)) {
            aManager.getPatternProcessor().setPrevFileTime(System.currentTimeMillis());
            return true;
        } else {
            return false;
        }
    }

    @PluginFactory
    public static ASizeBasedTriggeringPolicy createPolicy(@PluginAttribute("size") String size) {
        return new ASizeBasedTriggeringPolicy(size);
    }
}

Then use ASizeBasedTriggeringPolicy in log appender

<RollingFile name="complete-log" fileName="${log-path}/complete-${date:yyyy_MM_dd HH.mm.ss} .log" 
                 filePattern="${log-path}/app-complete-%d{yyyy_MM_dd HH.mm.ss} - %i.log" >
    ...
    ... 
    <Policies>
         <ASizeBasedTriggeringPolicy size="200 kB"  />
    </Policies>
</RollingFile>
Shapur
  • 498
  • 7
  • 17