Spring Boot ã§ãã°ã¤ã³ç»é¢ + ä¸è¦§ç»é¢ + ç»é²ç»é¢ã® Webã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( logback.xmlã®è¨å® )
æ¦è¦
Spring Boot ã§ãã°ã¤ã³ç»é¢ + ä¸è¦§ç»é¢ + ç»é²ç»é¢ã® Webã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( æ°ã«ãªã£ãç¹ãä¿®æ£ï¼ ) ã®ç¶ãã§ãã
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å 容ã§ãã
- ãç»é²ç»é¢ã® surfaceArea 㨠population ã® Bean Validation ãä¿®æ£ãã¦ãä¿åã§ããªãå¤ã¯å ¥åãã§ãã¯ã¨ã©ã¼ã«ãªãããã«ãããã¯å¯¾å¿ããªããã¨ã«ãã¾ãããå°æ°ç¹ããã®æ°å¤ããã§ãã¯ãããµã³ãã«ãç¡ããªããã¨ã«æ°ã¥ããã®ã§ã
ã½ããã¦ã§ã¢ä¸è¦§
åèã«ãããµã¤ã
-
Spring Boot Reference Guide - 66. Logging
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html -
The logback manual - Chapter 6: Layouts
http://logback.qos.ch/manual/layouts.html java.io.tmpdirã®OSã«ããå¤ã®å·®ç°ã«ã¤ãã¦
http://qiita.com/asadasan/items/e541abb6024996488580ITå¿åé²ã»ã¡ã¢æ¸ãã¨æ¥è¨ - slf4j,logback~javaã®ãã®ã³ã°ãã¼ã«
http://ameblo.jp/oregano-blog/entry-11586057084.htmlclash_m45ã®éçºããã° - logbackã§æ¥æ¬¡ãã°ãã¼ãã¼ã·ã§ã³ãã
http://clash-m45.hatenablog.com/entry/20110824/1314168361TERASOLUNA Server Framework for Java (5.x) Development Guideline 5.0.0.RELEASE documentation - 5.6. ãã®ã³ã°
http://terasolunaorg.github.io/guideline/5.0.0.RELEASE/ja/ArchitectureInDetail/Logging.htmlintra-mart Accel Platform / ãã° ä»æ§æ¸
http://www.intra-mart.jp/download/product/iap/im_core/im_log_specification/index.html#
æé
Spring Boot ã®ãã°åºåã®ããã©ã«ãè¨å®ã¯ã©ããªã£ã¦ããã®ãï¼
Tomcat ãèµ·åããã¨ãããªãã®ãã©ã¼ãããã®ãã°ã IntelliJ IDEA ã®ã³ã³ã½ã¼ã«ã«åºåããã¾ãã®ã§ãã¾ã㯠Spring Boot ã®ããã©ã«ãè¨å®ãã©ããªã£ã¦ããã®ã調ã¹ã¦ã¿ã¾ãã
logback.xml ã«
<include resource="org/springframework/boot/logging/logback/base.xml"/>
ã¨æ¸ãã¦ããã®ã§ããã®ãã¡ã¤ã«ãè¦ã¦ã¿ã¾ãã以ä¸ã® URL 㧠base.xml ã¨ãã®é¢é£ãã¡ã¤ã«ãè¦ããã¾ããorg/springframework/boot/logging/logback/
https://github.com/spring-projects/spring-boot/tree/master/spring-boot/src/main/resources/org/springframework/boot/logging/logback- ã³ã³ã½ã¼ã«ã«åºåããéã®ãã°ãã©ã¼ããã㯠defaults.xml ã®ä¸ã«
<property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex"/>
ã¨å®ç¾©ããã¦ãã¾ããã - root ã® level 㯠base.xml ã« "INFO" ã§å®ç¾©ããã¦ãããã³ã³ã½ã¼ã«ã ãã§ãªããã¡ã¤ã«ã«ãåºåãããããè¨å®ããã¦ãã¾ããã³ã³ã½ã¼ã«ã ãã ã¨æã£ã¦ãã¾ããã
- root 㯠"INFO" ã§å®ç¾©ãã¦ãã©ã¤ãã©ãªåä½ã§ level ã調æ´ããããã®ã ãåå¥ã« defaults.xml ã«å®ç¾©ãã¦ããããã§ãã
- ãã°ãã¡ã¤ã«ã®åºåå ´æã確èªãã¦ã¿ã¾ããåã defaults.xml ã®ä¸ã«
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
ã¨å®ç¾©ããã¦ãããä»ã¯ LOG_FILE ã LOG_PATH ã LOG_TEMP ãè¨å®ãã¦ããªãã®ã§ãjava.io.tmpdir ã«åºåããã¦ããã¨æããã¾ãã - Web 㧠java.io.tmpdir ã®å ´æã確èªãã¦ããè¦ã¦ã¿ãã¨
C:\Users\<ã¦ã¼ã¶å>\AppData\Local\Temp
ã®ä¸ã« spring.log ãåºåããã¦ãã¾ãããããã 10MBåä½ã§ãã¼ãã¼ã·ã§ã³ããã¦ãããspring.log.7 ã¾ã§åºåããã¦ãã¾ãããç¥ããªãéã« 70MB ããã°ãåºåããã¦ããã¨ã¯ããã - ä»ã®è¨å®ãã¡ã¤ã«ãè¦ã¦ã¿ã¾ããã³ã³ã½ã¼ã«åºå㯠console-appender.xml ã«ããã¡ã¤ã«åºå㯠file-appender.xml ã«å®ç¾©ããã¦ãã¾ãã
- console-appender.xml ã¯ã·ã³ãã«ã« UTF-8 ã§åºåããããè¨å®ããã¦ããã ãã§ããã
- file-appender.xml ã«ã¯ãã°ãã¼ãã¼ã·ã§ã³ãããã RollingFileAppender ã§è¨å®ããã¦ãã¾ããã10MB æ¯ã«ãã¼ãã¼ã·ã§ã³ããã®ã¯ããã®ã§ãããæ大æ°ãè¨å®ããã¦ããªãããã
- Windows 㧠Temp ãã©ã«ãã«ãããã¡ã¤ã«ãã©ã®ãããªã¿ã¤ãã³ã°ã§åé¤ããã®ã調ã¹ã¦ã¿ãããããã©ã«ãã§ã¯åé¤ãããªãããã§ãã
- ã³ã³ã½ã¼ã«ã«åºåããéã®ãã°ãã©ã¼ããã㯠defaults.xml ã®ä¸ã«
è¨å®æ¹é
以ä¸ã®æ¹éã§è¨å®ãããã¨ã«ãã¾ãã
- ãã°ãã©ã¼ãããã¯ä»ã®ã¾ã¾ã§ç¹ã«ä¸æºã¯ãªãã®ã§ããã®ã¾ã¾å©ç¨ãããã¨ã«ãã¾ãã
- logback-develop.xml ã¯ã³ã³ã½ã¼ã«åºåã®ã¿ã«ãã¾ãã
- logback-unittest.xml 㯠root ã® level ã® OFF è¨å®ã ãæ®ãã¦ããã以å¤ã®è¨å®ãåé¤ãã¾ãã
- logback-product.xml ã¯ãã¡ã¤ã«åºåã®ã¿ã«ãã¾ã ( Windows ã§ãµã¼ãã¹ã§åããã¤ãããªã®ã§ã³ã³ã½ã¼ã«ã«ã¯åºåãã¾ãã ) ãæ¥åä½ã§ãã¼ãã¼ã·ã§ã³ããã¦ãæ大30æ¥åä¿åãããããã«ãã¾ãã
logback.xml ãè¨å®ãã
IntelliJ IDEA ä¸ã§ 1.0.x-logbackxml ãã©ã³ããä½æãã¾ãã
src/main/resources ã®ä¸ã® logback.xml ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã°ãã¡ã¤ã«ã®åºåå ã«è¨å®ãã C:\webapps\ksbysample-webapp-basic\logs ãã£ã¬ã¯ããªãä½æãã¾ãã
logback-develop.xml ãè¨å®ãã
src/main/resources ã®ä¸ã® logback-develop.xml ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
åä½ç¢ºèªãã¾ããbootRun ã¿ã¹ã¯ãå®è¡ãã¾ãã
- IntelliJ IDEA ã®ã³ã³ã½ã¼ã«ã«ã¯ä»¥åã¨åæ§ã®ãã°ãåºåããã¾ããã
C:\Users\<ã¦ã¼ã¶å>\AppData\Local\Temp
ã®ä¸ã«ã¯ spring.log ãåºåãããªããªãã¾ããããã ããnative-platform2794970538314105909dir ã¨ããgradle139425685678566444.bin ã¨ããtomcat.7446016434940347344.8080ã¨ããåºåããã¦ãã¾ããããã®å¾ãlogback-product.xml ãä¿®æ£ã㦠jar ããèµ·åãã¦åä½ç¢ºèªããæã«ãåºåãããããã§ããã°ãåºåãããªãæ¹æ³ããªãã調æ»ãããã¨æãã¾ãã
Run View 㧠Ctrl+F2 ãæ¼ã㦠Tomcat ãåæ¢ãã¾ãã
logback-unittest.xml ãè¨å®ãã
src/main/resources ã®ä¸ã® logback-unittest.xml ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
åä½ç¢ºèªãã¾ãããRun 'Tests in 'ksbysample...' with Coverageããå®è¡ãã¾ãã
- IntelliJ IDEA ã®ã³ã³ã½ã¼ã«ã«ã¯ä½ãåºåããã¾ããã§ããã
C:\Users\<ã¦ã¼ã¶å>\AppData\Local\Temp
ã®ä¸ã«ã spring.log ã¯åºåããã¦ãã¾ããã§ããããã ã coverage0argsãidea_working_dirs_junit0.tmpãidea_test_0.out ã¨ãããã¡ã¤ã«ãåºåããã¦ãã¾ãã ( ãRun 'Tests in 'ksbysample...' with Coverageãçµäºå¾ãåé¤ãããã«æ®ã£ã¦ãã¾ãã ) ãããã㯠Coverage , JUnit, test é¢é£ã®ãã¡ã¤ã«ã¨æãããã®ã§ç¹ã«ä½ããã¾ããã
logback-product.xml ãè¨å®ãã
src/main/resources ã®ä¸ã® logback-product.xml ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
åä½ç¢ºèªãã¾ããã¾ã㯠build ã¿ã¹ã¯ãå®è¡ã㦠jar ãã¡ã¤ã«ãä½æãã¾ãã
Terminal View ãéããå¾ã以ä¸ã®ç»åã®ã³ãã³ããå®è¡ãã¾ãã
- IntelliJ IDEA ã® Terminal ã«ã¯ä½ãåºåããã¾ããã§ããã
- C:\webapps\ksbysample-webapp-basic\logs\ksbysample-webapp-basic.log ãä½æããã¾ããã
C:\Users\<ã¦ã¼ã¶å>\AppData\Local\Temp
ã®ä¸ã« spring.log ã¯åºåããã¦ãã¾ããã§ããããã ã tomcat.2747162588485574445.8080 ãä½æããã¦ãã¾ãããtomcat.2747162588485574445.8080 ã®ä¸ãè¦ãã¨ãã¡ã¤ã«ã¯ï¼ã¤ãåºåããã¦ãã¾ãããã以ä¸ã®ãµããã£ã¬ã¯ããªãä½æããã¦ãã¾ãã
ãã£ã¬ã¯ããªæ§æãè¦ãæãã§ã¯ Tomcat ã® WORK ãã£ã¬ã¯ããªã®ããã§ãããã£ã¬ã¯ããªããããããæå®ã㦠Temp ãã£ã¬ã¯ããªã«æ¯ååºåãããªãããã«ãããã¨æãã¾ãã
Terminal View 㧠Ctrl+C ãæ¼ã㦠Tomcat ãçµäºãã¾ãã
Spring Boot Reference Guide - Appendix A. Common application properties ã«è¨å®ããªããè¦ã¦ã¿ãã¨ãserver.tomcat.basedir ã¨ããè¨å®é ç®ãããã¾ããããã¶ãããã ã¨æããã¾ãã
src/main/resources ã®ä¸ã® application-product.properties ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
C:\webapps\ksbysample-webapp-basic\work ãã£ã¬ã¯ããªãä½æãã¾ãã
clean ã¿ã¹ã¯ãbuild ã¿ã¹ã¯ãå®è¡ã㦠jar ãã¡ã¤ã«ãä½æãç´ãã¾ããä»åã¯è¨å®ãã¡ã¤ã«ããå¤æ´ãã¦ãã¾ããã®ã§ clean ã¿ã¹ã¯ãå®è¡ããªã㨠jar ãã¡ã¤ã«ãä½æãç´ãã¦ããã¾ããã§ããã
Terminal View ããå度 jar ãã¡ã¤ã«ãå®è¡ãã¾ãã
C:\Users\<ã¦ã¼ã¶å>\AppData\Local\Temp
ã®ä¸ã« tomcat.2747162588485574445.8080 ã®ãããªãã£ã¬ã¯ããªã¯ä½æããã¾ããã§ãããserver.tomcat.basedir
ã§è¨å®ãããã£ã¬ã¯ããªã®ä¸ã«C:\webapps\ksbysample-webapp-basic\work\Tomcat\localhost\ROOT
ãä½æããã¦ãã¾ããã
Terminal View 㧠Ctrl+C ãæ¼ã㦠Tomcat ãçµäºãã¾ãã
commitãGitHub 㸠Pushã1.0.x-logbackxml -> 1.0.x 㸠Pull Requestã1.0.x ã§ãã¼ã¸ã1.0.x-logbackxml ãã©ã³ããåé¤
- commitãGitHub 㸠Pushã1.0.x-logbackxml -> 1.0.x 㸠Pull Requestã1.0.x ã§ãã¼ã¸ã1.0.x-logbackxml ãã©ã³ããåé¤ãããã¾ãã
ã½ã¼ã¹ã³ã¼ã
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>MS932</charset> </encoder> </appender> <property name="LOG_FILE" value="C:/webapps/ksbysample-webapp-basic/logs/ksbysample-webapp-basic.log"/> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> <file>${LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> </appender> <if condition='"${spring.profiles.active}" == "develop"'> <then> <include resource="logback-develop.xml"/> </then> </if> <if condition='"${spring.profiles.active}" == "unittest"'> <then> <include resource="logback-unittest.xml"/> </then> </if> <if condition='"${spring.profiles.active}" == "product"'> <then> <include resource="logback-product.xml"/> </then> </if> </configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
â<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
ã«å¤æ´ãã¾ããbase.xml ã§ãã°åºåå ã«<appender-ref ref="FILE" />
ãæå®ããã¦ãã¾ãããinclude å¾ã«ãã®è¨å®ãåãé¤ãæ¹æ³ãåããã¾ããã§ããã®ã§ãinclude ãã¦è¨å®ãããªãããã«ãã¾ããã<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> ... </appender>
ã追å ãã¾ãããcharset ã®è¨å®ã¯ utf8 ã«ãã㨠IntelliJ IDEA ã® Run View ã§æååããããããMS932 ã«ãã¦ãã¾ãã<property name="LOG_FILE" value="C:/webapps/ksbysample-webapp-basic/logs/ksbysample-webapp-basic.log"/>
ã追å ãã¦ãã°ãã¡ã¤ã«ã®åºåå ãå®ç¾©ãã¾ããã<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> ... </appender>
ã追å ãã¾ããã- ãã°ãã©ã¼ããã㯠defaults.xml ã§å®ç¾©ããã¦ãã FILE_LOG_PATTERN ããã®ã¾ã¾ä½¿ç¨ãã¦ãã¾ãã
- ãã¡ã¤ã«ã®åºåå ã¯ä¸ã§å®ç¾©ãç´ãã LOG_FILE ãè¨å®ãã¦ãã¾ãã
- ãã¼ãã¼ã·ã§ã³ã¯ TimeBasedRollingPolicy ã§æ¥æãã¼ã¹ã§ãã¼ãã¼ã·ã§ã³ãããããã«è¨å®ãã¦ãã¾ãã
- ãã¼ãã¼ã·ã§ã³ã®ã¿ã¤ãã³ã°ã¯
.%d{yyyy-MM-dd}
ã§æ¥åä½ã§è¨å®ãã¾ãããã¾ãæ大 30æ¥åä¿åãããããã«ãã¦ãã¾ããfileNamePattern 㨠maxHistory ã以ä¸ã®ããã«è¨å®ããã¨ååä½ã§ãã¼ãã¼ã·ã§ã³ããåä½ã確èªãããã¨ãåºæ¥ã¾ã ( æ¬å½ã«è¨å®éããã¼ãã¼ã·ã§ã³ããã®ããã®è¨å®ã§ç¢ºèªãã¾ãã ) ã
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd_HH-mm}</fileNamePattern> <maxHistory>5</maxHistory> </rollingPolicy>
logback-develop.xml
<?xml version="1.0" encoding="UTF-8"?> <included> <root level="INFO"> <appender-ref ref="CONSOLE"/> </root> <!-- Spring MVC --> <logger name="org.springframework.web" level="DEBUG"/> <!-- log4jdbc-log4j2 --> <logger name="jdbc.sqlonly" level="DEBUG"/> <logger name="jdbc.sqltiming" level="INFO"/> <logger name="jdbc.audit" level="INFO"/> <logger name="jdbc.resultset" level="ERROR"/> <logger name="jdbc.resultsettable" level="ERROR"/> <logger name="jdbc.connection" level="DEBUG"/> </included>
<root level="INFO"> ... </root>
ã追å ããåºåå 㯠CONSOLE ã®ã¿ã«è¨å®ãã¾ãã
logback-unittest.xml
<?xml version="1.0" encoding="UTF-8"?> <included> <logger name="root" level="OFF"/> </included>
<logger name="root" level="OFF"/>
ã ãã«ãã¾ãã
logback-product.xml
<?xml version="1.0" encoding="UTF-8"?> <included> <root level="INFO"> <appender-ref ref="FILE"/> </root> </included>
<root level="INFO"> ... </root>
ã追å ãã¾ãã<logger name="org.springframework.web" level="INFO"/>
ãåé¤ãã¾ãã
application-product.properties
server.tomcat.basedir = C:/webapps/ksbysample-webapp-basic spring.datasource.url = jdbc:mysql://localhost/world spring.datasource.username = root spring.datasource.password = xxxxxxxx spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.thymeleaf.cache = true
server.tomcat.basedir = C:/webapps/ksbysample-webapp-basic
ã追å ãã¾ãã
å±¥æ´
2015/03/29
åççºè¡ã