1

I am working on logging for Java and using log4j for the same. This is my config file for FileRollOver

status = warn
property.filename = $${date:YYYY-MM-dd}

# Rotate log file
appender.rolling.type = RollingFile
appender.rolling.name = LogToRollingFile
appender.rolling.fileName = ../app_log/service/app-java-${date:YYYY-MM-dd}.log
appender.rolling.filePattern = app_log/service/app-java-${date:YYYY-MM-dd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} [%-5p] %M: %m%n
appender.rolling.policies.type = Policies
#appender.rolling.policies.time.type = OnStartupTriggeringPolicy
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval=1
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 10

#logger specific configuration
loggers
logger.name=rolling
logger.additivity = true
logger.appenderRefs = rolling
logger.appenderRef.rolling.ref = LogToRollingFile
logger.appenderRef.rolling.stdout.ref = STDOUT

#root logger configuration
rootLogger.level = INFO
#rootLogger.appenderRef.stdout.ref = LogToConsole
rootLogger.appenderRef.stdout.ref = LogToRollingFile

The config is like a new file for logs should be created each new day. But when I am running it is throwing me this exception.

2023-05-05 14:37:39,819 main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile: java.lang.IllegalStateException: Pattern does not contain a date java.lang.IllegalStateException: Pattern does not contain a date

and

2023-05-05 14:37:39,823 main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender

Though my file name has date in it but it is not working out anyway. What could go wrong? PS: I am using IntelliJ Idea as IDE and it is a Gradle project.

joma
  • 11
  • 3

1 Answers1

0

There are two kinds of placeholders in Log4j 2.x Core (cf. this question):

  • patterns like %d{...}, which extract some property from an object that depends on the context,
  • lookups like ${date:...}, which extract some property from a LogEvent.

While nowadays they are mostly the same, the filePattern property needs to contain a date pattern, not a lookup.

Just use:

appender.rolling.filePattern = app_log/service/app-java-%d{yyyy-MM-dd}.%i.log.gz

Also remove the appender.rolling.fileName and change DefaultRolloverStrategy to DirectWriteRolloverStrategy, since the former does not support using the same value for fileName and filePattern.

Remark: you probably want to use yyyy instead of YYYY, since the latter means "week-based year", a kind of year composed by full weeks. E.g. 2023-01-01 is part of the week-based year 2022.

The %i I added to your pattern is necessary for size-based rotation.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • when I removed `appender.rolling.fileName` and added DirectFileRolloverStrategy this what it is throwing at me `2023-05-06 19:34:39,801 main ERROR Unable to locate plugin type for DirectFileRolloverStrategy` `2023-05-06 19:34:39,954 main ERROR Unable to locate plugin for DirectFileRolloverStrategy` – joma May 06 '23 at 14:07
  • Basically what I want is it should create a new log file every mid night (when date changes) and should properly rename it as per the pattern configured. Rather doing that it is adding the logs of the next day in the same file instead of creating a new one every day. – joma May 06 '23 at 14:09
  • Check the plugin name: it is `DirectWriteRolloverStrategy` (with `Write`) and **not** `DirectFileRolloverStrategy`. – Piotr P. Karwasz May 06 '23 at 16:28
  • My typing mistake I used the same `DirectWriteRolloverStrategy` still not working – joma May 09 '23 at 12:48