简体   繁体   English

如何使用log4j将希伯来语字符串写入日志文件

[英]How to write Hebrew strings to a log file using log4j

How to write a Hebrew string to a log4j file. 如何将希伯来字符串写入log4j文件。 Right now I see ?????? 现在我看到?????? in the file. 在文件中。

I have searched everywhere online to convert Unicode to string: 我在网上到处搜索将Unicode转换为字符串:

String abc = myStr.replaceAll("\u200F", "");
   abc = abc.replaceAll("\u200E", "");
   byte[] utf8Bytes = abc.getBytes(Charset.forName("UTF-8"));
   String value = new String(utf8Bytes);
   log.debug("value : "+ value );

I just need to write out a Hebrew string to a Log4j file in a readable format. 我只需要以可读格式将希伯来字符串写入Log4j文件。 Here is my configuration: 这是我的配置:

log4j.rootLogger=debug, stdout, R log4j.logger.testlogging=DEBUG 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender     
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout     
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd} %5p [%t] (%F:%L) - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender     
log4j.appender.R.File=C:\\dri\\ums.log log4j.appender.R.MaxBackupIndex=5     
log4j.appender.R.layout=org.apache.log4j.PatternLayout     
log4j.appender.R.layout.ConversionPattern= %d{dd MMM yyyy HH:mm:ss,SSS} %5p [%t] (%F:%L) - %m%n log4j.appender.FILE.encoding=UTF-8 

Based from what I've gathered from the comments and my own experience this is most probably not an issue with Log4j itself. 根据我从评论和我自己的经验中收集的内容,这很可能不是Log4j本身的问题。 I've posted a comment indicating just that: 我发布了一条评论,表明:

What exactly do you mean with Log4j file? 你对Log4j文件究竟是什么意思? Is it a regular text log file that the FileAppender points to? 它是FileAppender指向的常规文本日志文件吗? Because I've tried printing Hebrew text right now and all is working out fine. 因为我现在尝试打印希伯来文,所有这些都很好。 I believe this is not a Log4j issue and might be related to your text reader. 我相信这不是Log4j问题,可能与您的文本阅读器有关。

Other comments have confirmed their suspicion that it is your text reader that might be causing this issue. 其他评论已经证实他们怀疑是您的文本阅读器可能导致此问题。 I was able to reproduce your issue by doing the following in Notepad++ : 通过在Notepad++执行以下操作,我能够重现您的问题:

  • Open a new tab in notepad++. 在记事本++中打开一个新选项卡。
  • Copy and paste sample text containing Hebrew letters. 复制并粘贴包含希伯来字母的示例文本。
  • Language -> Convert to ANSI

Text before the conversion: 转换前的文字:

See also: אלף־בית‎ and אַלף־בית‎

Text after conversion: 转换后的文字:

See also: ???????? and ?????????

Based on the code you provided (assuming there is no shenanigans that we don't know for behind the scenes) we can definitively conclude you are either writing to a file that has it's encoding set to ANSI where all your special characters are being converted to question marks because they cannot be decoded or your characters are being read as UTF-8 but merely displayed as ANSI . 基于您提供的代码(假设我们在幕后没有我们不知道的恶作剧),我们可以明确地得出结论,您要么将其编码设置为ANSI的文件写入,其中所有特殊字符都将转换为问号,因为它们无法解码或您的字符被读取为UTF-8但仅显示为ANSI

ANSI and UTF-8 are both encoding formats. ANSI和UTF-8都是编码格式。 ANSI is the common one byte format used to encode Latin alphabet; ANSI是用于编码拉丁字母的常见单字节格式; whereas, UTF-8 is a Unicode format of variable length (from 1 to 4 bytes) which can encode all possible characters. 而UTF-8是可变长度的Unicode格式(从1到4个字节),可以编码所有可能的字符。

I would recommend following these steps: 我建议遵循以下步骤:

  • Navigate to Settings -> Preferences -> New Document -> Encoding and make sure that the UTF-8 (Apply to opened ANSI files) option is selected. 导航到Settings -> Preferences -> New Document -> Encoding ,并确保选中UTF-8 (Apply to opened ANSI files)选项。

  • Close all your files currently opened in Notepad++ and delete the log file. 关闭当前在Notepad++打开的所有文件并删除日志文件。 Make sure you are actually closing the files instead of just closing Notepad++ . 确保您实际关闭文件而不是关闭Notepad++ This should clear the file entries from cache and allow you to open them again with a different encoding. 这应该清除缓存中的文件条目,并允许您使用不同的编码再次打开它们。

  • Run your Java application and let Log4j print to the file. 运行Java应用程序并让Log4j打印到该文件。

  • Open the file with Notepad++ and check that you are encoding in UTF-8 by clicking on Encoding tab. 使用Notepad++打开文件,然后单击“ Encoding选项卡,检查是否使用UTF-8进行Encoding If the option is not set to UTF-8 , change it. 如果该选项未设置为UTF-8 ,请更改它。

  • If none of the above worked, please post further information in the comments. 如果上述方法均无效,请在评论中发布更多信息。

Unfortunately I am not that well versed in encoding matters and had to look some stuff up in the process of writing this, so I can't help you as much as I would like to. 不幸的是,我不是那么精通编码问题而且在写这篇文章的过程中不得不看一些东西,所以我无法像你想的那样帮助你。 However in addition to providing the steps above I can direct you to the following links which should provide you with further knowledge and (consequently) more insight into your problem: 但是,除了提供上述步骤之外,我还可以引导您访问以下链接,这些链接应该为您提供进一步的知识,并(因此)更深入地了解您的问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM