Search code examples
javatriggerslistensystem.out

Is there a way to fire a trigger from System.out.println?


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?

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.

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.

Thanks.


Solution

  • The most direct way to do this would be

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