/**
 * Copyright (c) 2009 - 2010 AppWork UG(haftungsbeschränkt) <e-mail@appwork.org>
 * 
 * This file is part of org.appwork.utils.logging
 * 
 * This software is licensed under the Artistic License 2.0,
 * see the LICENSE file or http://www.opensource.org/licenses/artistic-license-2.0.php
 * for details
 */
package org.appwork.utils.logging;

import java.text.DateFormat;
import java.util.Date;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;

import org.appwork.utils.Exceptions;

public class LogFormatter extends SimpleFormatter {
    /**
     * Date to convert timestamp to a readable format
     */
    private final Date       date          = new Date();
    /**
     * For thread controlled logs
     */
    private int              lastThreadID;

    /**
     * Dateformat to convert timestamp to a readable format
     */
    private final DateFormat longTimestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);

    @Override
    public synchronized String format(final LogRecord record) {
        /* clear StringBuilder buffer */
        final StringBuilder sb = new StringBuilder();

        // Minimize memory allocations here.
        this.date.setTime(record.getMillis());

        final String message = this.formatMessage(record);
        final int th = record.getThreadID();

        if (th != this.lastThreadID) {
            sb.append("\r\n THREAD: ");
            sb.append(th);

            sb.append("\r\n");
        }
        this.lastThreadID = th;

        sb.append(record.getThreadID());
        sb.append('|');
        sb.append(record.getLoggerName());
        sb.append(' ');
        sb.append(this.longTimestamp.format(this.date));
        sb.append(" - ");
        sb.append(record.getLevel().getName());
        sb.append(" [ ");
        if (record.getSourceClassName() != null) {
            sb.append(record.getSourceClassName());
        } else {
            sb.append(record.getLoggerName());
        }
        if (record.getSourceMethodName() != null) {
            sb.append('(');
            sb.append(record.getSourceMethodName());
            sb.append(')');
        }

        sb.append(" ] ");

        sb.append("-> ");
        sb.append(message);
        sb.append("\r\n");
        if (record.getThrown() != null) {
            sb.append(Exceptions.getStackTrace(record.getThrown()));
            sb.append("\r\n");
        }
        return sb.toString();
    }
}

@@ -78,25 +78,24 @@
 ork.org%3E%0D%0A *
- 
 %0D%0A * This fi
@@ -137,17 +137,16 @@
 ging%0D%0A *
- 
 %0D%0A * Thi
@@ -521,16 +521,58 @@
 ptions;%0D
+%0Aimport org.appwork.utils.os.CrossSystem;%0D
 %0A%0D%0Apubli
@@ -1042,16 +1042,88 @@
 EDIUM);%0D
+%0A    private final String     NEWLINE       = CrossSystem.getNewLine();%0D
 %0A%0D%0A    @
@@ -1566,14 +1566,42 @@
 end(
-%22%5Cr%5Cn 
+NEWLINE);%0D%0A            sb.append(%22
 THRE
@@ -1653,38 +1653,39 @@
       sb.append(
-%22%5Cr%5Cn%22
+NEWLINE
 );%0D%0A        %7D%0D%0A 
@@ -2470,38 +2470,39 @@
       sb.append(
-%22%5Cr%5Cn%22
+NEWLINE
 );%0D%0A        if (
@@ -2628,14 +2628,15 @@
 end(
-%22%5Cr%5Cn%22
+NEWLINE
 );%0D%0A

use system newline!