/*
 * Copyright to the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.rioproject.log;

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

/**
 * Formats log entries
 *
 * @author Dennis Reedy
 */
public class RioLogFormatter extends Formatter {
    Date date = new Date();
    private final static String format = "{0,date,yyyy.MM.dd HH:mm:ss,SSS}";
    private MessageFormat formatter;
    private Object args[] = new Object[1];
    private String lineSeparator = System.getProperty("line.separator");
    private boolean includePackageNames;

    public RioLogFormatter() {
        super();
        /* load options from the logging properties file */
        includePackageNames = hasDeclaredSupportFor(getClass().getName() + ".includePackageNames");
    }

    /**
     * Format the given LogRecord.
     *
     * @param record the log record to be formatted.
     * @return a formatted log record
     */
    public synchronized String format(LogRecord record) {
        StringBuilder sb = new StringBuilder();
        // Minimize memory allocations here.
        date.setTime(record.getMillis());
        args[0] = date;
        StringBuffer text = new StringBuffer();
        if (formatter == null) {
            formatter = new MessageFormat(format);
        }
        formatter.format(args, text, null);
        sb.append(text);
        sb.append(" ");
        sb.append(record.getLevel().getLocalizedName());
        sb.append(" ");
        if (record.getSourceClassName() != null) {
            String name = record.getSourceClassName();
            if(!includePackageNames) {
                int ndx = record.getSourceClassName().lastIndexOf(".");
                if (ndx > 0)
                    name = record.getSourceClassName().substring(ndx + 1, record.getSourceClassName().length());
                else
                    name = record.getSourceClassName();
            }
            sb.append(name);
        } else {
            sb.append(record.getLoggerName());
        }
        if (record.getSourceMethodName() != null) {
            sb.append(".");
            sb.append(record.getSourceMethodName());
        }

        String message = formatMessage(record);

        sb.append(": ");
        sb.append(message);
        sb.append(lineSeparator);
        if (record.getThrown() != null) {
            try {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                record.getThrown().printStackTrace(pw);
                pw.close();
                sb.append(sw.toString());
            } catch (Exception ex) {
            }
        }
        return sb.toString();
    }

    private boolean hasDeclaredSupportFor(String property) {
        String s = LogManager.getLogManager().getProperty(property);
        return !(s == null || s.trim().length() == 0) && s.equals("true");
    }
}

@@ -780,24 +780,56 @@
 .Formatter;%0A
+import java.util.logging.Level;%0A
 import java.
@@ -849,24 +849,24 @@
 LogManager;%0A
-
 import java.
@@ -1295,16 +1295,45 @@
 geNames;
+%0A    private int longest = 0;
 %0A%0A    pu
@@ -1533,16 +1533,524 @@
 Names%22);
+%0A%0A        Level%5B%5D levels = new Level%5B%5D%7BLevel.SEVERE,%0A                                     Level.WARNING,%0A                                     Level.INFO,%0A                                     Level.CONFIG,%0A                                     Level.FINE,%0A                                     Level.FINER,%0A                                     Level.FINEST%7D;%0A        for(Level l : levels) %7B%0A            if(l.getLocalizedName().length()%3Elongest)%0A                longest = l.getLocalizedName().length();%0A        %7D
 %0A    %7D%0A%0A
@@ -2300,24 +2300,138 @@
 gBuilder();%0A
+        sb.append(String.format(%22%25-%22+longest+%22s%22, record.getLevel().getLocalizedName()));%0A        sb.append(%22 %22);%0A
         // M
@@ -2768,88 +2768,8 @@
 %22);%0A
-        sb.append(record.getLevel().getLocalizedName());%0A        sb.append(%22 %22);
 %0A   

Format the line to have columns line up