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

org.openas2.logging.DefaultFormatter Maven / Gradle / Ivy

Go to download

Open source implementation of the AS2 standard for signed encrypted and compressed document transfer

There is a newer version: 2.10.1
Show newest version
package org.openas2.logging;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.openas2.OpenAS2Exception;
import org.openas2.util.DateUtil;


public class DefaultFormatter extends BaseFormatter {
    public String getTerminatedMessage(OpenAS2Exception exception) {
        StringBuffer buf = new StringBuffer("Termination of exception:" + System.getProperty("line.separator"));

        // Write exception trace
        StringWriter strWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(strWriter);
        exception.printStackTrace(printWriter);
        buf.append(strWriter.toString());

        // Write sources
        Map sources = exception.getSources();
        Iterator< Map.Entry> sourceIt = sources.entrySet().iterator();
        Map.Entry source;

        while (sourceIt.hasNext()) {
            source = sourceIt.next();
            buf.append(source.getKey().toString()).append(System.getProperty("line.separator"));
            buf.append(source.getValue().toString()).append(System.getProperty("line.separator")).append(System.getProperty("line.separator"));
        }

        return buf.toString();
    }

    public void format(Level level, String msg, OutputStream out) {
		PrintWriter writer = new PrintWriter(out);
        
        // Write timestamp		
	    writer.print(DateUtil.formatDate(dateFormat));		
		
		// Write log level
		writer.print(" ");
	    writer.print(level.getName().toUpperCase());
	    
	    // Write message
	    writer.print(" ");
        writer.println(msg);        
		writer.flush();
    }

    public void format(Throwable t, boolean terminated, OutputStream out) {
    	PrintWriter writer = new PrintWriter(out);
        writer.println(format(t, terminated));
        writer.println();
		writer.flush();
    }

	@Override
	public String format(Throwable t, boolean terminated)
	{
        StringBuffer sb = new StringBuffer();

        if (terminated) {
            sb.append("Termination of exception:");
        }

        // Write exception trace
        sb.append(ExceptionUtils.getStackTrace(t));

        // Write sources if terminated
        if (terminated && t instanceof OpenAS2Exception) {
        	OpenAS2Exception exception = (OpenAS2Exception) t;
            Map sources = exception.getSources();
            Iterator> sourceIt = sources.entrySet().iterator();
            Map.Entry source;

            while (sourceIt.hasNext()) {
                source = sourceIt.next();
                if (source.getKey() != null) {                
                	sb.append(source.getKey().toString());
                } else {
                	sb.append("null key");
                }
                if (source.getValue() != null) {                
                sb.append(source.getValue().toString());
                } else {
                	sb.append("null value");
                }
            }
        }
        return sb.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy