log4j2 TimeBasedTriggeringPolicy未记录任何内容

gmxoilav  于 2023-02-16  发布在  其他
关注(0)|答案(2)|浏览(188)

我想创建一个简单的TimeBasedTriggeringPolicy,每5分钟创建一个新的日志文件。但是它没有记录任何东西。我不知道我做错了什么。这里是详细信息,谢谢!
我的log4j依赖项:

<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.17.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-to-slf4j</artifactId>
        <version>2.17.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.17.1</version>
    </dependency>

log4j.rootLogger=INFO,rolling

log4j.appender.rolling.type=RollingFile
log4j.appender.rolling.name=fileAppender
log4j.appender.rolling.fileName=${spark.yarn.app.container.log.dir}/spark-${date:yyyyMMdd_HH-mm}.log
log4j.appender.rolling.filePattern=${spark.yarn.app.container.log.dir}/spark-%d{yyyyMMdd_HH-mm}-%i.log
log4j.appender.rolling.layout.type=PatternLayout
log4j.appender.rolling.layout.pattern=%d %p %t %c - %m%n
log4j.appender.rolling.policies.type=Policies
log4j.appender.rolling.policies.time.type=TimeBasedTriggeringPolicy
log4j.appender.rolling.policies.time.interval=5
log4j.appender.rolling.policies.time.modulate=true

log4j.rootLogger.level=info
log4j.rootLogger.appenderRef.rolling.ref=fileLogger

错误日志:

log4j:ERROR Could not find value for key log4j.appender.rolling
log4j:ERROR Could not instantiate appender named "rolling".
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/mnt1/yarn/usercache/hadoop/filecache/45/__spark_libs__6130489434181934811.zip/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/lib/hadoop/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
    • 最终工作结果(每分钟创建日志,每月轮换):**
log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.loggerId.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.loggerId.rollingPolicy.ActiveFileName=${spark.yarn.app.container.log.dir}/spark.log
log4j.appender.loggerId.rollingPolicy.FileNamePattern=${spark.yarn.app.container.log.dir}/spark_%d{dd-HHmm}.log.gz
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId.layout.ConversionPattern=%d %p %t %c - %m%n
log4j.appender.loggerId.encoding=UTF-8
iswrvxsc

iswrvxsc1#

您的配置中有几个错误(使用-Dlog4j.debug=true运行以捕获所有错误):

  • Log4j2没有前缀为log4j.的属性,请删除前缀,
  • 您的配置中没有spark.yarn.app.container.log.dir属性。如果您要使用Java系统属性,请使用${sys:spark.yarn.app.container.log.dir}
  • 定义根日志记录器两次:
  • 一次使用版本2.17.2中引入的速记符号:rootLogger = INFO, rolling
  • 另一次使用完整的符号:
rootLogger.level = INFO
rootLogger.appenderRef.rolling ref = fileLogger

两个定义都不使用您给附加器的名称:* * fileAppender**.

  • 您没有<SizeBasedRollingPolicy>,但您的模式包含%i。如果删除-%i,则fileNamefilePattern相同:您可能希望使用直接滚动策略并省略fileName
zvms9eto

zvms9eto2#

这将每分钟创建一个日志,我们不能以5分钟为增量,除非我们使用xml或自定义。
它每月旋转一次,我去掉了yyyyMM,这样它就可以每月旋转一次,去掉dd,它就会每天旋转一次:

log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.loggerId.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.loggerId.rollingPolicy.ActiveFileName=${spark.yarn.app.container.log.dir}/spark.log
log4j.appender.loggerId.rollingPolicy.FileNamePattern=${spark.yarn.app.container.log.dir}/spark_%d{dd-HHmm}.log.gz
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId.layout.ConversionPattern=%d %p %t %c - %m%n
log4j.appender.loggerId.encoding=UTF-8

相关问题