亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了在Spring Boot Web應(yīng)用程序中使用橋JAR從log4j 1.x遷移到log4j 2.x時(shí)無法生成日志的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試從log4j1.x遷移到log4j2.x。

Followed this link - https://logging.apache.org/log4j/2.x/manual/migration.html

但我沒有看到更改后會(huì)生成日志。我想不出我錯(cuò)過了什么。

以下是詳細(xì)信息-
現(xiàn)有的log4j版本-

 <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

替換為-

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-1.2-api</artifactId>
  <version>2.6.2</version>
</dependency>
<dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-jcl</artifactId>
 <version>2.6.2</version>
</dependency>

我可以看到log4j-1.2.17.jar被這四個(gè)JAR替換-

    log4j-jcl2.6.2.jar
    log4j-core-2.1.jar
    log4j-api-2.1.jar
    log4j-1.2-api-2.6.2.jar

這是現(xiàn)有的配置文件(文件名/usr/local/log4j.properties)-

log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/access.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
   

已替換此系統(tǒng)屬性值-

logging.config=/usr/local/log4j.properties

使用這兩行

log4j1.compatibility=true
log4j.configuration=/usr/local/log4j.properties

推薦答案

Spring Boot在非常早期的階段重新配置您的日志框架。這就是為什么在應(yīng)用程序啟動(dòng)期間,您的外部log4j.properties文件會(huì)被Spring替換。

如果不提供logging.config屬性,將使用固定的類路徑資源列表(參見list)或應(yīng)用默認(rèn)配置。

最近的Log4j版本上,您只需設(shè)置系統(tǒng)屬性:

logging.config=/usr/local/log4j.properties
log4j.configurationFactory=org.apache.log4j.config.Log4j1ConfigurationFactory

但是,您不能使用最新版本,因?yàn)椋?/p>

    突破性的變化(參見LOG4J2-1547)中的log4j-core使2.7.0版和更高版本與Spring Boot 1.2.x、不兼容
    aseries of security vulnerabilities將您的選擇進(jìn)一步限制為2.3.2版。

使用2.3.2版需要將log4j.properties文件轉(zhuǎn)換為Log4j 2.x格式(可以使用轉(zhuǎn)換器from this question):

<?xml version="1.0"?>
<Configuration name="Log4j1">
  <Appenders>
    <RollingFile name="file" fileName="/var/log/access.log"
    filePattern="/var/log/access.log.%i">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
      <Policies>
        <SizeBasedTriggeringPolicy size="5MB" />
      </Policies>
      <DefaultRolloverStrategy max="10" />
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="file" />
    </Root>
  </Loggers>
</Configuration>

并設(shè)置系統(tǒng)屬性:

logging.config=/path/to/the/file/above.xml

備注:Spring Boot提供了一系列啟動(dòng)器,可以拉取正確的依賴項(xiàng)。要使用Log4j 2.x,只需排除標(biāo)準(zhǔn)spring-boot-starter-logging,包含spring-boot-starter-log4j2即可。不需要顯式的Log4j依賴項(xiàng)(如果在代碼中使用Log4j 1.x,則log4j-1.2-api除外):

  <properties>
    <log4j2.version>2.3.2</log4j2.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-1.2-api</artifactId>
      <version>${log4j2.version}</version>
    </dependency>
  </dependencies>

這篇關(guān)于在Spring Boot Web應(yīng)用程序中使用橋JAR從log4j 1.x遷移到log4j 2.x時(shí)無法生成日志的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標(biāo)簽:Boot Spring Web 應(yīng)用程序 日志 生成 遷移
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評(píng)定2018-06-03

通用課目體育訓(xùn)練成績評(píng)定