Tomcat9源码调试(使用IDEA和Maven构建)之控制台部分中文日志乱码问题
这几天重新学习Tomcat,就想源码调试编译一下。参考了一些资料,使用IDEA基于Maven导入编译了源码,编译的tomcat版本是9.0.31。具体的构建过程就不多说了,网上可以搜索到很多的参考。如果有问题的话可以参考:https://www.cnblogs.com/jhxxb/p/10768580.html。
基本过程都一致的。这个博客主要是讲编译完成后遇到的两个问题:1.部分本地化的中文日志乱码。2.官方测试页面跳转不正常。在这之前先把我编译用的pom.xml文件贴一下,写日志的主要目的还是记录学习过程中遇到的问题,和解决问题的过程以及方法。
1.pom.xml文件
<?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>org.apache.tomcat</groupId>
<artifactId>Tomcat9.0.31</artifactId>
<name>Tomcat9.0.31</name>
<version>9.0.31</version>
<build>
<finalName>Tomcat9.0.31</finalName>
<sourceDirectory>java</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>java</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>test</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-apache-log4j</artifactId>
<version>1.10.7</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-commons-logging</artifactId>
<version>1.10.7</version>
</dependency>
<dependency>
<groupId>geronimo-spec</groupId>
<artifactId>geronimo-spec-jaxrpc</artifactId>
<version>1.1-rc4</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.7</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>ecj</artifactId>
<version>3.20.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>4.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
这个pom.xml文件导入的依赖包都是必须的,同时也是当前(2020年2月)最新的,运行过程中用了别的包可以自行加入。
2.中文日志乱码问题
以上是控制台的中文乱码,这和我们正常部署tomcat遇到的日志或控制台乱码是不一样的。那个问题可以通过修改tomcat的log.properites修正解决,网上也有很多回答,就不多说了。乱码问题要么是编码格式不对,要么是解码过程不对。经过追踪调试,最终在以下方法找到乱码的首次出现的。
这是一个解析属性文件文件的方法,从键值对可以看出,这是日志本地化的替换字符文件解析。通过调试台可以看到,属性在解析时就出现了乱码。一开始我以为是Tomcat的日志编码属性设置不对,但是反复修改不起作用。最后只能通过同时对两个程序进行debug追踪进行对比(一个正常部署的tomcat无乱码问题,一个自编译后运行的tomcat程序)。最终确定了是读取的peoperties文件不同。坑爹的地方在于运行读取文件的时候,每次使用的8k的byte[]作为buffer,而前800个byte数据是一样的,导致我一开始找错了方向,一直没看出来。最后实在没办法,只好通过IDEA的Evaluation Expression功能直接把这两个byte[]中的数据转存出来到txt文件中进行对比才发现第800个数据开始不对,原因应该是是前面文件前面的英文版版权声明是一致的。以下是转存数据的所在的方法截图以及在Evaluation Expression执行的转存代码。
转存数据代码
FileWriter writer = new FileWriter("src/main/resources/test.txt");
for (int i = 0; i < inByteBuf.length; i++) {
writer.write(i + " : ");
writer.write(Byte.toString(inByteBuf[i]));
writer.write("\r\n");
}
writer.flush();
writer.close();