package io.airlift.log;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.logging.ErrorManager.CLOSE_FAILURE;
import static java.util.logging.ErrorManager.FLUSH_FAILURE;
import static java.util.logging.ErrorManager.FORMAT_FAILURE;
import static java.util.logging.ErrorManager.WRITE_FAILURE;

final class OutputStreamHandler
        extends Handler
{
    private final Writer writer;

    public OutputStreamHandler(OutputStream out)
    {
        writer = new OutputStreamWriter(out, UTF_8);
        setFormatter(new StaticFormatter());
    }

    @Override
    public void publish(LogRecord record)
    {
        if (!isLoggable(record)) {
            return;
        }

        String message;
        try {
            message = getFormatter().format(record);
        }
        catch (Exception e) {
            reportError(null, e, FORMAT_FAILURE);
            return;
        }

        try {
            writer.write(message);
        }
        catch (Exception e) {
            reportError(null, e, WRITE_FAILURE);
        }
    }

    @Override
    public void flush()
    {
        try {
            writer.flush();
        }
        catch (Exception e) {
            reportError(null, e, FLUSH_FAILURE);
        }
    }

    @Override
    public void close()
    {
        try {
            writer.flush();
        }
        catch (Exception e) {
            reportError(null, e, CLOSE_FAILURE);
        }
    }
}

package io.airlift.log;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

import static java.nio.charset.StandardCharsets.UTF_8;

final class OutputStreamHandler
        extends Handler
{
    private final StaticFormatter formatter = new StaticFormatter();
    private final AtomicBoolean reported = new AtomicBoolean();
    private final Writer writer;

    public OutputStreamHandler(OutputStream out)
    {
        writer = new OutputStreamWriter(out, UTF_8);
    }

    @Override
    public void publish(LogRecord record)
    {
        if (!isLoggable(record)) {
            return;
        }

        try {
            writer.write(formatter.format(record));
            writer.flush();
        }
        catch (Exception e) {
            // try to report the first error
            if (!reported.getAndSet(true)) {
                PrintWriter error = new PrintWriter(writer);
                error.print("LOGGING FAILED: ");
                e.printStackTrace(error);
                error.flush();
            }
        }
    }

    @Override
    public void flush() {}

    @Override
    public void close() {}
}

Add flushing for console logging
Add flushing for console logging
