速记
1. 背景描述
在使用springboot开发web项目的过程中,前期工作一直很顺利,着实感觉到了springboot所提供的便利性。然而,一直没有踩太多坑的我心里反而不踏实,哈哈哈,贱癌入骨,无药可治。可就在今天正式线上部署(线上采用外部tomcat的方式进行部署,自配日志方便问题排查)的过程中,遇到了那么一点点滴问题:自配log4j配置文件不生效,配置路径无日志输出,但catalina.out日志正常输出。下面就简单记录描述一下。
2. 问题定位
第一感觉是觉得tomcat配置有误,可在经过了各种方式尝试无果后,开始怀疑是不是log4j文件被所引用项目中的默认的log4j所吞没了。顺着这个思路,开始进行尝试。
首先打印依赖树关系,找到冲突依赖排除掉。根据依赖关系,很明显,springboot 自带 logging 嫌疑最大,首当其冲将其排除掉,重新打包测试运行。如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
重新部署运行后,查看catalina.out日志输出,发现明显不一样了。如下:
01-Nov-2018 19:21:34.890 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars 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.
log4j: Trying to find [log4j.xml] using context classloader ParallelWebappClassLoader
context: xcar_hbase
delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@238e0d81
.
log4j: Trying to find [log4j.xml] using ParallelWebappClassLoader
context: xcar_hbase
delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@238e0d81
class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader ParallelWebappClassLoader
context: xcar_hbase
delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@238e0d81
.
log4j: Using URL [file:/export/tomcat/hbase-api/webapps/xcar_hbase/WEB-INF/classes/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/export/tomcat/hbase-api/webapps/xcar_hbase/WEB-INF/classes/log4j.properties
log4j: Parsing for [root] with value=[INFO,console,DRFA,RFA,RFAW].
log4j: Level token is [INFO].
log4j: Category root set to INFO
log4j: Parsing appender named "console".
log4j: Parsing layout options for "console".
log4j: Setting property [conversionPattern] to [%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n].
log4j: End of parsing for "console".
log4j: Setting property [target] to [System.out].
log4j: Parsed "console" options.
log4j: Parsing appender named "DRFA".
log4j: Parsing layout options for "DRFA".
log4j: Setting property [conversionPattern] to [%d{yy/MM/dd HH:mm:ss} %-5p [%t] %c{2}: %m%n].
log4j: End of parsing for "DRFA".
log4j: Setting property [encoding] to [UTF-8].
log4j: Setting property [datePattern] to [.yyyy-MM-dd].
log4j: Setting property [threshold] to [INFO].
log4j: Setting property [file] to [/export/tomcat/hbase-api/logs/dls/info.log].
log4j: setFile called: /export/tomcat/hbase-api/logs/dls/info.log, true
log4j: setFile ended
log4j: Appender [DRFA] to be rolled at midnight.
log4j: Parsed "DRFA" options.
log4j: Parsing appender named "RFA".
log4j: Parsing layout options for "RFA".
log4j: Setting property [conversionPattern] to [%d{yy/MM/dd HH:mm:ss} %p [%t] %c{2} >>> %m%n].
log4j: End of parsing for "RFA".
log4j: Setting property [encoding] to [UTF-8].
log4j: Setting property [file] to [/export/tomcat/hbase-api/logs/dls/error.log].
log4j: Setting property [maxBackupIndex] to [5].
log4j: Setting property [threshold] to [ERROR].
log4j: Setting property [maxFileSize] to [200MB].
log4j: setFile called: /export/tomcat/hbase-api/logs/dls/error.log, true
log4j: setFile ended
log4j: Parsed "RFA" options.
log4j: Parsing appender named "RFAW".
log4j: Parsing layout options for "RFAW".
log4j: Setting property [conversionPattern] to [%d{yy/MM/dd HH:mm:ss} %p [%t] %c{2} >>> %m%n].
log4j: End of parsing for "RFAW".
log4j: Setting property [encoding] to [UTF-8].
log4j: Setting property [maxBackupIndex] to [1].
log4j: Setting property [file] to [/export/tomcat/hbase-api/logs/dls/debug.log].
log4j: Setting property [threshold] to [DEBUG].
log4j: Setting property [maxFileSize] to [20MB].
log4j: setFile called: /export/tomcat/hbase-api/logs/dls/debug.log, true
log4j: setFile ended
log4j: Parsed "RFAW" options.
log4j: Finished configuring.
可以看到,日志中打出了自配log4j被加载的过程,tomcat在加载spring的过程中首先查找系统资源路径下是否有spring相关[log4j.xml]或是[log4j.properties]文件,直到检测结果均为 false 时才会到 tomcat classes 路径下查找项目自身的log4j相关配置文件并加载。
3. 完整POM
下面给大家贴出springboot相关完整pom文件:
<?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>
<groupId>com.xxxx.xxxx</groupId>
<artifactId>xxxxx</artifactId>xxxxx
<packaging>war</packaging>
<version>1.0.1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--指定Java编译版本-->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.11</junit.version>
<springboot.version>1.5.8.RELEASE</springboot.version>
</properties>
<!--Inherit defaults from Spring Boot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<!-- Package as an executable jar -->
<dependencies>
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--打包可执行包并部署到外部Tomcat容器需添加该依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>xcar_hbase</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<!--项目中没有引入web.xml声明,否则报错-->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
网友评论