以下のurlの写経。
いつも思いますが、ログ出力って、ごく単純な機能なのに、 なんで、こんなにもPG言語やライブラリ毎に利用法に違いがあるんでしょ?
毎度、使用方法を忘れている気がします。
STEP 1 - pom.xml の編集
以下にPOM全体を記載していますが、 「spring-boot-starter-logging」「spring-boot-starter-log4j2」あたりを追記。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>jp.co.sexy.spak2</groupId> <artifactId>SpakSpring</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpakSpring</name> <description>Spak written from SEASAR2 to SpringBoot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <!-- LOG4J2 のよる出力の為、以下を追加 --> <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-test</artifactId> <scope>test</scope> </dependency> <!-- HTML TEMPLATEは thymeleafでなく、昔ながらのjsp --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- ログ出力は、LOG4J2 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
STEP 2 - src/main/resources/log4j2.xml の作成
参考にさせて頂いたurlでは、様々設定されていますが、 今回、とりあえず出力するだけですので、以下の程度です。
<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <Properties> <Property name="app_name">demo</Property> <Property name="date">%d{yyyy-MM-dd HH:mm:ss.SSS}</Property> <Property name="daily_log">logs/app${app_name}_%d{yyyy-MM-dd}.log</Property> </Properties> <appenders> <Console name="Console" target="SYSTEM_OUT" > <PatternLayout pattern="${date}, [${app_name}], [ %-5level ], %logger{10}, %msg %n" /> </Console> <RollingFile name="File" fileName="logs/app.log" filePattern="${daily_log}.gz"> <PatternLayout pattern="${date}, [${app_name}], [ %-5level ], %logger{10}, %msg %n" /> <Policies> <TimeBasedTriggeringPolicy /> </Policies> </RollingFile> </appenders> <loggers> <root level="info"> <appender-ref ref="Console" /> <appender-ref ref="File" /> </root> </loggers> </configuration>
STEP 3 - *.java での出力処理
「★」の部分を追記します。
package jp.end0tknr.action; import org.apache.logging.log4j.LogManager; // ★ import org.apache.logging.log4j.Logger; // ★ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexAction { // ★ static protected Logger logger = LogManager.getLogger(IndexAction.class); @RequestMapping("/") public String index(Model model) { logger.info("start index()"); // ★ return "index"; } }
STEP4 - 出力確認
以上により、コンソール & $APP_ROOT/logs/app.log へ、 以下のように出力されます。
2021-03-20 15:18:50.894, [demo], [ INFO ], jp.co.sexy.spak.SpakSpringApplication, Starting SpakSpringApplication using Java 1.8.0_202 on LAPTOP-Q767CC9I with PID 23104 (C:\Users\end0t\workspace_eclipse_2020\SpakSpring\target\classes started by end0t in C:\Users\end0t\workspace_eclipse_2020\SpakSpring) 2021-03-20 15:18:50.976, [demo], [ INFO ], jp.co.sexy.spak.SpakSpringApplication, No active profile set, falling back to default profiles: default 2021-03-20 15:18:52.103, [demo], [ INFO ], org.springframework.boot.web.embedded.tomcat.TomcatWebServer, Tomcat initialized with port(s): 8080 (http) 2021-03-20 15:18:52.125, [demo], [ INFO ], org.apache.coyote.http11.Http11NioProtocol, Initializing ProtocolHandler ["http-nio-8080"] 2021-03-20 15:18:52.126, [demo], [ INFO ], org.apache.catalina.core.StandardService, Starting service [Tomcat] 2021-03-20 15:18:52.127, [demo], [ INFO ], org.apache.catalina.core.StandardEngine, Starting Servlet engine: [Apache Tomcat/9.0.44] 2021-03-20 15:18:52.340, [demo], [ INFO ], org.apache.jasper.servlet.TldScanner, At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 2021-03-20 15:18:52.345, [demo], [ INFO ], org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/], Initializing Spring embedded WebApplicationContext 2021-03-20 15:18:52.345, [demo], [ INFO ], org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext, Root WebApplicationContext: initialization completed in 1294 ms 2021-03-20 15:18:52.568, [demo], [ INFO ], org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor, Initializing ExecutorService 'applicationTaskExecutor' 2021-03-20 15:18:52.682, [demo], [ INFO ], org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping, Adding welcome page template: index 2021-03-20 15:18:52.806, [demo], [ INFO ], org.apache.coyote.http11.Http11NioProtocol, Starting ProtocolHandler ["http-nio-8080"] 2021-03-20 15:18:52.850, [demo], [ INFO ], org.springframework.boot.web.embedded.tomcat.TomcatWebServer, Tomcat started on port(s): 8080 (http) with context path '' 2021-03-20 15:18:52.985, [demo], [ INFO ], jp.co.sexy.spak.SpakSpringApplication, Started SpakSpringApplication in 2.632 seconds (JVM running for 5.894) 2021-03-20 15:19:11.227, [demo], [ INFO ], org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/], Initializing Spring DispatcherServlet 'dispatcherServlet' 2021-03-20 15:19:11.227, [demo], [ INFO ], org.springframework.web.servlet.DispatcherServlet, Initializing Servlet 'dispatcherServlet' 2021-03-20 15:19:11.228, [demo], [ INFO ], org.springframework.web.servlet.DispatcherServlet, Completed initialization in 1 ms 2021-03-20 15:19:11.268, [demo], [ INFO ], jp.co.sexy.spak.action.IndexAction, start index()