Java 修改控制台输出以进行调试

Java modifying console outputs for debugging

我最近被打印到我的控制台的大量消息弄得不知所措,但不知道它们来自哪里。

有没有什么方法可以轻松制作类似于 system.out.println 的自定义 class,它将打印出自定义消息以及

Apache 确实提供了日志记录 class,以及它的不同级别。使用这些时,您实际上可以指定需要打印的级别,它还可以帮助您确定调用它的位置。

以下面一行为例,

[2015-03-31 12:51:29,426] DEBUG {org.springframework.beans.factory.support.DefaultListableBeanFactory} - 检索到 bean '(inner bean)#1b534211#2' 的依赖 bean:[org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver #0] {org.springframework.beans.factory.support.DefaultListableBeanFactory}

它说,它是在 3 月 31 日 12:51 时登录的。记录器用于调试一些逻辑,它是从 org.springframework.beans.factory.support.DefaultListableBeanFactory class.

调用的

也许您可以使用 OuputStream,在其中自定义控制台中的输出

Is there any way to easily make a custom class similar to system.out.println that will print out a custom message along with

print the location of code the message is coming from
be toggleable by importance level
not be a pain to use
other helpful stuff?

您基本上只是描述了记录器的用途。 Java 有相当不错的日志记录框架,如 log4j、logback 等。使用任何具有 sl4j 外观的框架。使用 sl4j 日志记录就像 System.out.println().

一样简单

只需创建一个记录器实例

private static final Logger log = LoggerFactory.getLogger(YourClassName.class);

然后像这样打印日志

log.debug("Some log");

您还可以编写不同级别的参数化日志消息(级别用于表示重要性级别)

log.debug("Username={}", username);
log.trace("Username={}", username);
log.warn("Username={}", username);

这可能是开始的好地方 http://www.javacodegeeks.com/2012/04/using-slf4j-with-logback-tutorial.html