admin管理员组

文章数量:1122832

I am using %d{UNIX} with apache log4j which is not working gradle file:

implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.24.1'
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.24.1'

log4j2.xml

<RollingFile name="ROLLING" fileName="logs/app.log" filePattern="logs/app-%d{UNIX}.log.gz" ignoreExceptions="false">
    <PatternLayout>
        <Pattern>%m%n</Pattern>
    </PatternLayout>
    <Policies>
        <TimeBasedTriggeringPolicy interval="5" modulate="true"/>
        <SizeBasedTriggeringPolicy size="20 KB"/>
    </Policies>
    <DefaultRolloverStrategy max="10"/>
</RollingFile>

Error :

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

I am refering the doc: Pattern Layout Log4j

I am using %d{UNIX} with apache log4j which is not working gradle file:

implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.24.1'
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.24.1'

log4j2.xml

<RollingFile name="ROLLING" fileName="logs/app.log" filePattern="logs/app-%d{UNIX}.log.gz" ignoreExceptions="false">
    <PatternLayout>
        <Pattern>%m%n</Pattern>
    </PatternLayout>
    <Policies>
        <TimeBasedTriggeringPolicy interval="5" modulate="true"/>
        <SizeBasedTriggeringPolicy size="20 KB"/>
    </Policies>
    <DefaultRolloverStrategy max="10"/>
</RollingFile>

Error :

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

I am refering the doc: Pattern Layout Log4j

Share Improve this question edited Nov 25, 2024 at 9:09 RCS asked Nov 22, 2024 at 16:18 RCSRCS 1,43212 silver badges31 bronze badges 1
  • Can you add the entire error message (with stacktrace) to the question? – Piotr P. Karwasz Commented Nov 22, 2024 at 22:38
Add a comment  | 

1 Answer 1

Reset to default 0

The TimeBasedTriggeringPolicy determines the rollover interval based on the %d{...} pattern (see documentation). For example if your pattern is %d{dd-HH-mm} then the rollover interval will be minutes.

However, the Log4j-specific UNIX specifier is not supported. If it were supported, it would probably use a rollover interval measured in seconds. Are you trying to rotate your files every 5 seconds?

Edit: I noticed another problem in your configuration: you don't have a %i pattern, which is required so that size-based rollovers don't overwrite old files.

There are different requirements on the file pattern, depending on what policies you use:

  1. If you use only a size-based policy, you need %d, %i or both. The date pattern will be filled with the current rollover timestamp.
  2. If you use only a time-based policy, you need %d with a supported pattern to determine the rollover period. The pattern will be replaced with the timestamp of the previous rollover.
  3. If you use both, you need both the %d and %i patterns. Each triggering policy is then responsible for its pattern: size-based rollovers will increment %i, time-based rollovers will increment %d.

本文标签: log4j2Unix time is not working with log4j to perform file rolloverStack Overflow