LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   a logger and a printStackTrace in Java (https://www.linuxquestions.org/questions/programming-9/a-logger-and-a-printstacktrace-in-java-259019/)

poeta_boy 11-24-2004 09:45 PM

a logger and a printStackTrace in Java
 
Hello:

I have a simple class logger, for applications and keep a log of exceptions:

Code:

import java.io.*;


/** Class that will act as a general logger for the system, intended for debugging and status control
  *
  * @author KingCrimson
  */
public final class Logger
{

                private static PrintWriter file;

                public static synchronized void log(String what)
                {
                        try

                        {
                                file = new PrintWriter(new FileWriter("c:/ccss.log", true));
                                file.println(what);
                                file.close();
                        }
                        catch(Exception e)
                        {
                                System.out.println("Error en archivo");
                                e.printStackTrace();
                        }
                }// log

}// logger

and I wanna print there all info related to exceptions, but in a catch statement, the Exception e has a method e.printStackTrace() wich is void, can I take that info and pass it as a string so it suits my Logger class and do something like:

Logger.log(e.printStackTrace());

I've been using the e.getMessage() and the e.toString() but it's not quite the same

Please Help! Thanks a lot

Poeta

Jose Muñiz 11-24-2004 11:30 PM

I'm not sure if it works, but if you do a

StackTraceElements[] array = exception.getStackTrace();

And then you cycle through each element of the array, executing the following instruction in each cycle:

Logger.log(array[i].toString());

I think that should do the job right?

poeta_boy 11-24-2004 11:39 PM

Hola paisano!!

Si funcionó, muchisimas gracias :)

Ahora solo tengo que pelearme con el sql jajaja de casualidad no sabrás que significa el SQLState keycode: S1000 ???

poeta
-----------------------------------------

It did work, thanks a lot! now I just have to struggle with this sql thing.... any idea what SQLState keycode: S1000 stands for?
I'm using mySql and org.gjt.mm.mysql.Driver as a driver for Java

Thanks a lot!


All times are GMT -5. The time now is 10:22 AM.