{"id":54,"date":"2021-01-13T09:11:50","date_gmt":"2021-01-13T01:11:50","guid":{"rendered":"http:\/\/mengstudy.com\/?p=54"},"modified":"2021-01-15T09:03:11","modified_gmt":"2021-01-15T01:03:11","slug":"springboot%e6%97%a5%e5%bf%97%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fugary.com\/?p=54","title":{"rendered":"SpringBoot\u65e5\u5fd7\u914d\u7f6e"},"content":{"rendered":"<h2>SpringBoot\u65e5\u5fd7\u914d\u7f6e<\/h2>\n<p>\u5728\u524d\u4e00\u4e2a\u9879\u76eesimple-boot-demo\u9879\u76ee\u4e2d\u914d\u7f6e\u65e5\u5fd7\u76f8\u5173\u3002<\/p>\n<p>SpringBoot\u65e5\u5fd7\u4f9d\u8d56\u4e3a\uff1a<code>spring-boot-starter-logging<\/code>\uff0c\u4e0d\u8fc7\u5bf9\u4e8eweb\u9879\u76ee\uff0c\u5df2\u7ecf\u5f15\u5165\u4e86<code>spring-boot-starter-web<\/code>,\u9ed8\u8ba4\u5df2\u7ecf\u81ea\u5e26\u4e86\u65e5\u5fd7\u7684<code>spring-boot-starter-logging<\/code>\uff0c\u5df2\u7ecf\u9ed8\u8ba4\u5f15\u5165\u4e86<code>logback<\/code>\u5b9e\u73b0\uff0c\u53ea\u9700\u8981\u914d\u7f6eapplication.yml\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;ch.qos.logback&lt;\/groupId&gt;\n    &lt;artifactId&gt;logback-classic&lt;\/artifactId&gt;\n    &lt;version&gt;${logback.version}&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>Spring\u5185\u90e8\u4f7f\u7528<a href=\"https:\/\/commons.apache.org\/logging\">Commons Logging<\/a>\uff0cSpringBoot\u9879\u76ee\u65e5\u5fd7\u5b9e\u73b0\u53ef\u4ee5\u7528log4j2\uff0clogback\uff0cJDK (Java Util Logging)\u7b49\u3002<\/p>\n<p>\u53c2\u8003\u6587\u6863\uff1a<\/p>\n<p><a href=\"https:\/\/docs.spring.io\/spring-boot\/docs\/2.2.6.RELEASE\/reference\/html\/howto.html#howto-logging\">https:\/\/docs.spring.io\/spring-boot\/docs\/2.2.6.RELEASE\/reference\/html\/howto.html#howto-logging<\/a><\/p>\n<p><a href=\"https:\/\/docs.spring.io\/spring-boot\/docs\/2.2.6.RELEASE\/reference\/html\/spring-boot-features.html#boot-features-logging\">https:\/\/docs.spring.io\/spring-boot\/docs\/2.2.6.RELEASE\/reference\/html\/spring-boot-features.html#boot-features-logging<\/a><\/p>\n<h3>\u4f7f\u7528\u9ed8\u8ba4logback<\/h3>\n<h4>\u914d\u7f6e\u65e5\u5fd7\u7ea7\u522b<\/h4>\n<p>level\u662f\u4e2amap\uff0c\u914d\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u540d\u79f0\u7684\u7ea7\u522b\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u90fd\u662f\u914d\u7f6e\u4e9b\u5305\u540d\u79f0\u5c31\u53ef\u4ee5\u4e86\uff0c\u56e0\u4e3a\u4e00\u822c\u90fd\u662f\u7528\u7c7b\u540d\u4f5c\u4e3a\u65e5\u5fd7\u540d\u79f0\uff0c\u5e38\u89c1\u5199\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">private static final Logger logger = LoggerFactory.getLogger(SomeClass.class);<\/code><\/pre>\n<pre><code class=\"language-yaml\">logging:\n  level: # level\u662f\u4e2amap\uff0c\u914d\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u540d\u79f0\u7684\u7ea7\u522b\n    root: error\n    org.springframework: debug<\/code><\/pre>\n<h4>\u914d\u7f6e\u6587\u4ef6\u65e5\u5fd7<\/h4>\n<p>\u9ed8\u8ba4\u914d\u7f6e\u4e0b\u53ea\u4f1a\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u65e5\u5fd7\uff0c\u6ca1\u6709\u6587\u4ef6\u65e5\u5fd7\uff0c\u53ef\u4ee5\u914d\u7f6e\u6587\u4ef6\u540d\u6765\u63a7\u5236\u6587\u4ef6\u8f93\u51fa\u3002<\/p>\n<p>\u65e5\u5fd7\u6587\u4ef6\u914d\u7f6e\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\"><code>logging.file.name<\/code><\/th>\n<th style=\"text-align: left;\"><code>logging.file.path<\/code><\/th>\n<th style=\"text-align: left;\">\u793a\u4f8b<\/th>\n<th style=\"text-align: left;\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><em>(none)<\/em><\/td>\n<td style=\"text-align: left;\"><em>(none)<\/em><\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">\u53ea\u6709\u63a7\u5236\u53f0\u65e5\u5fd7<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u6307\u5b9a\u6587\u4ef6\u540d<\/td>\n<td style=\"text-align: left;\"><em>(none)<\/em><\/td>\n<td style=\"text-align: left;\"><code>my.log<\/code><\/td>\n<td style=\"text-align: left;\">\u6307\u5b9a\u6587\u4ef6\u540d\uff0c\u76f8\u5bf9\u8def\u5f84\u7684\u8bdd\u5728\u5f53\u524d\u76ee\u5f55\uff0c\u4e5f\u53ef\u4ee5\u662f\u7edd\u5bf9\u8def\u5f84<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><em>(none)<\/em><\/td>\n<td style=\"text-align: left;\">\u6307\u5b9a\u6587\u4ef6\u76ee\u5f55<\/td>\n<td style=\"text-align: left;\"><code>\/var\/log<\/code><\/td>\n<td style=\"text-align: left;\">\u6307\u5b9a\u76f8\u5bf9\u6216\u7edd\u5bf9\u76ee\u5f55\uff0c\u9ed8\u8ba4\u6587\u4ef6\u540d\u4e3a\uff1a<code>spring.log<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u65e5\u5fd7\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-yaml\">logging:\n  file:\n    name: C:\/simple-logs\/simple-boot-demo.log<\/code><\/pre>\n<h4>\u6587\u4ef6\u65e5\u5fd7\u8be6\u60c5<\/h4>\n<p>\u53e6\u5916\u4e00\u4e9b\u6587\u4ef6\u65e5\u5fd7\u914d\u7f6e\u5982\u4e0b\uff0c\u4e3b\u8981\u914d\u7f6e\u6587\u4ef6\u5927\u5c0f\u5206\u9694\u3001\u5386\u53f2\u8bb0\u5f55\u3001\u8f93\u51fa\u683c\u5f0f\u7b49\uff0c\u8fd9\u4e9b\u914d\u7f6e\u5728IDE\u4e2d\u90fd\u6709\u63d0\u793a\u8f93\u5165\uff1a<\/p>\n<pre><code class=\"language-yaml\">logging:\n  file:\n    name: C:\/simple-logs\/simple-boot-demo.log\n    max-size: 10MB # \u5355\u4e2a\u6587\u4ef6\u5927\u5c0f\n    max-history: 7 # \u6700\u5927\u5386\u53f2\u5929\u65707\u5929\n    total-size-cap: 0B # \u603b\u6587\u4ef6\u5927\u5c0f\u9650\u52360MB\uff0c\u65e0\u9650\u5236\n  patten:\n    file: %d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}\n  level: # level\u662f\u4e2amap\uff0c\u914d\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u540d\u79f0\u7684\u7ea7\u522b\n    root: error\n    org.springframework: debug<\/code><\/pre>\n<p><strong>\u6ce8\u610f\uff1a\u8fd9\u4e9b\u914d\u7f6e\u5728\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u914d\u7f6e\u6587\u4ef6<code>logging.config=classpath:logback-spring.xml<\/code>\u65f6\uff0c\u5982\u679cinclude\u5f15\u5165sping-boot\u9879\u76ee\u4e2d\u7684logback\u914d\u7f6exml\u7247\u6bb5\uff0c\u8fd9\u4e9bapplication.yml\u4e2d\u7684\u914d\u7f6e\u4e5f\u662f\u6709\u6548\u7684\uff0c\u800c\u4e14level\u914d\u7f6e\u8fd8\u662f\u4f18\u5148\u4e8e\u914d\u7f6e\u6587\u4ef6<\/strong><\/p>\n<h4>\u65e5\u5fd7\u5206\u7ec4\u914d\u7f6e<\/h4>\n<p>\u65e5\u5fd7\u53ef\u4ee5\u5206\u7ec4\u914d\u7f6e\uff0c\u9488\u5bf9\u90e8\u5206\u65e5\u5fd7\u540d\u79f0\u5b9a\u4e49\u4e00\u4e2a\u5206\u7ec4\uff0c\u53ef\u4ee5\u7edf\u4e00\u914d\u7f6e\u6b64\u5206\u7ec4\u7684\u7ea7\u522b\uff1a<\/p>\n<pre><code class=\"language-properties\">logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat\nlogging.level.tomcat=DEBUG<\/code><\/pre>\n<p>SpringBoot\u5185\u7f6e\u7684\u65e5\u5fd7\u5206\u7ec4\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Name<\/th>\n<th style=\"text-align: left;\">Loggers<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">web<\/td>\n<td style=\"text-align: left;\"><code>org.springframework.core.codec<\/code>, <code>org.springframework.http<\/code>, <code>org.springframework.web<\/code>, <code>org.springframework.boot.actuate.endpoint.web<\/code>, <code>org.springframework.boot.web.servlet.ServletContextInitializerBeans<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">sql<\/td>\n<td style=\"text-align: left;\"><code>org.springframework.jdbc.core<\/code>, <code>org.hibernate.SQL<\/code>, <code>org.jooq.tools.LoggerListener<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4f7f\u7528\u81ea\u5b9a\u4e49logback<\/h3>\n<p>SpringBoot\u5982\u679c\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684logback\u4f5c\u4e3a\u65e5\u5fd7\u5b9e\u73b0\uff0c\u9700\u8981\u7f16\u5199logback.xml\uff0c\u4f46\u662f\u5f15\u5165logback\u4e4b\u540e\uff0clogback\u4f1a\u81ea\u52a8\u67e5\u627elogback.xml\uff0c\u56e0\u6b64\u4e0d\u80fd\u628a\u540d\u5b57\u5b9a\u4e49\u4e3alogback.xml\uff0c\u5426\u5219\u53ef\u80fd\u65e9\u4e8eSpringBoot\u521d\u59cb\u5316\uff0c\u9020\u6210\u4e00\u4e9b\u914d\u7f6e\u65e0\u6548\u95ee\u9898\u3002<\/p>\n<h4>\u81ea\u5b9a\u4e49\u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>\u4f7f\u7528<code>logback-spring.xml<\/code>\u6216\u8005\u4f7f\u7528<code>loging.config<\/code>\u6765\u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\uff08\u9ed8\u8ba4\u540d\u5b57\u4e3a<code>logback-spring.xml<\/code>\u7684\u6587\u4ef6springboot\u4f1a\u81ea\u52a8\u83b7\u53d6\u5230\uff0c\u81ea\u5b9a\u4e49\u540d\u5b57\u9700\u8981\u624b\u52a8\u914d\u7f6e\uff09\uff1a<\/p>\n<pre><code class=\"language-yaml\">logging:\n  config: classpath:demo-logback.xml<\/code><\/pre>\n<h4>logback\u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>\u5728spring-boot\u9879\u76ee\u7684jar\u5305\u4e2d\uff0cspring\u4e3a\u4e86\u7b80\u5316\u914d\u7f6e\uff0c\u5df2\u7ecf\u5728\u9879\u76ee\u4e2d\u5185\u7f6e\u4e86\u4e00\u4e9blogback\u7684\u914d\u7f6e\u7247\u6bb5\uff0c\u65b9\u4fbf\u4f7f\u7528include\u65b9\u5f0f\u5f15\u5165\uff0c\u6253\u5f00\u53ef\u4ee5\u770b\u5230\u5185\u5bb9\u662f\u7528<code>&lt;included&gt;&lt;\/include&gt;<\/code>\u5305\u88f9\uff0c\u56e0\u6b64\u81ea\u5b9a\u4e49\u914d\u7f6e\u6587\u4ef6<code>logback.xml<\/code>\u7684\u65f6\u5019\u76f4\u63a5include\u5373\u53ef\u3002<\/p>\n<p>\u914d\u7f6e\u7247\u6bb5\u8def\u5f84\uff1a<code>org\/springframework\/boot\/logging\/logback\/<\/code><\/p>\n<table>\n<thead>\n<tr>\n<th>\u6587\u4ef6\u540d<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>defaults.xml<\/td>\n<td>\u9ed8\u8ba4\u7684\u4e00\u4e9b\u65e5\u5fd7\u914d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>console-appender.xml<\/td>\n<td>\u63a7\u5236\u53f0\u8f93\u51faappender\u7b49<\/td>\n<\/tr>\n<tr>\n<td>file-appender.xml<\/td>\n<td>\u6587\u4ef6\u8f93\u51faappender\u7b49<\/td>\n<\/tr>\n<tr>\n<td>base.xml<\/td>\n<td>\u57fa\u7840\u914d\u7f6e\uff0c\u5305\u542b\u4e0a\u9762\u7684\u4e09\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u517c\u5bb9\u8001\u7248\u672cspringboot\u7684\u914d\u7f6e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5178\u578b<code>logback.xml<\/code>\u914d\u7f6e\u6587\u4ef6\u5982\u4e0b\uff0c\u5176\u4e2d\u5f15\u7528\u4e86spring-boot\u63d0\u4f9b\u7684xml\u7247\u6bb5\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;configuration&gt;\n    &lt;include resource=&quot;org\/springframework\/boot\/logging\/logback\/defaults.xml&quot; \/&gt;\n    &lt;include resource=&quot;org\/springframework\/boot\/logging\/logback\/console-appender.xml&quot; \/&gt;\n    &lt;include resource=&quot;org\/springframework\/boot\/logging\/logback\/file-appender.xml&quot; \/&gt;\n    &lt;root level=&quot;debug&quot;&gt;\n        &lt;appender-ref ref=&quot;CONSOLE&quot; \/&gt;\n        &lt;appender-ref ref=&quot;FILE&quot; \/&gt;\n    &lt;\/root&gt;\n    &lt;logger name=&quot;org.springframework.web&quot; level=&quot;ERROR&quot;\/&gt;\n&lt;\/configuration&gt;<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u914d\u7f6e\u662f\u6587\u4ef6\u53d6\u540d\u4e3a<code>demo-logback.xml<\/code>\uff0c\u65b9\u4fbf\u5f15\u5165\u3002<\/p>\n<p>\u914d\u7f6eapplication.yml\uff1a<\/p>\n<pre><code class=\"language-yaml\">logging:\n  file:\n    name: C:\/simple-logs\/simple-boot-demo.log\n    max-size: 100KB # \u53ef\u4ee5\u6539\u5927\u5c0f\u6d4b\u8bd5\u751f\u6210\u591a\u4e2a\u5e26\u65f6\u95f4\u4fe1\u606f\u7684\u6587\u4ef6\n    max-history: 7\n    total-size-cap: 0MB\n  config: classpath:demo-logback.xml<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<ol>\n<li>SpringBoot\u9879\u76ee\u5728\u901a\u5e38\u60c5\u51b5\u4e0b\u4e0d\u9700\u8981\u7528\u81ea\u5b9a\u4e49\u7684<code>logback.xml<\/code>\u914d\u7f6e\uff0c\u9664\u975e\u8fd8\u9700\u8981\u4e3a\u65e5\u5fd7\u505a\u4e00\u4e9b\u5b9a\u5236\uff08\u5982\u9700\u8981append\u5230logstash\u7b49\uff09\uff0c\u5426\u5219\u76f4\u63a5\u4f7f\u7528<code>application.yml<\/code>\u914d\u7f6e\u5c31\u8db3\u591f\u4e86\u3002<\/li>\n<li>\u81ea\u5b9a\u4e49<code>logback.xml<\/code>\uff0c\u6700\u597d\u81ea\u5b9a\u4e49\u4e00\u4e2a\u540d\u79f0\u6216\u8005\u4f7f\u7528<code>logback-spring.xml<\/code><\/li>\n<li>\u4f7f\u7528include\u65b9\u5f0f\u5f15\u5165spring-boot.jar\u4e2d\u7684xml\u7684\u65f6\u5019\uff0capplication.yml\u4e2d\u5f88\u591a\u914d\u7f6e\u4e5f\u662f\u6709\u6548\u7684\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>SpringBoot\u65e5\u5fd7\u914d\u7f6e \u5728\u524d\u4e00\u4e2a\u9879\u76eesimple-boot-demo\u9879\u76ee\u4e2d\u914d\u7f6e\u65e5\u5fd7\u76f8\u5173\u3002 SpringB [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[13,11],"_links":{"self":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/54"}],"collection":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=54"}],"version-history":[{"count":2,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}