本文介紹了Log4j%1至log4j%2屬性文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
有人能幫幫我嗎?
我正在從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-api
、log4j-api
和log4j-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
這篇關于Log4j%1至log4j%2屬性文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,