有没有办法从System.out.println触发触发器? [英] Is there a way to fire a trigger from System.out.println?

查看:113
本文介绍了有没有办法从System.out.println触发触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常愚蠢的问题,但是,是否有一种方法可以在Java中将某些内容打印到控制台并在其他地方监听时触发触发器?

This might be a very dumb question but, is there a way to fire a trigger whenever something is printed to the console in Java and listen for it somewhere else?

我对触发器或System类(或Logger而言)了解甚少,因此,如果有更简便的方法(没有触发器),请告诉我.

I have very little understanding of triggers or of the System class (or of Logger for that matter) so if there is an easier way of doing this (without triggers) please tell me so.

我们正在尝试使用Logger记录所有测试.当前,我们一直在使用简单的"System.out.println"在控制台中记录所有内容,因此我想知道是否可以在打印任何内容时触发触发器,然后让侦听器将该信息添加到Logger.

We are trying to use Logger to log all of our tests. Currently, we have been using the simple "System.out.println" to log everything in the console, so I was wondering whether I could fire a trigger whenever something in printed to it and then have a listener add that info to Logger.

谢谢.

推荐答案

最直接的方法是

System.setOut(new PrintStream(System.out) {
  public void println(String s) {
    logger.log(s);
    super.println(s);
  }
  // override some other methods?
});

这篇关于有没有办法从System.out.println触发触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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