Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将stacktrace打印到文件中_Java_Logging - Fatal编程技术网

Java 将stacktrace打印到文件中

Java 将stacktrace打印到文件中,java,logging,Java,Logging,我有一个错误堆栈跟踪方法,它向log4j发送错误消息: public void writeErrorStackMessage(String message) { log.error(message); } 我还想将堆栈跟踪的打印添加到文件中 catch (Exception e) { lm.writeErrorStackMessage(e.printStackTrace()); } 问题是什么是将错误堆栈

我有一个错误堆栈跟踪方法,它向log4j发送错误消息:

public void writeErrorStackMessage(String message)
    {
        log.error(message);
    }
我还想将堆栈跟踪的打印添加到文件中

catch (Exception e)
        {
            lm.writeErrorStackMessage(e.printStackTrace());
        }
问题是什么是将错误堆栈作为消息发送的正确方法

编辑:


我以这种方式修改了代码。

方法
e.printStackTrace()
也接受PrintWriter或PrintStream参数,因此如果您的
lm
对象有一个OutputStream,您应该可以这样做

e.printStackTrace(new PrintStream (lm.getOutputStream ()));

您可以将log4j.properties配置为使用PrrintWriter将stacktrace发送到文件或写入文件。你想要什么?我想把它作为简单的错误消息发送。我这样修改了代码。但是有没有更优雅的解决方案呢?它的优雅之处是什么?有没有一种不用数组就能做到这一点的方法?在这种情况下,只需使用Exception
getmessage()
方法即可
e.printStackTrace(new PrintStream (lm.getOutputStream ()));