/*
 ** 2011 April 22
 **
 ** The author disclaims copyright to this source code.  In place of
 ** a legal notice, here is a blessing:
 **    May you do good and not evil.
 **    May you find forgiveness for yourself and forgive others.
 **    May you share freely, never taking more than you give.
 */
package info.ata4.log;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
 * Log formatter for console output.
 *
 * @author Nico Bergemann <barracuda415 at yahoo.de>
 */
public class ConsoleFormatter extends Formatter {
    
    private static final Map<Level, String> LEVEL_PREFIX;
    
    static {
        Map<Level, String> levelPrefix = new HashMap<>();
        levelPrefix.put(Level.CONFIG,  "[cfg]");
        levelPrefix.put(Level.FINE,    "[dbg]");
        levelPrefix.put(Level.FINER,   "[dbg]");
        levelPrefix.put(Level.FINEST,  "[trc]");
        levelPrefix.put(Level.INFO,    "[inf]");
        levelPrefix.put(Level.SEVERE,  "[err]");
        levelPrefix.put(Level.WARNING, "[wrn]");
        
        LEVEL_PREFIX = Collections.unmodifiableMap(levelPrefix);
    }

    @Override
    public String format(LogRecord record) {
        StringBuilder sb = new StringBuilder();
        sb.append(LEVEL_PREFIX.get(record.getLevel()));
        sb.append(' ');

        String[] classNameParts = record.getLoggerName().split("\\.");

        // add class name for non-info records
        if (record.getLevel() != Level.INFO && classNameParts.length != 0) {
            sb.append(classNameParts[classNameParts.length - 1]);
            sb.append(": ");
        }

        sb.append(formatMessage(record));

        // print stack trace if given
        Throwable thrown = record.getThrown();
        if (thrown != null) {
            sb.append(", caused by ");
            StringWriter sw = new StringWriter();
            try (PrintWriter pw = new PrintWriter(sw)) {
                thrown.printStackTrace(pw);
            }
            sb.append(sw.toString());
        }
        
        sb.append("\n");

        return sb.toString();
    }
}

@@ -891,17 +891,20 @@
 IG,  %22%5Bc
-f
+onfi
 g%5D%22);%0A  
@@ -939,25 +939,27 @@
 FINE,    %22%5Bd
-b
+ebu
 g%5D%22);%0A      
@@ -994,17 +994,19 @@
 R,   %22%5Bd
-b
+ebu
 g%5D%22);%0A  
@@ -1046,17 +1046,19 @@
 T,  %22%5Btr
-c
+ace
 %5D%22);%0A   
@@ -1098,16 +1098,17 @@
    %22%5Binf
+o
 %5D%22);%0A   
@@ -1148,16 +1148,18 @@
 ,  %22%5Berr
+or
 %5D%22);%0A   
@@ -1197,18 +1197,22 @@
 ING, %22%5Bw
+a
 rn
+ing
 %5D%22);%0A   

Revert "Using fixed width log prefixes"