显示多行文本的每行日志
假设我有一个多行文本“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 - ...
有没有办法自动进行这种处理,或者我应该写一个简单的包装来管理这个设置?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不可能也不推荐。
在您的第一个示例中,很明显存在两个日志语句,而在第二个示例中,人们可能一眼就会认为存在四个日志语句。
一条日志语句应该提供有关发生的情况和发生时间的单一来源信息,并且该信息应该在某种程度上有用。
想象一下,如果您有一个错误语句(例如异常),由于其堆栈跟踪,该语句将跨越 30 行左右。在您的情况下,这看起来像是 30 个错误,而自动化工具也可能会将其报告为 30 个错误。这是错误信息,应该避免。
更不用说,当您处理更复杂的日志记录情况时,“一个日志语句!=一个写入的日志”可能会导致同步混乱,多个线程同时写入同一个文件,或者更糟糕的是,多个 JVM 这样做。
如果“左边太远”的事情让你很伤心,我建议对生成的日志文件进行一些后期处理,例如在不包含 [DEBUG] 的每一行的开头添加 8 个空格左右,[信息],...
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],...