Java 如何在 LOG 语句后添加换行符/空行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35702342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:01:47  来源:igfitidea点击:

How do I add a line break / blank line after my LOG statement?

javaloggingslf4jline-breaks

提问by java123999

I want to add a blank line after my LOGstatement in order to make my logs more separatedand readable.

我想在我的LOG语句后添加一个空行,以使我的日志更加分离可读

How do I do this?

我该怎么做呢?

Current statement:

当前声明:

 LOGGER.info("Person's name is  {} .", person.getName());

Note that I don't want to do this after everystatement, just for certain statements.

请注意,我不想在每个语句之后这样做,只是为了某些语句。

采纳答案by Davide Lorenzo MARINO

Simply add \nat the end of the string to log.

只需\n在字符串末尾添加即可记录。

LOGGER.info("Person's name is  {} .\n", person.getName());

If you are in a windows environment use \r\n

如果您在 windows 环境中使用 \r\n



To get the right value of new line if you don't know the end operating system of your application you can use the property line.separator

如果您不知道应用程序的最终操作系统,要获得新行的正确值,您可以使用该属性 line.separator

String lineSeparator = System.getProperty("line.separator");
LOGGER.info("Person's name is  {} .{}", person.getName(), lineSeparator);

回答by lordkain

dont use \ncharacter, but ask the System what is the newline propery. could be different in linuxor windows or..

不要使用\n字符,而是询问系统什么是换行符属性。在 linux 或 windows 中可能有所不同或..

so use

所以用

System.lineSeparator();

or before java 7 use

或在 java 7 使用之前

System.getProperty("line.separator");