如何在Java中继续登录同一行? [英] How to continue log on same line in Java?
问题描述
这类似于这个问题,这是关于bash文件。
我们创建了一个日志,在我们运行TestNG测试时输出到控制台:
We've created a log which outputs to the console while we're running our TestNG tests:
private Log log = LogFactory.getLog(xyz.class);
稍后,在测试期间,日志中充满了详细信息,让我们知道究竟是什么完成:
Later on, during the tests, the log is filled with details to let us know what exactly is being done:
log.info("Setting browser...");
this.browser = browser;
log.info("Completed.");
现在正按照您的预期打印出来:
Right now it's printing out as you would expect:
Setting browser...
Completed.
我想在同一行打印出来:
I'd like to have this print out on the same line:
Setting browser...
几毫秒之后:
Setting browser...Completed.
这是否可以使用LogFactory?
Is this possible with the LogFactory?
推荐答案
在不知道您正在使用哪个日志库的情况下,我猜几乎可以肯定不是,除非您想明确指定换行符在其他地方。
Without knowing precisely which logging library you are using, I would guess that the answer is almost certainly not unless you want to explicitly specify a newline character everywhere else.
java.util.logging
(以及 Log4J
)框架允许您插入格式化程序来格式化您的语句。您需要指定一个不以换行符结尾的格式。例如,在 Log4J 。他们给出了一个示例模式
The java.util.logging
(and Log4J
) frameworks allow you to plugin formatters to format your statements. You'd need to specify a format which did not end with a newline character. For example, in Log4J. they give an example pattern as
%-5p [%t]: %m%n
因此,如果您删除除以外的所有内容%m
(%n
是换行符,其他内容包括时间和日志级别; %t
和<$我认为c $ c>%p ,新行不会自动附加到每个语句。
So if you removed everything except the %m
(the %n
is the newline and the other stuff includes the time and log-level; %t
and %p
respectively, I think), newlines would not be automatically appended to each statement.
然而,这意味着无处不在否则,你的其他日志语句必须如下所示:
However, this means that everywhere else, your other log statements would have to look like this:
log.info("I want this on one line\n");
这篇关于如何在Java中继续登录同一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!