简体   繁体   English

如何解决日志中西里尔符号的问题

[英]How to fix problem with cyrillic symbols in logbook

I just added implementation 'org.zalando:logbook-parent:2.14.0'我刚刚添加了实现 'org.zalando:logbook-parent:2.14.0'
to my project based on 'org.springframework.boot' version '2.4.3' and Java 11.我的项目基于'org.springframework.boot'版本'2.4.3'和Java 11。

In application.yml next properties:在 application.yml 中的下一个属性:

logbook:
  format:
    style: http
logging:
  level:
    org:
      hibernate:
        SQL: DEBUG
      springframework:
        web: DEBUG
        security: INFO
      zalando:
        logbook: TRACE

log from spring is OK, looks like "bankName": "ПОЧТА банк" but zalando.logbook: "bankName":"ПОЧТРбанк"来自 spring 的日志还可以,看起来像"bankName": "ПОЧТА банк"但 zalando.logbook: "bankName":"ПОЧТРбанк"

I add next to build.gradle, but nothing changes.我在 build.gradle 旁边添加,但没有任何变化。

 tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }

What I need to do to fix this encoding problem?我需要做什么来解决这个编码问题?

Thanks in advance.提前致谢。

Edit: to read logs I use Kibana + Elastic编辑:阅读日志我使用 Kibana + Elastic

When strings get longer that generally means that some translation step receives bytes that are intended to be treated as UTF-8, but is being read with ISO-8859 or Cp1252 or some similar aged encoding instead.当字符串变长时,这通常意味着某些翻译步骤接收的字节旨在被视为 UTF-8,但正在使用 ISO-8859 或 Cp1252 或一些类似的老化编码来读取。

Here's the full process.这是完整的过程。 Every time we flip from bytes to chars or vice versa, a charset encoding is involved.每次我们从字节翻转到字符或反之亦然时,都会涉及到字符集编码。 If the wrong encoding is involved in even a single step, the end result is completely mangled.如果即使在一个步骤中涉及错误的编码,最终结果也会完全被破坏。 They all have to align (they all have to be UTF-8).它们都必须对齐(它们都必须是 UTF-8)。 Be aware that all files are bytes and not characters in nature .请注意,所有文件本质上都是字节而不是字符

  1. Code editor.代码编辑器。
  2. Compiler.编译器。
  3. The 'logger client' / logger output system. “记录器客户端”/记录器 output 系统。
  4. The tool you use to view the logfile.用于查看日志文件的工具。

Code editor代码编辑器

You wrote explicit non-ascii characters in your source code.您在源代码中编写了明确的非 ascii 字符。 A java source file is bytes, so when you type a cyrillic character, it shows up on your screen as a cyrillic character in your java source file, but when happens when you hit file/save? java 源文件是字节,因此当您键入西里尔字符时,它会在您的屏幕上显示为 java 源文件中的西里尔字符,但是当您点击文件/保存时会发生什么情况? You'll need to check your editor.你需要检查你的编辑器。 Open up the editor, open a file with cyrillic characters, and check the encoding.打开编辑器,打开一个带有西里尔字符的文件,然后检查编码。 On eclipse, you'd click the File menu and hit the Properties option while the file is open.在 eclipse 上,您可以在文件打开时单击“文件”菜单并点击“属性”选项。 The encoding is listed on the bottom of the 'Resource' panel, in the section 'Text file encoding'.编码列在“资源”面板底部的“文本文件编码”部分中。

Compiler编译器

Javac needs to read that file. Javac 需要读取该文件。 The option you pasted ( options.encoding = 'UTF-8' ) in your gradle file takes care of this.您在 gradle 文件中粘贴的选项 ( options.encoding = 'UTF-8' ) 可以解决这个问题。

Viewing查看

As I said, all files are bytes , so when you open any text file, you must choose an encoding.正如我所说,所有文件都是字节,所以当你打开任何文本文件时,你必须选择一种编码。 Many smart text editors will take a wild guess but some just assume a certain encoding.许多智能文本编辑器会大胆猜测,但有些只是假设某种编码。 Check your editor - there's generally an option to pick which encoding the file is in, Text files do not contain within themselves the encoding that was used, except in very rare circumstances (generally a BOM which can be encoded in all UTF flavours, but that's unique to UTF specifically. and isn't commonly added).检查您的编辑器 - 通常可以选择文件所在的编码,文本文件本身不包含所使用的编码,除非在极少数情况下(通常是可以以所有 UTF 风格编码的 BOM,但仅此而已UTF 特有的。通常不添加)。 It is entirely possible there's nothing wrong at all with your code, you're just looking at it with a text editor that isn't told to read it as UTF-8 .您的代码完全有可能没有任何问题,您只是使用文本编辑器查看它,而不会被告知将其读取为 UTF-8

The black box黑匣子

If it's none of the above, ouch, then this becomes much more difficult: Logs are sent as strings (which means, encoding isn't a thing, fortunately, and no mangling can occur) inside the JVM until they arrive at a so-called 'client' or 'log backend' which then does whatever it wants with them: Log systems are abstract concepts.如果以上都不是,哎呀,那么这将变得更加困难:日志在 JVM 中作为字符串发送(这意味着,幸运的是,编码不是一件事,并且不会发生混乱),直到它们到达一个 so-称为“客户端”或“日志后端”,然后对它们进行任何操作:日志系统是抽象概念。 Handlers are intentionally just 'here you go, a log message at this level, do what you will with it'.处理程序故意只是“给你 go,这个级别的日志消息,做你想做的事”。 If you want to display them with flashing lights on your television, then feel free to write a log client for that that does this.如果您想在电视上用闪光灯显示它们,请随意编写一个日志客户端来执行此操作。

Your logclient ends up writing the log message into a file, and therefore, it is turning a string into bytes and therefore, it is applying an encoding.您的日志客户端最终将日志消息写入文件,因此,它将字符串转换为字节,因此,它正在应用编码。

That other answer you found indicates that zalando's logbook library uses the encoding as returned by the servlet runner, and that answer explains how to ensure that's UTF-8.您找到的其他答案表明 zalando 的日志库使用 servlet 运行程序返回的编码,并且该答案解释了如何确保这是 UTF-8。 I assume you did that.我假设你这样做了。

So how do I solve this problem?那么我该如何解决这个问题呢?

Go through all 4 of the above steps: Go 通过上述所有 4 个步骤:

  • Ensure your editor is in UTF-8 mode.确保您的编辑器处于 UTF-8 模式。
  • Ensure that gradle block you pasted in your question is being used.确保正在使用您粘贴在问题中的 gradle 块。
  • Ensure that other answer's setup to ensure your servlet runner is set to UTF-8 is implemented.确保实施其他答案的设置,以确保您的 servlet 运行程序设置为 UTF-8。
  • Ensure that the thing you use to look at your log file is configured to use UTF-8.确保您用于查看日志文件的内容配置为使用 UTF-8。

If you've done all those things, the problem should go away.如果你已经做了所有这些事情,那么问题应该 go 了。

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

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