log4j control console and file output garbled problem resolution

Source: Internet
Author: User
Tags log4j

Source: http://www.coderli.com/log4j-console-file-garbled A small problem, but it makes me feel that there are few people who really move their brains.  Let me talk about it. Today encountered a small problem, log4j output to file garbled, the console is normal. Obviously the coding problem leads to. Google a search, almost a water said:

In the project log4j in the English version of Linux output Chinese log is garbled.

because the encoding format (encoding) is not set in the log4j configuration file, Log4j uses the system default encoding. Lead to garbled characters. The

workaround is to set the encoding format UTF-8 by:

log4j.appender.syslog.encoding=utf-8
This is obviously a turn, because the whole net is almost the same. This is a properties configuration, not XML. If you want to have XML, configure the following:
<appender name= "A1" class= "Org.apache.log4j.RollingFileAppender" >         
<param name= "Encoding" value= "UTF-8" "/>         
<param name=" File "value=" All.log "/>        
 ... 
</appender>

However, I was already set into UTF-8, and garbled. Therefore, the above answer is not rigorous.

First talk about the author's situation, in fact, the author's problem is very simple, two sets of log4j appender configuration, an output file, a console, the file configuration of the Utf-8 code, the console is not configured. phenomenon, the console normal, file garbled.
-------------------------------------------changed the file to GBK, not disorderly. Console changed to GBK, garbled. The console changed to Utf-8, Normal. You may be confused when you get here.   Why so messy. In fact, the truth is very simple, garbled, nature is the encoding does not match. What matches. log4j with utf-8 input, your file is not utf-8 accepted it.   Check, sure enough not to change to Utf-8 code, solve. You may have to ask, where is the console match. The Eclipse console is also encoded, I think you should know.   The Eclipse console is coded, and, not only, you can also set up separate encodings for each program that executes.           Naturally, the code here matches, it will not be garbled. Reference two: http://www.blogjava.net/andy199999/articles/176221.html

The items at hand are processed with log4j for log output, do not know how, the recent output of the log content incredibly appeared garbled-question mark, and more depressing is, from another class of attributes read in Chinese can be normal display, tried various methods, such as to add a encode to the log for GBK, UTF-8, can not solve this problem, suddenly think of the Java source file will be the problem ...

Think of this because in myeclipse inside see Chinese is garbled, I have adjusted content TYPES (that is, Window->preferences->general->content TYPES), changed the encoding format of the text , that is, the default encode, check, and sure enough, so I put the default Java sources source of the GBK to delete, and then recompile, garbled problem resolution, oh, put here, for those who have the heart of such problems in the time to mention a wake up:

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.