<noframes id="bhrfl"><address id="bhrfl"></address>

    <address id="bhrfl"></address>

    <noframes id="bhrfl"><address id="bhrfl"><th id="bhrfl"></th></address>

    <form id="bhrfl"><th id="bhrfl"><progress id="bhrfl"></progress></th></form>

    <em id="bhrfl"><span id="bhrfl"></span></em>

    全部
    常見問題
    產品動態
    精選推薦

    Spring Boot 2.x基礎教程:使用Log4j2記錄日志

    管理 管理 編輯 刪除

    動手試一試

    這里省略Spring Boot項目的基礎創建,如果您還不會可以看本教程的快速入門open in new window

    下面的操作你可以基于Spring Boot 2.x中默認日志框架Logbackopen in new window一文的例子繼續下去,也可以用任何一個Spring Boot 2.x的項目來嘗試。

    第一步:在pom.xml中引入Log4j2的Starter依賴spring-boot-starter-log4j2,同時排除默認引入的spring-boot-starter-logging,比如下面這樣:


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</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>
    

    第二步:在配置文件application.properties中,通過logging.config配置指定log4j2的配置文件位置,比如下面這樣:


    logging.config=classpath:log4j2.xml
    

    第三步:在resource目錄下新建log4j2.xml(這里不絕對,根據第二步中配置的內容來創建),然后加入log4j2的日志配置,比如,下面這樣:


    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="INFO">
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            </Console>
        </Appenders>
        <Loggers>
            <Root level="INFO">
                <AppenderRef ref="Console"/>
            </Root>
        </Loggers>
    </Configuration>
    

    主要是方便大家理解如何把log4j2引入Spring Boot,對于log4j2如何配置這里不做詳細介紹,所以這里就放個簡單配置讓程序跑起來即可。如果想深入了解log4j2的配置,可以點擊這里查看open in new window

    OK,到這里整合過程就結束了。是不是很簡單呢?如果您學習過程中如遇困難?可以加入我們超高質量的Spring技術交流群open in new window,參與交流與討論,更好的學習與進步!更多Spring Boot教程可以點擊直達!open in new window,歡迎收藏與轉發支持!

    #常見問題

    可能有小伙伴會問,之前不是推薦大家用Slf4j來記錄日志,隔離了具體實現的日志框架么?那我怎么知道這一頓操作之后,真的已經用上Log4j2了呢?

    這個其實很好判斷,大家只需要在用到日志的地方,加個端點,Debug跑起來,觀察下log對象就可以了,比如:

    下面這個是使用默認Logback的情況:

    使用Logback

    下面這個是使用Log4j2的情況

    使用Log4j2

    最后,因為之前Log4j2有過很大的漏洞問題,大家一定要用最新版本哦!

    安全起見,建議您至少要用2.17.0以上的版本(如果用的Spring Boot 2.6.2+,那已經是2.17.0了,不需要擔心)。當然,目前最新已經到2.17.1,您也可以自己升級到2.17.1來使用,如何升級呢?還是按照這篇文章open in new window介紹的操作即可。

    #代碼示例

    本文的完整工程可以查看下面倉庫中2.x目錄下的chapter8-2工程:


    請登錄后查看

    CRMEB 最后編輯于2025-02-28 15:15:37

    快捷回復
    回復
    回復
    回復({{post_count}}) {{!is_user ? '我的回復' :'全部回復'}}
    排序 默認正序 回復倒序 點贊倒序

    {{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

    作者 管理員 企業

    {{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
    {{item.is_suggest == 1? '取消推薦': '推薦'}}
    沙發 板凳 地板 {{item.floor}}#
    {{item.user_info.title || '暫無簡介'}}
    附件

    {{itemf.name}}

    {{item.created_at}}  {{item.ip_address}}
    打賞
    已打賞¥{{item.reward_price}}
    {{item.like_count}}
    {{item.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復

    {{itemc.user_info.nickname}}

    {{itemc.user_name}}

    回復 {{itemc.comment_user_info.nickname}}

    附件

    {{itemf.name}}

    {{itemc.created_at}}
    打賞
    已打賞¥{{itemc.reward_price}}
    {{itemc.like_count}}
    {{itemc.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復
    查看更多
    打賞
    已打賞¥{{reward_price}}
    772
    {{like_count}}
    {{collect_count}}
    添加回復 ({{post_count}})

    相關推薦

    快速安全登錄

    使用微信掃碼登錄
    {{item.label}} 加精
    {{item.label}} {{item.label}} 板塊推薦 常見問題 產品動態 精選推薦 首頁頭條 首頁動態 首頁推薦
    取 消 確 定
    回復
    回復
    問題:
    問題自動獲取的帖子內容,不準確時需要手動修改. [獲取答案]
    答案:
    提交
    bug 需求 取 消 確 定
    打賞金額
    當前余額:¥{{rewardUserInfo.reward_price}}
    {{item.price}}元
    請輸入 0.1-{{reward_max_price}} 范圍內的數值
    打賞成功
    ¥{{price}}
    完成 確認打賞

    微信登錄/注冊

    切換手機號登錄

    {{ bind_phone ? '綁定手機' : '手機登錄'}}

    {{codeText}}
    切換微信登錄/注冊
    暫不綁定
    亚洲欧美字幕
    CRMEB客服

    CRMEB咨詢熱線 咨詢熱線

    400-8888-794

    微信掃碼咨詢

    CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
    返回頂部 返回頂部
    CRMEB客服