Spring的Logger配置和中文乱码问题

来源:互联网 发布:数据挖掘 模型评估 编辑:程序博客网 时间:2024/05/16 12:57

环境:Window10(默认语言为英文)

springframeworl 4.3.0.RELEASE

控制台mvn tomcat7:run启动项目

pom.xml关键部分

<properties><org.springframework.version>4.3.0.RELEASE</org.springframework.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>org.odata4j</groupId><artifactId>odata4j-jersey</artifactId><version>0.7.0</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.0</version></dependency><!-- spring frameworks --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${org.springframework.version}</version><!--<exclusions>--><!--<exclusion>--><!--<groupId>commons-logging</groupId>--><!--<artifactId>commons-logging</artifactId>--><!--</exclusion>--><!--</exclusions>--></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${org.springframework.version}</version></dependency><!-- <dependency>                    <groupId>org.springframework.data</groupId>                    <artifactId>spring-data-commons</artifactId>                    <version>1.13.4.RELEASE</version>                </dependency> --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId><version>1.11.6.RELEASE</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-rest-webmvc</artifactId><version>2.6.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework.hateoas</groupId><artifactId>spring-hateoas</artifactId><version>0.23.0.RELEASE</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.9.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.9.0</version><!--<scope>test</scope>--></dependency><dependency><groupId>org.apache.xbean</groupId><artifactId>xbean-spring</artifactId><version>3.7</version></dependency><!-- Unit Test Frameworks --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${org.springframework.version}</version><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-all</artifactId><version>1.9.5</version><scope>test</scope></dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-module-junit4</artifactId><version>${powermock.version}</version><scope>test</scope></dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-api-mockito</artifactId><version>${powermock.version}</version><scope>test</scope></dependency><!-- Axis Library --><dependency><groupId>axis</groupId><artifactId>axis</artifactId><version>1.4</version></dependency><dependency><groupId>org.apache.axis</groupId><artifactId>axis-jaxrpc</artifactId><version>1.4</version></dependency><dependency><groupId>org.apache.ws.commons.axiom</groupId><artifactId>axiom-api</artifactId><version>1.2.20</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-adb</artifactId><version>1.5</version></dependency><dependency><groupId>org.apache.ws.commons.axiom</groupId><artifactId>axiom-impl</artifactId><version>1.2.20</version><scope>runtime</scope></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.4</version></dependency><dependency><groupId>org.apache.olingo</groupId><artifactId>olingo-odata2-api</artifactId><version>${olingo.version}</version></dependency><dependency><groupId>org.apache.olingo</groupId><artifactId>olingo-odata2-core</artifactId><version>${olingo.version}</version><exclusions><exclusion><groupId>javax.ws.rs</groupId><artifactId>javax.ws.rs-api</artifactId></exclusion></exclusions></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.6.1</version></dependency><dependency><groupId>com.michaelwflaherty</groupId><artifactId>cleverbotapi</artifactId><version>1.0.2</version></dependency><dependency><groupId>org.apdplat</groupId><artifactId>word</artifactId><version>1.3</version></dependency></dependencies><build><plugins><plugin><artifactId>maven-war-plugin</artifactId><version>2.1</version><configuration><warName>${project.artifactId}</warName></configuration></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>2.5.1</version><configuration><source>1.8</source><target>1.8</target><showDeprecation>true</showDeprecation><showWarnings>true</showWarnings></configuration></plugin><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.0</version><configuration><contextFile>${project.basedir}/src/test/resources/context.xml</contextFile><!-- <path>/</path> --></configuration></plugin><plugin><groupId>org.apache.activemq.tooling</groupId><artifactId>activemq-perf-maven-plugin</artifactId><version>5.9.0</version></plugin></plugins></build>
这里因为activemq带了slf4j,org.spdlat中引入了slf4j-api和logback-classic,所以不需要另外引入slf4j和log4j,如果需要的话引入slf4j-api

运行项目显示绑定的是log4j

SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/C:/Users/i332390/.m2/repository/org/apache/activemq/activemq-all/5.9.0/activemq-all-5.9.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: Found binding in [jar:file:/C:/Users/i332390/.m2/repository/ch/qos/logback/logback-classic/0.9.28/logback-classic-0.9.28.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]Sep 27, 2017 4:39:27 PM org.apache.catalina.core.ApplicationContext log
在src/main/resources下创建log4j.xml (appender的encoding为第一个关键点)

<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd"><log4j:configuration>  <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">    <param name="encoding" value="UTF-8" />    <layout class="org.apache.log4j.PatternLayout">      <param name="ConversionPattern" value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>    </layout>  </appender>  <logger name="com.sap.c4c.wechat.web.PublicAccountController">    <level value="debug"/>    <appender-ref ref="STDOUT" />  </logger>  <logger name="com.sap.c4c.wechat.service">    <level value="debug"/>    <appender-ref ref="STDOUT" />  </logger>  <!-- Strictly speaking, the level attribute is not necessary since -->  <!-- the level of the root level is set to DEBUG by default.       -->  <root>    <level value="info"/>    <appender-ref ref="STDOUT" />  </root></log4j:configuration>

使用时引入 org.slf4j.Logger和org.slf4j.LoggerFactory

private static final Logger logger = LoggerFactory.getLogger(YourClass.class.getName());logger.info("输出");

cmd控制台执行

chcp 65001
设置属性,选择合适的字体,比如

这时可以看到编码




此时,原来显示为???或者乱码的中文能正确显示


对于http请求的中文乱码,设置Content-Type为application/json;charset=UTF-8

对于http回复的中文乱码,在servlet配置中可以这样写

    <mvc:annotation-driven>        <mvc:message-converters>            <bean class="org.springframework.http.converter.StringHttpMessageConverter" >                <property name="supportedMediaTypes" value="application/json;charset=UTF-8"/>            </bean>        </mvc:message-converters>    </mvc:annotation-driven>



参考链接:

http://blog.csdn.net/lixiaolong521999/article/details/70046489

http://www.cnblogs.com/edwardlauxh/archive/2010/07/22/1918554.html

https://my.oschina.net/huanger/blog/714179

http://blog.csdn.net/inkfish/article/details/4761357

http://www.cnblogs.com/guogangj/p/3931397.html

http://blog.csdn.net/lansetiankong12/article/details/52512095

https://stackoverflow.com/questions/40922522/how-do-i-properly-include-the-org-apache-catalina-filters-setcharacterencodingf

http://blog.csdn.net/simon_1/article/details/9092747

http://blog.csdn.net/kontrol/article/details/7767983

原创粉丝点击