每行显示多行文本的日志 [英] Displaying a log per line for a multiline text

查看:70
本文介绍了每行显示多行文本的日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有多行文字"a \ nb \ nc";当我记录它时,例如使用"debug"方法,我只会得到一个记录;

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

这是预期的行为,但是除了输出之外的第一行也显示在输出的左侧:

this is the expected behaviour but then the lines excluding the first are displayed too 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 - ...

_

是否可以自动进行这种处理,还是应该编写一个简单的包装程序来管理此设置?

_

谢谢.

推荐答案

不可能,不推荐.

在您的第一个示例中,很明显存在两个日志语句,而在您的第二个示例中,一眼就可以假设其中有四个.

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.

想象一下,如果您有一条错误声明(例如异常),由于其堆栈跟踪而将跨越30行左右.在您的情况下,这看起来像是30个错误,自动化工具也可能会将其报告为30个错误.这是错误信息,应该避免.

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.

更不用说一条日志语句!=一条书面日志"可能会在您处理更复杂的日志记录情况时引起同步混乱,其中多个线程同时写入同一个文件,或者更糟的是,多个JVM在这样做如此.

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.

如果左侧位置太远"给您带来很多麻烦,我建议对生成的日志文件进行一些后期处理,例如在不包含任何内容的每一行的开头添加8个空格左右[DEBUG],[INFO],...

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],...

这篇关于每行显示多行文本的日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆