开发者

Displaying a log per line for a multiline text

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 automatica开发者_开发技巧lly 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],...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜