/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.logger;

import foam.nanos.logger.Logger;
import foam.nanos.NanoService;
import java.util.logging.*;
import java.text.SimpleDateFormat;
import java.io.IOException;

public class StdoutLogger
  extends AbstractLogger
  implements NanoService
{
  protected java.util.logging.Logger logger;

  public void start() {
    logger = java.util.logging.Logger.getAnonymousLogger();
    logger.setUseParentHandlers(false);
    logger.setLevel(Level.ALL);
    Handler handler = new ConsoleHandler();
    handler.setLevel(Level.ALL);
    handler.setFormatter(new CustomFormatter());
    logger.addHandler(handler);
  }

  protected class CustomFormatter extends Formatter {
    long   prevTime;
    String prevTimestamp;

    @Override
    public String format(LogRecord record) {
      int           lev = record.getLevel().intValue();
      String        msg = record.getMessage();
      StringBuilder str = sb.get();

      if ( prevTime / 1000 != System.currentTimeMillis() / 1000 ) {
        prevTime = System.currentTimeMillis();
        prevTimestamp = sdf.get().format(prevTime);
      }

      str.append(prevTimestamp);
      str.append(',');

      // debug special case, fine level == 500
      if ( lev == 500 ) {
        str.append("DEBUG");
      } else {
        str.append(record.getLevel());
      }

      str.append(msg);
      str.append('\n');
      return str.toString();
    }
  }

  public void log(Object... args) {
    logger.info(combine(args));
  }

  public void info(Object... args) {
    logger.info(combine(args));
  }

  public void warning(Object... args) {
    logger.warning(combine(args));
  }

  public void error(Object... args) {
    logger.severe(combine(args));
  }

  // can't normally do .debug() with custom formatter: use fine instead
  public void debug(Object...  args) {
    logger.fine(combine(args));
  }
}

@@ -225,22 +225,22 @@
 ava.
-util.logging.*
+io.IOException
 ;%0Aim
@@ -284,30 +284,30 @@
 rt java.
-io.IOException
+util.logging.*
 ;%0A%0Apubli
@@ -356,33 +356,8 @@
 ger%0A
-  implements NanoService%0A
 %7B%0A  
@@ -397,16 +397,17 @@
 r logger
+_
 ;%0A%0A  pub
@@ -414,18 +414,20 @@
 lic 
-void start
+StdoutLogger
 () %7B
@@ -437,16 +437,17 @@
   logger
+_
  = java.
@@ -494,24 +494,25 @@
 ;%0A    logger
+_
 .setUseParen
@@ -539,16 +539,17 @@
   logger
+_
 .setLeve
@@ -698,16 +698,17 @@
   logger
+_
 .addHand
@@ -1550,32 +1550,33 @@
 gs) %7B%0A    logger
+_
 .info(combine(ar
@@ -1633,16 +1633,17 @@
   logger
+_
 .info(co
@@ -1711,16 +1711,17 @@
   logger
+_
 .warning
@@ -1790,16 +1790,17 @@
   logger
+_
 .severe(
@@ -1941,16 +1941,17 @@
   logger
+_
 .fine(co

Make StdoutLogger not be a NanoService so easier to use.