0

I am currently working on a program and I set up custom log handlers. My log handler looks like this:

public class ProxyLogFormat extends Formatter {

    // Create a DateFormat to format the logger timestamp.
    private static final DateFormat df = new SimpleDateFormat("hh:mm:ss.SSS");

    public String format(LogRecord record) {
        StringBuilder builder = new StringBuilder(1000);
        builder.append(df.format(new Date(record.getMillis()))).append(" - ");
        builder.append("[").append(record.getLoggerName()).append("] -");
        builder.append("[").append(record.getLevel()).append("] - ");
        builder.append(formatMessage(record));
        builder.append("\n");
        return builder.toString();
    }

    public String getHead(Handler h) {
        return super.getHead(h);
    }

    public String getTail(Handler h) {
        return super.getTail(h);
    }
}

I register the log handler like this:

   Handler CH = new ConsoleHandler();
    CH.setFormatter(new ProxyLogFormat());
    log.addHandler(CH);

(log being a Logger object) However, aginest all my trys, the log looks like this:

10:31:21.249 - [MAIN] -[INFO] - Starting proxy...
May 11, 2018 10:31:21 PM pw.mmvyoutube.proxycrack.Main main
INFO: Starting proxy...
10:31:21.265 - [MAIN] -[INFO] - Host: 0.0.0.0
May 11, 2018 10:31:21 PM pw.mmvyoutube.proxycrack.Main main
INFO: Host: 0.0.0.0
10:31:21.266 - [MAIN] -[INFO] - Remote Port: 111
May 11, 2018 10:31:21 PM pw.mmvyoutube.proxycrack.Main main
INFO: Remote Port: 111
10:31:21.268 - [MAIN] -[INFO] - Local Port: 100
May 11, 2018 10:31:21 PM pw.mmvyoutube.proxycrack.Main main
INFO: Local Port: 100

As you can see, some messages repeat and it still has the annoying May 11, 2018 10:31:21 PM pw.mmvyoutube.proxycrack.Main main How can I remove this line?

jmehrens
  • 10,580
  • 1
  • 38
  • 47
Matthew L Parks
  • 17
  • 1
  • 1
  • 10

1 Answers1

0

You have multiple ConsoleHandler objects installed in the logger tree. You need to locate those extra handlers and remove them.

You have a few options:

  1. You could invoke LogManager.reset() before you install your console handler. This will remove all installed handlers in the logger tree.
  2. Don't create a new ConsoleHandler and simply change the format of the default ConsoleHandler. For example

    for (Handler h : Logger.getLogger("").getHandlers()) { if(h instanceof ConsoleHandler) { h.setFormatter(new ProxyLogFormat()); } }

  3. Avoid using code to setup your logger tree and use a logging.properties file. Using that file you can change the default formatter.

  4. Edit the default format pattern of the SimpleFormatter. This can be specified using the java.util.logging.SimpleFormatter.format key in the logging.properties or as a system property. Your pattern is %1$tH:%1$tM:%1$tS.%1$tL - [%3$s] -[%4$s] %5$s%n

jmehrens
  • 10,580
  • 1
  • 38
  • 47