<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"MS Gothic";
panose-1:2 11 6 9 7 2 5 8 2 4;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"\@MS Gothic";
panose-1:2 11 6 9 7 2 5 8 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:12.0pt;
font-family:"Times New Roman",serif;}
span.EmailStyle18
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hello, <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I am using slf4j with logback as backend log solution. When I trying to make the log file support Unicode, I add <encoder><charset>UTF-8</charset><encoder> to logback.xml,<o:p></o:p></p>
<p class="MsoNormal">However I find problems.<o:p></o:p></p>
<p class="MsoNormal">My slf4j-api version 1.7.20<o:p></o:p></p>
<p class="MsoNormal"> Logback-classic and logback-core: 1.1.7<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Code:<o:p></o:p></p>
<p class="MsoNormal" style="text-autospace:none"><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F0055">package</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> cbao.cmdtests;</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F0055">import</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> org.slf4j.Logger;</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F0055">import</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> org.slf4j.LoggerFactory;</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#3F5FBF">/**</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#3F5FBF">*
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F9FBF">@author</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#3F5FBF">
<u>cbao</u></span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#3F5FBF">*</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#3F5FBF">*/</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F0055">public</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F0055">class</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> LogTests {</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#3F5FBF">/**</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#3F5FBF"> *
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F9FBF">@param</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#3F5FBF"> args</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#3F5FBF"> *
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F9FBF">@throws</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#3F5FBF"> InterruptedException</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#3F5FBF"> */</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F0055">public</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F0055">static</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F0055">void</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> main(String[]
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#6A3E3E">args</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">)
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F0055">throws</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> InterruptedException {</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> Logger
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#6A3E3E">logger</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> = LoggerFactory.<i>getLogger</i>(</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#2A00FF">"LogTests"</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">);</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">
</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F0055">for</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> (</span><b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#7F0055">int</span></b><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#6A3E3E">i</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> = 0;
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#6A3E3E">i</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> < 5000;
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#6A3E3E">i</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">++) {</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#6A3E3E">logger</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">.info(</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#2A00FF">"This
is ANSI text"</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">);</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> Thread.<i>sleep</i>(0);</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> }</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">
</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#6A3E3E">logger</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">.info(</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#2A00FF">"This
is unicode text: </span><span style="font-size:10.0pt;font-family:"MS Gothic";color:#2A00FF">中文</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:#2A00FF">"</span><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">);</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black"> }</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif;color:black">}</span><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Logback.xml:<o:p></o:p></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"><?<u><span style="color:black">xml</span></u> version="1.0" encoding="UTF-8"?><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"><configuration scan="true"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> <encoder><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif">
<u><span style="color:black"><charset></span></u>UTF-8<u><span style="color:black"></charset></span></u><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> <Pattern>%d %-4relative [%thread] %-5level %logger{35} - %<u><span style="color:black">msg</span></u>%n</Pattern><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> </encoder><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> </appender><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> <appender name="FILE" class="ch.qos.logback.core.FileAppender"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> <file>logs/application.log</file><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> <encoder><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif">
<u><span style="color:black"><charset></span></u>UTF-8<u><span style="color:black"></charset></span></u><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> <pattern>%d %-4relative [%thread] %-5level %logger{35} - %<u><span style="color:black">msg</span></u>%n</pattern><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> <immediateFlush>true</immediateFlush><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> </encoder><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> </appender><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> <root level="DEBUG"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> <appender-<u><span style="color:black">ref</span></u>
<u><span style="color:black">ref</span></u>="consoleAppender" /><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> <appender-<u><span style="color:black">ref</span></u>
<u><span style="color:black">ref</span></u>="FILE"/><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New",serif"> </root><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif"></configuration><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif">After running above code, the log file format is 1252 (ANSI – Latin I)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif">The end of log file:<o:p></o:p></span></p>
<p class="MsoNormal">2016-04-21 17:00:12,666 151328 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:12,717 151379 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:12,767 151429 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:12,817 151479 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:12,867 151529 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:12,918 151580 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:12,968 151630 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:13,019 151681 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:13,070 151732 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:13,120 151782 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:13,171 151833 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:13,222 151884 [main] INFO LogTests - This is ANSI text
<o:p></o:p></p>
<p class="MsoNormal">2016-04-21 17:00:13,273 151935 [main] INFO LogTests - This is unicode text: ä¸æ–‡<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">As my test, if there is no unicode log, the newly created log file will be
<span style="font-size:10.0pt;font-family:"Courier New",serif">1252 (ANSI – Latin I).<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif">As soon as first unicode string logged, the log file changed (unicode: UTF-8).<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif">However, if there are significant ansi text before first unicode text could be logged, the log file will stay as ANSI even after utf-8 logged, then the unicode log can’t be
display correctly.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif">Is there any switches in logback.xml can fix it?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif">Thanks
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif">Cheng Bao<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New",serif"><o:p> </o:p></span></p>
</div>
</body>
</html>