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

at.spardat.xma.boot.logger.LogHandlerBase Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

/*
 * Created on : 25.06.2003
 * Created by : s3595
 */
package at.spardat.xma.boot.logger;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Date;
import java.text.MessageFormat;
import java.util.Properties;

/**
 * LogHandlerDefault
 *
 * @author s3595,(CGS)
 * @version $Id: LogHandlerBase.java 2084 2007-11-27 14:53:31Z s3460 $
 *
 */
public abstract class LogHandlerBase implements ILogHandler {

    protected MessageFormat formatter;
    protected Object        args[]    = new Object[1];
    protected String        format    = "{0,date} {0,time}"; //$NON-NLS-1$
    protected String        separator = " : ";
    protected Properties    props     = new Properties();

    protected LogHandlerBase() { }

    public void setProperties( Properties in ) {
        props = in;
    }

    public void publish(LogRecord record) { }

    public void close() { }

    protected String format( LogRecord record ){

        StringBuffer sb = new StringBuffer(100);
        Date dat = new Date( record.getMillis() );


        args[0] = dat;
        StringBuffer text = new StringBuffer();
        if (formatter == null) {
            formatter = new MessageFormat(format);
        }
        formatter.format(args, text, null);
        sb.append(text.toString());

        sb.append(separator);
        if (record.getSourceClassName() != null) {
            sb.append(record.getSourceClassName());
        } else {
            String name = record.getLoggerName();
            sb.append( name + ( name.length()< 20 ? space(20-name.length()) : "") );
        }
        if (record.getSourceMethodName() != null) {
            sb.append(" "); //$NON-NLS-1$
            sb.append(record.getSourceMethodName());
        }

        sb.append(separator);
        sb.append(record.getLevelName() );
        sb.append(separator);
        sb.append( this.formatMessage(record) );

        if (record.getThrown() != null) {
            sb.append( System.getProperty("line.separator")); //$NON-NLS-1$

            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 synchronized String formatMessage(LogRecord record) {
        String format = record.getMessage();

        // Do the formatting.
        try {
            Object parameters[] = record.getParameters();
            if (parameters == null || parameters.length == 0) {
                return format;
            }
            // Is is a java.text style format?
            if (format.indexOf("{0") >= 0) { //$NON-NLS-1$
                return java.text.MessageFormat.format(format, parameters);
            }
            return format;

        } catch (Exception ex) {
            return format;
        }
    }

    private String space( int count ) {
        if(count<=0)
            return "";

        StringBuffer sb = new StringBuffer(count);
        for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy