Log4J将空行打印到日志文件 [英] Log4J print empty line to logfile

查看:419
本文介绍了Log4J将空行打印到日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,您可以使用System.out.println()打印空白行,但是它如何与日志文件中的log4j一起使用? 在那里,我还希望日志文件中有一个或多个空行.

In java you could use System.out.println() to print a blank line but how does it work with log4j in a logfile? There I also want to have one or more blank lines inside the logfile.

我已经阅读了以下内容: log4j如何附加空白行

I already read the follwing: log4j how to append blank line

但这并没有真正的帮助,因为使用

But this is not really helpful because with

logger.debug("\n");
logger.debug("");

您不打印消息,但是其他信息(例如时间等)(记录器的布局)仍存储在日志文件中.但是我只想有一个空行. 有人可以帮我吗?

you do not print a message but other information like time and so on (the layout of the logger) are still stored inside the logfile. But I just want to have a total empty line. Can anyone help me please?

推荐答案

Log4j不能写空白行.

Log4j is not designed to write blank lines.

您无法在记录器中找到此选项,因为记录器独立于他的向文件或控制台或其他内容写入内容的附加程序.

You cannot find this option in the logger, because the logger is independent of his appenders who write to a file or the console or something different.

我认为您需要创建自定义FileAppender,以在编写之前检查Logging消息.如果您的message.equals("\n");是通过自己访问文件而在不添加布局的情况下添加空白行,则跳过带有布局的常规日志记录.那么您可以使用Logger.debug("\n");

I think you need to create your custom FileAppender which checks the Logging Message before the writing. If your message.equals("\n"); you append a blank line without the layout by accessing the file by your own and skip the normal logging with the layout. then you can use Logger.debug("\n");

添加空行也是一种不好的做法.这类似于将您的日志消息分成几行.您希望所有Logmessage都在一行中,因此很容易为LogViewer-Tools(例如链锯OtrosLogViewer 一个例外是堆栈跟踪.

Also it is a bad practice to add blank lines. It is similar to split your Log-Message over several lines. Your want all your Logmessage in one line each, so they are easy to parse for LogViewer-Tools like chainsaw or OtrosLogViewer One exception are Stacktraces.

这篇关于Log4J将空行打印到日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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