运行SpringBoot 项目乱码问题
编译乱码
{
"configurations": [
{
"name": "Build",
"type": "jps-build",
"workingDir": "C:\\Users\\xxx\\Documents\\idea\\demo",
"module": "jiqfu-demo",
"includeTests": false,
"additionalVmOptions": [
// 重点是添加下面这段代码解决编译问题
"-Dfile.encoding=UTF-8"
],
},
{
"name": "DemoApplication",
"type": "spring-boot",
"workingDir": "C:\\Users\\xxx\\Documents\\idea\\demo",
"dependsOn": [
"Build"
],
"mainClass": "top.jiqfu.demo.DemoApplication",
"module": "jiqfu-demo",
"options": [
"-XX:TieredStopAtLevel=1",
"-Dspring.output.ansi.enabled=always",
"-Dcom.sun.management.jmxremote",
"-Dspring.jmx.enabled=true",
"-Dspring.liveBeansView.mbeanDomain",
"-Dspring.application.admin.enabled=true",
"-Dmanagement.endpoints.jmx.exposure.include=*",
"-Dfile.encoding=UTF-8",
"@C:\\Users\\xxx\\AppData\\Local\\Temp\\idea_arg_file853143121"
],
"activeProfiles": [
""
]
}
]
}
控制台乱码解决
方法1配置文件添加
logging:
charset:
console: GBK
方法2添加logback.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 控制台 appender -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
<charset>GBK</charset>
</encoder>
</appender>
<!-- 出错日志 appender -->
<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滚 daily -->
<!-- log.dir 在maven profile里配置 -->
<fileNamePattern>./log/error/daotong-error-%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
<charset>GBK</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 访问日志 appender -->
<appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滚 daily -->
<fileNamePattern>./log/warn/daotong-warn-%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
<charset>GBK</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印WARN日志 -->
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- info日志 appender -->
<appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滚 daily -->
<fileNamePattern>./log/info/daotong-info-%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
<charset>GBK</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!--日志打印的包的范围,及分类日志文件存储 -->
<logger name="com.daotonginfo.manage" additivity="false">
<level value="INFO" />
<appender-ref ref="STDOUT"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="WARN"/>
<appender-ref ref="INFO" />
</logger>
<!--控制台打印资源加载信息-->
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
方式3 添加环境变量
"LOGGING_CHARSET_CONSOLE": "GBK"
{
"configurations": [
// .....
"environment": {
"LOGGING_CHARSET_CONSOLE": "GBK"
},
}
]
}