/*
 * $Id: VKCApacheFormatter.java,v 1.3 2004/11/10 15:02:46 koppy Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 */

package vkc.misc;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

/**
 * Publish the Log Record in Apache Log File Format. Die entsprechende Argumente
 * werden beim log Aufruf des Loggers ueber das Array param uebergeben. Dem
 * Apache Format entspricht folgende REihenfolge:
 * <ol> 
 * <li>param[0] = Remothostname</li>
 * <li>param[1] = ident </li>
 * <li>param[2] = auth </li>
 * <li>param[3] = "date" </li>
 * <li>param[4] = request</li>
 * <li>param[5] = status </li>
 * <li>param[6] = bytes</li>
 * </ol>
 * 
 * @author gau
 */

public class VKCApacheFormatter extends Formatter {
    
    public static final int HOST = 0;
    public static final int IDENT = 1;
    public static final int AUTH = 2;
    public static final int DATE = 3;
    public static final int REQUEST = 4;
    public static final int STATUS = 5;
    public static final int BYTES = 6;
    
    SimpleDateFormat formatter = new SimpleDateFormat("[dd/MMM/yyyy:HH:mm:ss Z]");

    public String format(LogRecord logRecord) {
        StringBuffer buf = new StringBuffer();

        String mydate = new String(formatter.format(new Date(logRecord
                .getMillis()))
                + " ");

        if (logRecord.getParameters() != null) {
            for (int index = 0; index < logRecord.getParameters().length; index++) {
                if (logRecord.getParameters()[index] == null)
                    buf.append("- ");

                else {
                    if ((String) logRecord.getParameters()[index] == "date")
                        buf.append(mydate);

                    else
                        buf.append(logRecord.getParameters()[index] + " ");
                }
            }
        }

        buf.append("\n");

        return buf.toString();
    }
}

