Solve the problem that the log file generated by Logback cannot display Chinese Characters

Source: Internet
Author: User
In the new project, logback + slf4j is used to replace the original log4j + slf4j and run as a WebApplication in tomcat. It is found that all Chinese characters in the generated log file are garbled. The first solution is to set the encoding of the generated log file in logback. Two settings are found on the internet, but they do not work: 1. logback + slf4j is used to replace the original log4j + slf4j in the new project, and is used as a Web Appli CatIon in McRun at and find that all Chinese characters in the generated log file are garbled.

The first solution is to set the encoding of the generated log file in logback. Two settings are found on the Internet,But none of them work.:

1. Set ENcOding. Add a row in appender UTF-8 But an error is reported during startup, saying that the Encoding element cannot be recognized. Some people say that Encoding is no longer available in Versions later than 0.9.22. So we found the second method.

2. Set char in EncoderSet. Logback implements different Encoder in the Set, where LayoutWrapPingEncoder has a charset attribute, and someone mentioned that you can use charset to set the log file encoding. Modify the encoder attribute:

  1. <Encoder Class="Ch. qos. logback. core. encoder. LayoutWrappingEncoder">
  2. <Pattern>% D {yyyy-MM-DdHH: mm: ss. SSS }:% p [% c] [% t] % m % n Pattern>
  3. <Charset>UTF-8 Charset>
  4. Encoder>

However, the system prompts that pattern cannot be found at startup.

Therefore, both methods end with failure. It may be that I did not find the correct setting method. If you have successfully set the method, please share it.

Finally, you can change the encoding of logback by setting the JVM parameter during tomcat startup by using the encoding of the program running by default:

Open catalina. bat. In the first line of the code, that isSet CATALINA_OPTSPreviously, add a row:

Set JAVA_OPTS = % JAVA_OPTS %-DFile. Encoding = UTF-8

Restart tomcat to test the log file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.