Search code examples
javamysqljdbclog4jappender

How to save ERROR logs in to a database with JDBC Appender


I would like to save the stacktrace of exceptions to a mysql database using JDBC appender. The issue here is I don't want to use e.printstacktrace in my code. Is there a way to go about?


Solution

  • Managed to convert the stacktrace to a string by using ExceptionUtil class. Once the stacktrace is converted to a string I was able to save the error log using the DB appender as described below.

    try{
        //Some code that throws an exception
        }
    
    catch(SomeException e)
        {
        String stackTrace = ExceptionUtils.getStackTrace(e);
        log.error(":: An error has occurred :::: " + stackTrace);
        }