log Error from Throwable - Java java.lang

Java examples for java.lang:Exception

Description

log Error from Throwable

Demo Code


//package com.java2s;

import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object pObjects = "java2s.com";
        logError(pObjects);//  w ww.  j  a v  a2  s.c o  m
    }

    public static void logError(Object... pObjects) {
        System.err.println(getMsg(pObjects));
    }

    private static StringBuilder getMsg(Object... pObjects) {
        StringBuilder lMsg = new StringBuilder();
        for (Object o : pObjects) {
            if (o != null) {
                if (o instanceof Throwable) {
                    Throwable e = (Throwable) o;
                    StringWriter lStack = new StringWriter();
                    e.printStackTrace(new PrintWriter(lStack));
                    lMsg.append(e.getClass().getSimpleName());
                    lMsg.append(":");
                    lMsg.append(e.getMessage());
                    lMsg.append('\n');
                    lMsg.append(lStack);
                }
                lMsg.append(o);
                lMsg.append(' ');
            }
        }
        return lMsg;
    }
}

Related Tutorials