log4j 1 到 log4j 2 属性文件

log4j 1 to log4j 2 properties file

有人可以帮我吗?

我正在从 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>

我查看了其他答案,但 none 帮助了我。这就是为什么我诉诸于寻求帮助。 有人可以帮帮我吗?提前致谢。

属性文件格式不是Log4j中的默认配置格式2.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

(我假设所有三个 jar,log4j-1.2-apilog4j-apilog4j-core 都在当前目录中)。在你的情况下,这给你:

<?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