Log4j%1至log4j%2属性文件

2022-04-12 00:00:00 java log4j log4j2

有人能帮帮我吗?

我正在从log4j%1转到%2,但无法获取工作属性文件。滚动文件vds.log未显示

这是我以前的工作道具

# default properties to initialise log4j
log4j.rootLogger=INFO, rollingFile

# settings for specific packages
log4j.logger.com.wordnik.swagger=INFO
log4j.logger.org.apache.camel.component.servlet=INFO
log4j.logger.org.apache.camel.component.swagger=INFO
log4j.logger.org.apache.camel=INFO

# Console appender
# Uncomment when running locally
#log4j.appender.console=org.apache.log4j.ConsoleAppender
#log4j.appender.console.layout=org.apache.log4j.PatternLayout
#log4j.appender.console.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n


log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.maxFileSize=10MB
log4j.appender.rollingFile.maxBackupIndex=10
log4j.appender.rollingFile.File=C:/logs/vds.log

log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n
log4j.appender.rollingFile.append=true

这就是我尝试的方法,但不起作用

status = debug
name= vds

# Log files location
property.basePath = c:/logs

appender.logger.com.wordnik.swagger=INFO
appender.logger.org.apache.camel.component.servlet=INFO
appender.logger.org.apache.camel.component.swagger=INFO
appender.logger.org.apache.camel=INFO

# RollingFileAppender name, pattern, path and rollover policy
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= ${basePath}/app.log
appender.rolling.filePattern= ${basePath}/app_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%l] - %msg%n
appender.rolling.policies.type = Policies

# RollingFileAppender rotation policy
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.delete.type = Delete
appender.rolling.strategy.delete.basePath = ${basePath}
appender.rolling.strategy.delete.maxDepth = 10
appender.rolling.strategy.delete.ifLastModified.type = IfLastModified

# Delete all files older than 30 days
appender.rolling.strategy.delete.ifLastModified.age = 30d

# Configure root logger
rootLogger.level = debug
rootLogger.appenderRef.rolling.ref = fileLogger

POM

 <!-- logging -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.16.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.16.0</version>
        </dependency>

我查看了其他答案,但都没有帮助我。这就是我求助的原因。 有人能帮帮我吗?提前谢谢。


解决方案

属性文件格式不是Log4j2.x中的默认配置格式(仅在2.4中引入)。如果您要使用XML,log4j-1.2-api中有一个自动配置转换器,它可以使用以下命令将属性文件转换为XML:

java -cp log4j-1.2-api-2.16.0.jar:log4j-api-2.16.0.jar:log4j-core-2.16.0.jar org.apache.log4j.config.Log4j1ConfigurationConverter -i log4j.properties -o log4j2.xml

(我假设log4j-1.2-apilog4j-apilog4j-core这三个JAR都在当前目录中)。在您的情况下,这将为您提供:

<?xml version="1.0" ?>
<Configuration name="Log4j1">
  <Appenders>
    <RollingFile
      name="rollingFile"
      fileName="C:/logs/vds.log"
      filePattern="C:/logs/vds.log.%i">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n" />
      <Policies>
        <SizeBasedTriggeringPolicy size="10485760" />
      </Policies>
      <DefaultRolloverStrategy max="1" />
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="rollingFile" />
    </Root>
    <Logger
      name="org.apache.camel.component.swagger"
      level="INFO" />
    <Logger
      name="org.apache.camel.component.servlet"
      level="INFO" />
    <Logger
      name="org.apache.camel"
      level="INFO" />
    <Logger
      name="com.wordnik.swagger"
      level="INFO" />
  </Loggers>
</Configuration>

(缩进是我的)。备注:max="1"可能是转换器中的错误。

如果您坚持使用属性格式,您的记录器配置应该如下所示:

logger.1.name=com.wordnik.swagger
logger.1.level=INFO
logger.2.name=org.apache.camel.component.servlet
logger.2.level=INFO
logger.3.name=org.apache.camel.component.swagger
logger.3.level=INFO
logger.4.name=org.apache.camel
logger.4.level=INFO

相关文章