Add a new class DemoCtrl.java
cat src/main/java/com/ctrl/DemoCtrl.java package com.ctrl; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/demo/ctrl") public class DemoCtrl { @GetMapping public String demoCtrlGet(){ return "This is a demo - about the RESTFul Get request."; } @GetMapping(value = "/gen") public String generateLog(){ return "Generate log. You can try /gen/echo"; } @GetMapping(value = "/gen/{echo}") public String generateLog(@PathVariable String echo){ return echo; } }Add an anotation @ComponentScan(basePackages = {“com.ctrl”}) to class DemoApplication.java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = {"com.ctrl"}) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }Change ./src/main/application.properties. Add following line:
logging.file=${sys:user.home}/logback.logThe log will be printed to $HOME/logfile.log Let take a look at the dependencies:
$ mvn dependency:tree ... [INFO] Scanning for projects... [INFO] [INFO] --------------------------< com.example:demo >-------------------------- [INFO] Building demo 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ demo --- [INFO] com.example:demo:jar:0.0.1-SNAPSHOT [INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.1.4.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.4.RELEASE:compile [INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO] | | | +- ch.qos.logback:logback-core:jar:1.2.3:compile [INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.26:compile [INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.2:compile [INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.11.2:compile [INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.26:compile [INFO] | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile [INFO] | +- org.springframework:spring-core:jar:5.1.6.RELEASE:compile [INFO] | | \- org.springframework:spring-jcl:jar:5.1.6.RELEASE:compile [INFO] | \- org.yaml:snakeyaml:jar:1.23:runtime [INFO] +- org.springframework.boot:spring-boot-starter-json:jar:2.1.4.RELEASE:compile [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile [INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.8:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.8:compile [INFO] | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.8:compile [INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.4.RELEASE:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.17:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.17:compile [INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.17:compile [INFO] +- org.hibernate.validator:hibernate-validator:jar:6.0.16.Final:compile [INFO] | +- javax.validation:validation-api:jar:2.0.1.Final:compile [INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile [INFO] | \- com.fasterxml:classmate:jar:1.4.0:compile [INFO] +- org.springframework:spring-web:jar:5.1.6.RELEASE:compile [INFO] | \- org.springframework:spring-beans:jar:5.1.6.RELEASE:compile [INFO] \- org.springframework:spring-webmvc:jar:5.1.6.RELEASE:compile [INFO] +- org.springframework:spring-aop:jar:5.1.6.RELEASE:compile [INFO] +- org.springframework:spring-context:jar:5.1.6.RELEASE:compile [INFO] \- org.springframework:spring-expression:jar:5.1.6.RELEASE:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.277 s [INFO] Finished at: 2019-04-17T03:41:45-04:00 [INFO] ------------------------------------------------------------------------Be we do any changes, let’s take a look at the pom.xml file
<?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 http://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.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>It looks pretty simple. Let’s add log4j2 dependencies as following:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <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-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>Check the dependencies:
$ mvn dependency:tree WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release [INFO] Scanning for projects... [INFO] [INFO] --------------------------< com.example:demo >-------------------------- [INFO] Building demo 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ demo --- [INFO] com.example:demo:jar:0.0.1-SNAPSHOT [INFO] +- org.springframework.boot:spring-boot-starter:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot:jar:2.1.4.RELEASE:compile [INFO] | | \- org.springframework:spring-context:jar:5.1.6.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.4.RELEASE:compile [INFO] | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile [INFO] | +- org.springframework:spring-core:jar:5.1.6.RELEASE:compile [INFO] | | \- org.springframework:spring-jcl:jar:5.1.6.RELEASE:compile [INFO] | \- org.yaml:snakeyaml:jar:1.23:runtime [INFO] +- org.springframework.boot:spring-boot-starter-log4j2:jar:2.1.4.RELEASE:compile [INFO] | +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.11.2:compile [INFO] | | +- org.slf4j:slf4j-api:jar:1.7.26:compile [INFO] | | \- org.apache.logging.log4j:log4j-api:jar:2.11.2:compile [INFO] | +- org.apache.logging.log4j:log4j-core:jar:2.11.2:compile [INFO] | +- org.apache.logging.log4j:log4j-jul:jar:2.11.2:compile [INFO] | \- org.slf4j:jul-to-slf4j:jar:1.7.26:compile [INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.1.4.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-json:jar:2.1.4.RELEASE:compile [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile [INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.8:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.8:compile [INFO] | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.8:compile [INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.4.RELEASE:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.17:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.17:compile [INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.17:compile [INFO] +- org.hibernate.validator:hibernate-validator:jar:6.0.16.Final:compile [INFO] | +- javax.validation:validation-api:jar:2.0.1.Final:compile [INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile [INFO] | \- com.fasterxml:classmate:jar:1.4.0:compile [INFO] +- org.springframework:spring-web:jar:5.1.6.RELEASE:compile [INFO] | \- org.springframework:spring-beans:jar:5.1.6.RELEASE:compile [INFO] \- org.springframework:spring-webmvc:jar:5.1.6.RELEASE:compile [INFO] +- org.springframework:spring-aop:jar:5.1.6.RELEASE:compile [INFO] \- org.springframework:spring-expression:jar:5.1.6.RELEASE:compileThis is not enough, we have to add log4j2.xml under the resources directory.(This file comes from here)
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="30"> <Appenders> <!-- 输出到控制台 --> <Console name="STDOUT" target="SYSTEM_OUT"> <!-- %d 日期 %p 日志级别 %c 输出所属的类目,通常就是所在类的全名 [%t] 线程 - %m 输出代码中指定的消息 %n 换行 %L : 日志输出所在行数 %M : 日志输出所在方法名:q! --> <PatternLayout pattern="%d %p %c [%t] - %m%n"/> </Console> <!-- 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份-日建立的文件夹下面并进行压缩,作为存档 --> <RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/info.log" filePattern="${sys:user.home}/logs/provider/${date:yyyy-MM-dd}/info-%d{yyyy-MM-dd}-%i.log.gz"> <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) --> <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" /> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n" /> <Policies> <SizeBasedTriggeringPolicy size="100 MB" /> <!-- 切割文件的话最多切割多少个 --> <DefaultRolloverStrategy max="20"/> </Policies> </RollingFile> <!-- 按照天来存储,过了这天的东西会被保存为压缩包,并且配置了大小分割 --> <RollingFile name="InfoFileAppender" fileName="${sys:user.home}/logs/info.log" filePattern="${sys:user.home}/logs/provider/${date:yyyy-MM-dd}/info-%d{yyyy-MM-dd}-%i.log.gz"> <PatternLayout pattern="%d %-5p %c:%L [%t] - %m%n"/> <Policies> <!-- 时间滚动策略,默认1小时 --> <TimeBasedTriggeringPolicy modulate="true" interval="24"/> </Policies> </RollingFile> </Appenders> <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效--> <Loggers> <Logger name="com.opensymphony.xwork2.ognl.OgnlValueStack" level="ERROR"/> <logger name="org.springframework" level="INFO"></logger> <logger name="org.mybatis" level="INFO"></logger> <Logger name="net.sf.ehcache" level="INFO"/> <Logger name="com.alisoft.xplatform.asf" level="WARN"/> <Logger name="com.mbi" level="ERROR"/> <Logger name="net.mlw" level="INFO"/> <Logger name="java.sql" level="INFO"/> <Logger name="org.hibernate.type" level="ERROR"/> <Logger name="com.opensymphony.webwork" level="ERROR"/> <Logger name="org.apache" level="INFO"/> <Logger name="org.jgroups" level="WARN"/> <Logger name="org.jboss.axis" level="INFO"/> <Logger name="org.jboss.management" level="INFO"/> <Logger name="org.apache.commons.httpclient" level="ERROR"/> <Logger name="org.springframework" level="INFO"/> <Logger name="org.springboot" level="INFO" /> <!-- 缺省日志级别,如果package有定制级别,则按package的定制级别走,即使package级别更低 --> <Root level="INFO"> <AppenderRef ref="STDOUT"/> <AppenderRef ref="RollingFileInfo"/> <!-- <AppenderRef ref="InfoFileAppender"/> --> </Root> </Loggers> </Configuration>