Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Foo {
- // ...
- public static final Logger logger = Logger.getLogger("FooLog");
- private static Foo.MyConsoleHandler myConsoleHandler = new MyConsoleHandler();
- static {
- java.util.logging.LogManager.getLogManager().reset();
- logger.addHandler(myConsoleHandler);
- logger.setLevel(java.util.logging.Level.ALL);
- }
- /**
- * We don't want ALL the logging stuffs to be printed out in red color to the console (e.g. in Eclipse)
- * - for example, the texts printed out with logger.info("...") should remain black.
- *
- * @see http://stackoverflow.com/questions/9794516/change-appengine-console-red-color-in-eclipse/16229664#16229664
- * @author Pete
- */
- public static class MyConsoleHandler extends java.util.logging.StreamHandler {
- private java.util.logging.Formatter formatter = new SimpleFormatter();
- public void publish(java.util.logging.LogRecord record) {
- if (record.getLevel().intValue() < java.util.logging.Level.WARNING.intValue()) {
- System.out.println(formatter.formatMessage(record));
- // flushing the stream, because the error output stream is not buffered, and this way the messages can mix:
- // (@see http://stackoverflow.com/questions/9146257/why-do-system-err-statements-get-printed-first-sometimes)
- System.out.flush();
- } else {
- System.out.flush(); // we have to flush it here too
- System.err.println(formatter.format(record));
- }
- }
- }
- // ...
- public void bar() {
- logger.info("Info");
- logger.severe("Severe");
- // ...
- }
- // ...
- }
Add Comment
Please, Sign In to add comment