{"id":232567,"date":"2021-12-31T09:10:50","date_gmt":"2021-12-31T01:10:50","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=232567"},"modified":"2021-12-22T14:11:43","modified_gmt":"2021-12-22T06:11:43","slug":"spring-boot-log4j2","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/spring-boot-log4j2.html","title":{"rendered":"Spring Boot \u5e94\u5bf9 Log4j2 \u6ce8\u5165\u6f0f\u6d1e\u6307\u5357"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nLog4J2\u6f0f\u6d1e\u6d89\u53ca\u7684\u5f71\u54cd\u592a\u5e7f\u4e86\uff0c\u6628\u5929\u53d1\u6587\u540e\u5f88\u591a\u7c89\u4e1d\u7559\u8a00\u95eeSpring Boot\u9879\u76ee\u662f\u5426\u53d7\u5230Log4J2\u6f0f\u6d1e\u5f71\u54cd\u3002Spring\u5b98\u65b9\u5df2\u7ecf\u5168\u9762\u8fdb\u884c\u4e86\u6392\u67e5\uff0c\u73b0\u5728\u5927\u5bb6\u53ef\u4ee5\u77e5\u9053\u8fd9\u4e9b\u4fe1\u606f\u548c\u5e94\u5bf9\u65b9\u6cd5\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u9ed8\u8ba4\u914d\u7f6e\u4e0d\u53d7\u5f71\u54cd<\/strong><\/div>\n

Spring Boot\u9ed8\u8ba4\u65e5\u5fd7\u7ec4\u4ef6\u662flogback\uff0c\u5f00\u53d1\u8005\u901a\u8fc7\u65e5\u5fd7\u95e8\u9762Slf4j\u8fdb\u884c\u96c6\u6210\u5bf9\u63a5\u3002Spring Boot \u7528\u6237\u53ea\u6709\u5728\u5c06\u9ed8\u8ba4\u65e5\u5fd7\u7cfb\u7edf\u5207\u6362\u5230 Log4J2 \u65f6\u624d\u4f1a\u53d7\u5230\u6b64\u6f0f\u6d1e\u7684\u5f71\u54cd\u3002Spring Boot\u5305\u542b\u7684log4j-to-slf4j\u548clog4j-api\u3001spring-boot-starter-logging\u4e0d\u80fd\u72ec\u7acb\u5229\u7528\u3002\u53ea\u6709log4j-core\u5728\u65e5\u5fd7\u6d88\u606f\u4e2d\u4f7f\u7528\u548c\u5305\u542b\u7528\u6237\u8f93\u5165\u7684\u5e94\u7528\u7a0b\u5e8f\u5bb9\u6613\u53d7\u5230\u653b\u51fb\u3002<\/p>\n

\u4e5f\u5c31\u662f\u8bf4Spring Boot\u73b0\u5728\u5305\u542bLog4J2\u7684\u4f9d\u8d56\u53ea\u8981\u4f60\u4e0d\u542f\u7528\u662f\u4e0d\u4f1a\u89e6\u53d1\u6f0f\u6d1e\u7684\u3002<\/p>\n

\"\"<\/p>\n

\u4e0b\u7248\u672c\u66f4\u65b0\u8865\u4e01<\/strong><\/div>\n

Spring Boot\u5c06\u57282021 \u5e74 12 \u6708 23 \u65e5\u540e\u53d1\u5e03\u7684 2.5.8 \u548c 2.6.2 \u7248\u672c\u5c06\u91c7\u7528\u6253\u4e86\u8865\u4e01\u7684Log4J v2.15.0\uff0c\u4f46\u7531\u4e8e\u8fd9\u662f\u4e00\u4e2a\u6781\u5176\u4e25\u91cd\u7684\u6f0f\u6d1e\uff0c\u4e00\u5b9a\u8981\u8986\u76d6\u6211\u4eec\u7684\u4f9d\u8d56\u9879\u7ba1\u7406\u5e76\u5c3d\u5feb\u5347\u7ea7\u60a8\u7684 Log4J2 \u4f9d\u8d56\u9879\u3002<\/p>\n

Maven\u7528\u6237<\/strong><\/span><\/div>\n

\u5bf9\u4e8e Maven \u7528\u6237\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8986\u76d6\u81ea\u5df1\u9879\u76ee\u4e2dpom.xml\u7684\u7248\u672c\u53f7\u914d\u7f6e\u5c5e\u6027\u6765\u4fee\u6539\u8be5\u4f9d\u8d56\u7684\u7248\u672c\u53f7\u3002\u63d0\u5347Log4J2\u5230\u5b89\u5168\u7248\u672c\u53ea\u9700\u8981\uff1a<\/p>\n

\r\n   <properties> <\/span>\r\n       <log4j2.version>2.15.0<\/log4j2.version> <\/span>\r\n   <\/properties> <\/span>\r\n<\/pre>\n

\u7136\u540e\u4f7f\u7528.\/mvnw dependency:list | grep log4j\u547d\u4ee4\u8fd0\u884c\u4ee5\u68c0\u67e5\u7248\u672c\u662f\u5426\u4e3a 2.15.0\u3002<\/p>\n

Gradle\u7528\u6237<\/strong><\/span><\/div>\n

\u5bf9\u4e8e\u5927\u591a\u6570\u7528\u6237\u6765\u8bf4\uff0c\u8bbe\u7f6elog4j2.version\u5c5e\u6027\u5c31\u8db3\u591f\u4e86\uff1a<\/p>\n

ext['log4j2.version'] = '2.15.0' <\/pre>\n

\u5982\u679c\u4f60\u7684Gradle\u5e76\u6ca1\u6709\u76f4\u63a5\u5bf9Spring Boot\u8fdb\u884c\u4f9d\u8d56\u7ba1\u7406\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0Log4J BOM\u4f9d\u8d56\u9879:<\/p>\n

implementation(platform(\"org.apache.logging.log4j:log4j-bom:2.15.0\")) <\/pre>\n

\u201c\u4e07\u91d1\u6cb9\u201d\u7684\u65b9\u6cd5\u662f\u58f0\u660e\u4e00\u4e2aGradle\u7684resolutionStrategy:<\/p>\n

configurations.all { \r\n resolutionStrategy.eachDependency { DependencyResolveDetails details -> \r\n  if (details.requested.group == 'org.apache.logging.log4j') { \r\n   details.useVersion '2.15.0' \r\n  } \r\n } \r\n} <\/pre>\n

\u4e0a\u9762\u4e09\u79cd\u65b9\u6cd5\u65e0\u8bba\u4f60\u4f7f\u7528\u54ea\u79cd\uff0c\u5b89\u5168\u8d77\u89c1\u90fd\u9700\u8981\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u8fdb\u884c\u68c0\u67e5\u786e\u8ba4\uff1a<\/p>\n

\/gradlew dependencyInsight --dependency log4j-core <\/pre>\n
\u6f0f\u6d1e\u6f14\u793a<\/strong><\/div>\n

\u6f0f\u6d1e\u653b\u51fb\u7684\u6f14\u793a\u4ee3\u7801\uff0c\u6211\u5c06\u5728\u5468\u4e00\u901a\u8fc7\u516c\u4f17\u53f7\u6587\u7ae0\u8fdb\u884c\u8be6\u7ec6\u8bb2\u89e3\uff0c\u8bf7\u6301\u7eed\u5173\u6ce8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Spring Boot\u9ed8\u8ba4\u65e5\u5fd7\u7ec4\u4ef6\u662flogback\uff0c\u5f00\u53d1\u8005\u901a\u8fc7\u65e5\u5fd7\u95e8\u9762Slf4j\u8fdb\u884c\u96c6\u6210\u5bf9\u63a5\u3002Spring B […]<\/p>\n","protected":false},"author":1920,"featured_media":208819,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-232567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/232567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=232567"}],"version-history":[{"count":11,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/232567\/revisions"}],"predecessor-version":[{"id":232773,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/232567\/revisions\/232773"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/208819"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=232567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=232567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=232567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}