Here you can find the source of getStackTraceAsString(Throwable ex)
Parameter | Description |
---|---|
ex | The exception for which to write out the stack trace. If you pass null it will print the Stack trace of a newly created exception. |
public static String getStackTraceAsString(Throwable ex)
//package com.java2s; /*//w w w . j a va2 s.c o m * Copyright (c) 2015 Christian Chevalley * This file is part of Project Ethercis * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class Main { /** * Prints the stack trace logonservice a String so it can be put on the normal logs. * @param ex The exception for which to write out the stack trace. If you pass null it will print the Stack trace of * a newly created exception. * @return The Stack trace logonservice a String. */ public static String getStackTraceAsString(Throwable ex) { // this is just to send the stack trace to the log file (stderr does not go there) ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream pstr = new PrintStream(baos); if (ex == null) ex = new Exception(); ex.printStackTrace(pstr); return new String(baos.toByteArray()); } }