对于多行文本,每行显示一条日志[英] Displaying a log per line for a multiline text

本文是小编为大家收集整理的关于对于多行文本,每行显示一条日志的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

说我有一个多行文本" a \ nb \ nc";当我登录时,例如使用"调试"方法,我只会得到一个日志;

这是预期的行为,但是不包括第一个行的线在输出的左侧显示:

1234 [1] [DEBUG] Test - a
b
c
1235 [1] [DEBUG] Test - ...

一个简单的解决方法是每行生成一个日志以获得:

1234 [1] [DEBUG] Test - a
1235 [1] [DEBUG] Test - b
1236 [1] [DEBUG] Test - c
1237 [1] [DEBUG] Test - ...

是否有任何方法可以自动进行此类处理,或者我应该编写一个简单的包装器来管理此设置?

推荐答案

不可能,不推荐.

在您的第一个示例中,很明显存在两个日志语句,而在您的第二个示例中,人们可能会认为其中有四个.

一个日志语句应提供有关发生的事情以及发生的事情的单一源信息,并且该信息应以某种方式有用.

想象一下,如果您有一个错误语句,例如一个异常,它将由于其堆栈跟踪而跨越30行.在您的情况下,这看起来像30个错误,并且自动化工具也可能报告为30个错误.这是错误的信息,应避免.

更不用说"一个日志语句!=一个书面日志"可能在处理更复杂的日志记录情况时可能会导致同步破坏,而多个线程同时写入同一文件,或更糟糕的是,多个JVM所以.

如果"左边太远"的事情给您带来了很多悲伤,我建议您在生成的日志文件上进行一些帖子处理,例如在不包含的每一行的开始时添加8个空间左右[debug],[info],...

本文地址:https://www.itbaoku.cn/post/1574778.html

问题描述

Say I have a multi-line text "a\nb\nc"; when I log it, for example with the "debug" method, I get only one log;

This is the expected behaviour, but then the lines excluding the first are displayed on the left in the output:

1234 [1] [DEBUG] Test - a
b
c
1235 [1] [DEBUG] Test - ...

A simple workaround is to generate one log per line to obtain:

1234 [1] [DEBUG] Test - a
1235 [1] [DEBUG] Test - b
1236 [1] [DEBUG] Test - c
1237 [1] [DEBUG] Test - ...

Is there any way of having this kind of handling automatically or should I write a simple wrapper to manage this setting?

推荐答案

Not possible and not recommended.

In your first example, it's clear that there exist two log statements, whereas in your second example one might assume at a glance that there are four of them.

One log statement should provide a single source information about what happened and when it happened, and that information should be useful in some way.

Imagine if you have one error statement, like an exception, that will span across 30 or so lines because of its stack trace. That would look like 30 errors in your case, and an automated tool might also report it as 30 errors. This is misinformation and should be avoided.

Not to mention "one log statement != one written log" might cause synchronization havoc when you're dealing with more complex logging situations, with multiple threads writing to the same file at the same time, or worse, multiple JVMs doing so.

If the "too far on the left" thing is giving you much grief, I'd suggest doing some post processing on generated log file, such as adding 8 spaces or so at the beginning of every line that doesn't contain [DEBUG], [INFO],...