All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.paralleluniverse.common.logging.SimpleFormatter Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
/*
 * Copyright (c) 2011-2014, Parallel Universe Software Co. All rights reserved.
 * 
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *  
 *   or (per the licensee's choosing)
 *  
 * under the terms of the GNU Lesser General Public License version 3.0
 * as published by the Free Software Foundation.
 */
package co.paralleluniverse.common.logging;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;

/**
 * Exactly the same as {@link java.util.logging.SimpleFormatter java.util.logging.SimpleFormatter}, except passes
 * thread ID as the last (#7) parameter to {@link String#format(java.lang.String, java.lang.Object[]) String.format}.
 * @author pron
 */
public class SimpleFormatter extends Formatter {
    private static final String format = LogManager.getLogManager().getProperty("java.util.logging.SimpleFormatter.format");// LoggingSupport.getSimpleFormat();
    private final Date dat;

    public SimpleFormatter() {
        this.dat = new Date();
    }
    
    @Override
    public synchronized String format(LogRecord record) {
        dat.setTime(record.getMillis());
        String source;
        if (record.getSourceClassName() != null) {
            source = record.getSourceClassName();
            if (record.getSourceMethodName() != null) {
               source += " " + record.getSourceMethodName();
            }
        } else {
            source = record.getLoggerName();
        }
        String message = formatMessage(record);
        String throwable = "";
        if (record.getThrown() != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            pw.println();
            record.getThrown().printStackTrace(pw);
            pw.close();
            throwable = sw.toString();
        }
        return String.format(format,
                             dat,
                             source,
                             record.getLoggerName(),
                             record.getLevel().getLocalizedName(),
                             message,
                             throwable,
                             record.getThreadID());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy