{"id":26,"date":"2019-01-09T22:37:28","date_gmt":"2019-01-09T14:37:28","guid":{"rendered":"http:\/\/www.sttxx.com\/?p=26"},"modified":"2019-01-09T22:39:33","modified_gmt":"2019-01-09T14:39:33","slug":"peizhispring-bootrizhi","status":"publish","type":"post","link":"http:\/\/www.sttxx.com\/index.php\/2019\/01\/09\/peizhispring-bootrizhi\/","title":{"rendered":"\u914d\u7f6eSpring Boot\u65e5\u5fd7"},"content":{"rendered":"\n<h4 id=\"%E5%85%B3%E4%BA%8E%E6%97%A5%E5%BF%97\">\u5173\u4e8e\u65e5\u5fd7<\/h4>\n\n\n\n<p>Spring Boot\u6ca1\u6709\u5f3a\u5236\u65e5\u5fd7\u8bb0\u5f55\u4f9d\u8d56\u6027\uff0c\u9664\u4e86Commons Logging API\uff0c\u8fd9\u901a\u5e38\u7531Spring Framework\u7684<strong>spring-jcl<\/strong>\u6a21\u5757\u63d0\u4f9b\u3002\u8981\u4f7f\u7528<a href=\"https:\/\/logback.qos.ch\/\">Logback<\/a>\uff0c\u60a8\u9700\u8981\u5728\u7c7b\u8def\u5f84\u4e2d\u5305\u542b\u5b83\u548c<strong>spring-jcl<\/strong>\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u542f\u52a8\u5668\uff0c\u5b83\u4eec\u90fd\u4f9d\u8d56\u4e8e<strong>spring-boot-starter-logging<\/strong>\u3002\u5bf9\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\uff0c\u60a8\u53ea\u9700\u8981<strong>spring-boot-starter-web<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4f9d\u8d56\u4e8e\u65e5\u5fd7\u8bb0\u5f55\u542f\u52a8\u5668\u3002\u5982\u679c\u60a8\u4f7f\u7528Maven\uff0c\u4ee5\u4e0b\u4f9d\u8d56\u9879\u4f1a\u4e3a\u60a8\u6dfb\u52a0\u65e5\u5fd7\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;dependency&gt; <br>         &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; <br>        &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt; <br>&lt;\/dependency&gt;<\/pre>\n\n\n\n<p>Spring Boot\u6709\u4e00\u4e2a<strong>LoggingSyste<\/strong>m\u62bd\u8c61\uff0c\u5b83\u8bd5\u56fe\u6839\u636e\u7c7b\u8def\u5f84\u7684\u5185\u5bb9\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u3002\u5982\u679cLogback\u53ef\u7528\uff0c\u5219\u5b83\u662f\u7b2c\u4e00\u9009\u62e9\u3002<br>\u5982\u679c\u60a8\u9700\u8981\u5bf9\u8bb0\u5f55\u8fdb\u884c\u7684\u552f\u4e00\u66f4\u6539\u662f\u8bbe\u7f6e\u5404\u79cd\u8bb0\u5f55\u5668\u7684\u7ea7\u522b\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u201clogging.level\u201d\u524d\u7f00\u5728<strong>application.properties<\/strong>\u4e2d\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">logging.level.org.springframework.web=DEBUG logging.level.org.hibernate=ERROR<\/pre>\n\n\n\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u201clogging.file\u201d\u8bbe\u7f6e\u8981\u5199\u5165\u65e5\u5fd7\u7684\u6587\u4ef6\u7684\u4f4d\u7f6e\uff08\u9664\u63a7\u5236\u53f0\u5916\uff09\u3002<br>\u8981\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7cfb\u7edf\u7684\u66f4\u7ec6\u7c92\u5ea6\u8bbe\u7f6e\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u76f8\u5173LoggingSystem\u652f\u6301\u7684\u672c\u673a\u914d\u7f6e\u683c\u5f0f\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cSpring Boot\u4ece\u7cfb\u7edf\u7684\u9ed8\u8ba4\u4f4d\u7f6e\uff08\u4f8b\u5982Logback\u7684<strong>classpath\uff1alogback.xml<\/strong>\uff09\u4e2d\u9009\u62e9\u672c\u673a\u914d\u7f6e\uff0c\u4f46\u60a8\u53ef\u4ee5\u4f7f\u7528\u201clogging.config\u201d\u5c5e\u6027\u8bbe\u7f6e\u914d\u7f6e\u6587\u4ef6\u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<h4 id=\"%E9%85%8D%E7%BD%AE-logback\">\u914d\u7f6e Logback<\/h4>\n\n\n\n<p>\u5982\u679c\u5c06logback.xml\u653e\u5728\u7c7b\u8def\u5f84\u7684\u6839\u76ee\u5f55\u4e2d\uff0c\u5219\u4ece\u90a3\u91cc\uff08\u6216\u4ecelogback-spring.xml\u4e2d\u83b7\u53d6\uff09\u4ee5\u5229\u7528Boot\u63d0\u4f9b\u7684\u6a21\u677f\u529f\u80fd\u3002Spring Boot\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u7684\u57fa\u672c\u914d\u7f6e\uff0c\u5982\u679c\u8981\u8bbe\u7f6e\u7ea7\u522b\uff0c\u53ef\u4ee5\u5305\u62ec\u8be5\u914d\u7f6e\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; <br>        &lt;configuration&gt; <br>        &lt;include resource=\"org\/springframework\/boot\/logging\/logback\/base.xml\"\/&gt; <br>        &lt;logger name=\"org.springframework.web\" level=\"DEBUG\"\/&gt; &lt;\/configuration&gt;<\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u770b\u4e00\u4e0bspring-boot jar\u4e2d\u7684base.xml\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u4f7f\u7528\u4e86LoggingSystem\u4e3a\u4f60\u521b\u5efa\u7684\u4e00\u4e9b\u6709\u7528\u7684System\u5c5e\u6027\uff1a<\/p>\n\n\n\n<ul><li>$ {PID}\uff1a\u5f53\u524d\u8fdb\u7a0bID\u3002<\/li><li>$ {LOG_FILE}\uff1a\u662f\u5426\u5728Boot\u7684\u5916\u90e8\u914d\u7f6e\u4e2d\u8bbe\u7f6e\u4e86logging.file\u3002<\/li><li>$ {LOG_PATH}\uff1a\u662f\u5426\u5728Boot\u7684\u5916\u90e8\u914d\u7f6e\u4e2d\u8bbe\u7f6e\u4e86logging.path\uff08\u8868\u793a\u65e5\u5fd7\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff09\u3002<\/li><li>$ {LOG_EXCEPTION_CONVERSION_WORD}\uff1a\u662f\u5426\u5728Boot\u7684\u5916\u90e8\u914d\u7f6e\u4e2d\u8bbe\u7f6e\u4e86logging.exception-conversion-word\u3002<\/li><\/ul>\n\n\n\n<p>Spring Boot\u8fd8\u901a\u8fc7\u4f7f\u7528\u81ea\u5b9a\u4e49Logback\u8f6c\u6362\u5668\u5728\u63a7\u5236\u53f0\u4e0a\uff08\u4f46\u4e0d\u5728\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff09\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6f02\u4eae\u7684<em>ANSI<\/em>\u989c\u8272\u7ec8\u7aef\u8f93\u51fa\u3002 \u6709\u5173\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u9ed8\u8ba4\u7684<strong>base.xml<\/strong>\u914d\u7f6e\u3002<\/p>\n\n\n\n<p>\u5982\u679cGroovy\u5728\u7c7b\u8def\u5f84\u4e0a\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u4f7f\u7528<strong>logback.groovy<\/strong>\u914d\u7f6eLogback\u3002 \u5982\u679c\u5b58\u5728\uff0c\u5219\u4f18\u5148\u8003\u8651\u6b64\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<h5 id=\"%E9%85%8D%E7%BD%AElogback%E6%97%A5%E5%BF%97%E6%96%87%E4%BB%B6%E8%BE%93%E5%87%BA\">\u914d\u7f6eLogback\u65e5\u5fd7\u6587\u4ef6\u8f93\u51fa<\/h5>\n\n\n\n<p>\u5982\u679c\u8981\u7981\u7528\u63a7\u5236\u53f0\u65e5\u5fd7\u8bb0\u5f55\u5e76\u4ec5\u5c06\u8f93\u51fa\u5199\u5165\u6587\u4ef6\uff0c\u4f60\u9700\u8981\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684logback-spring.xml\u6765\u5bfc\u5165file-appender.xml\u800c\u4e0d\u662fconsole-appender.xml\uff0c\u5982\u4e0b\u4f8b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?> <br>        &lt;configuration> <br>                &lt;include resource=\"org\/springframework\/boot\/logging\/logback\/defaults.xml\" \/>                         &lt;property name=\"LOG_FILE\" value=\"${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-\/tmp}}\/}spring.log}\"\/> <br>                &lt;include resource=\"org\/springframework\/boot\/logging\/logback\/file-appender.xml\" \/> <br>        &lt;root level=\"INFO\"> <br>                &lt;appender-ref ref=\"FILE\" \/> <br>        &lt;\/root> <br>&lt;\/configuration><\/pre>\n\n\n\n<p>\u60a8\u8fd8\u9700\u8981\u5c06logging.file\u6dfb\u52a0\u5230application.properties\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">logging.file=myapplication.log<\/pre>\n\n\n\n<h4 id=\"%E9%85%8D%E7%BD%AE-log4j\">\u914d\u7f6e Log4j<\/h4>\n\n\n\n<p>Spring Boot\u652f\u6301Log4j 2\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\u914d\u7f6e\uff08\u5982\u679c\u5b83\u5728\u7c7b\u8def\u5f84\u4e0a\uff09\u3002 \u5982\u679c\u4f7f\u7528\u542f\u52a8\u5668\u6765\u7ec4\u88c5\u4f9d\u8d56\u9879\uff0c\u5219\u5fc5\u987b\u6392\u9664Logback\uff0c\u7136\u540e\u5305\u542blog4j 2\u3002 \u5982\u679c\u4f60\u4e0d\u4f7f\u7528\u542f\u52a8\u5668\uff0c\u9664\u4e86Log4j 2\u4e4b\u5916\uff0c\u8fd8\u9700\u8981\u63d0\u4f9b\uff08\u81f3\u5c11\uff09spring-jcl\u3002<\/p>\n\n\n\n<p>\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u53ef\u80fd\u662f\u901a\u8fc7\u542f\u52a8\u5668\uff0c\u5373\u4fbf\u9700\u8981\u6392\u9664Logback\u3002\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u5982\u4f55\u5728Maven\u4e2d\u8bbe\u7f6e\u542f\u52a8\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;dependency&gt; <br>        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; <br>        &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt; <br>&lt;\/dependency&gt; <br>&lt;dependency&gt; <br>        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; <br>        &lt;artifactId&gt;spring-boot-starter&lt;\/artifactId&gt; <br>        &lt;exclusions&gt; <br>                &lt;exclusion&gt;         <br>                        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; <br>                        &lt;artifactId&gt;spring-boot-starter-logging&lt;\/artifactId&gt; <br>                &lt;\/exclusion&gt; <br>        &lt;\/exclusions&gt; <br>&lt;\/dependency&gt; <br>&lt;dependency&gt; <br>        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; <br>        &lt;artifactId&gt;spring-boot-starter-log4j2&lt;\/artifactId&gt; <br>&lt;\/dependency&gt;<\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u4e86\u5728Gradle\u4e2d\u8bbe\u7f6e\u542f\u52a8\u5668\u7684\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">dependencies { <br>        compile 'org.springframework.boot:spring-boot-starter-web' <br>        compile 'org.springframework.boot:spring-boot-starter-log4j2'<br>} <br>configurations { <br>        all { <br>                exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' <br>        }<br> }<\/pre>\n\n\n\n<h6 id=\"log4j%E5%90%AF%E5%8A%A8%E7%A8%8B%E5%BA%8F%E5%B0%86%E5%B8%B8%E8%A7%81%E6%97%A5%E5%BF%97%E8%AE%B0%E5%BD%95%E8%A6%81%E6%B1%82%E7%9A%84%E4%BE%9D%E8%B5%96%E5%85%B3%E7%B3%BB%E8%81%9A%E9%9B%86%E5%9C%A8%E4%B8%80%E8%B5%B7%EF%BC%88%E4%BE%8B%E5%A6%82%E8%AE%A9tomcat%E4%BD%BF%E7%94%A8java.util.logging%E4%BD%86%E4%BD%BF%E7%94%A8log4j-2%E9%85%8D%E7%BD%AE%E8%BE%93%E5%87%BA%EF%BC%89%E3%80%82-%E6%9C%89%E5%85%B3%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF%EF%BC%8C%E8%AF%B7%E5%8F%82%E9%98%85actuator-log4j-2%E4%BA%8B%E4%BE%8B%E5%B9%B6%E6%9F%A5%E7%9C%8B%E5%85%B6%E5%AE%9E%E9%99%85%E6%93%8D%E4%BD%9C%E3%80%82\">Log4j\u542f\u52a8\u7a0b\u5e8f\u5c06\u5e38\u89c1\u65e5\u5fd7\u8bb0\u5f55\u8981\u6c42\u7684\u4f9d\u8d56\u5173\u7cfb\u805a\u96c6\u5728\u4e00\u8d77\uff08\u4f8b\u5982\u8ba9Tomcat\u4f7f\u7528java.util.logging\u4f46\u4f7f\u7528Log4j 2\u914d\u7f6e\u8f93\u51fa\uff09\u3002 \u6709\u5173\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605<a href=\"https:\/\/github.com\/spring-projects\/spring-boot\/tree\/v2.1.0.RELEASE\/spring-boot-samples\/spring-boot-sample-actuator-log4j2\">Actuator Log4j 2<\/a>\u4e8b\u4f8b\u5e76\u67e5\u770b\u5176\u5b9e\u9645\u64cd\u4f5c\u3002<\/h6>\n\n\n\n<h6 id=\"%E8%A6%81%E7%A1%AE%E4%BF%9D%E4%BD%BF%E7%94%A8java.util.logging%E6%89%A7%E8%A1%8C%E7%9A%84%E8%B0%83%E8%AF%95%E6%97%A5%E5%BF%97%E8%AE%B0%E5%BD%95%E8%B7%AF%E7%94%B1%E5%88%B0log4j-2%EF%BC%8C%E8%AF%B7%E9%80%9A%E8%BF%87%E5%B0%86java.util.logging.manager%E7%B3%BB%E7%BB%9F%E5%B1%9E%E6%80%A7%E8%AE%BE%E7%BD%AE%E4%B8%BAorg.apache.logging.log4j.jul.logmanager%E6%9D%A5%E9%85%8D%E7%BD%AE%E5%85%B6jdk%E6%97%A5%E5%BF%97%E8%AE%B0%E5%BD%95%E9%80%82%E9%85%8D%E5%99%A8%E3%80%82\">\u8981\u786e\u4fdd\u4f7f\u7528java.util.logging\u6267\u884c\u7684\u8c03\u8bd5\u65e5\u5fd7\u8bb0\u5f55\u8def\u7531\u5230Log4j 2\uff0c\u8bf7\u901a\u8fc7\u5c06java.util.logging.manager\u7cfb\u7edf\u5c5e\u6027\u8bbe\u7f6e\u4e3aorg.apache.logging.log4j.jul.LogManager\u6765\u914d\u7f6e\u5176<a href=\"https:\/\/logging.apache.org\/log4j\/2.0\/log4j-jul\/index.html\">JDK\u65e5\u5fd7\u8bb0\u5f55\u9002\u914d\u5668<\/a>\u3002<\/h6>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e\u65e5\u5fd7 Spring Boot\u6ca1\u6709\u5f3a\u5236\u65e5\u5fd7\u8bb0\u5f55\u4f9d\u8d56\u6027\uff0c\u9664\u4e86Commons Lo<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[3,8],"_links":{"self":[{"href":"http:\/\/www.sttxx.com\/index.php\/wp-json\/wp\/v2\/posts\/26"}],"collection":[{"href":"http:\/\/www.sttxx.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sttxx.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sttxx.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sttxx.com\/index.php\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":3,"href":"http:\/\/www.sttxx.com\/index.php\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":29,"href":"http:\/\/www.sttxx.com\/index.php\/wp-json\/wp\/v2\/posts\/26\/revisions\/29"}],"wp:attachment":[{"href":"http:\/\/www.sttxx.com\/index.php\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sttxx.com\/index.php\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sttxx.com\/index.php\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}