How much logging should I do at production for SQLExcpetions?
https://softwareengineering.stackexchange.com/questions/7539
-
16-10-2019 - |
Question
I'm writing a small database-centric application in Java Swing that will be used internally in a small company. I haven't much experience with developing production-level applications.
I don't use an ORM, just a few simple methods that run SQL queries with Java JDBC. On every method that access the database I use a try-catch
statement for SQLException
and IOException
. Should I do any logging here? In case I should, how much should I log? Please give me examples of good practice in this case.
Solution
Two main things in production system are:
- Don't blow up the logs with information that is not interesting
- Allow raising the log level for troubleshooting purpose.
Use some kind of a logging infrastructure. If the exception can occur during the normal operation, report it in debug level. If it is a real problem, report it in error level. Either way you will have a way to troubleshoot the system by raising the log level.
Also - when you report an exception always make sure the the entire stack trace + inner exceptions are printed.
OTHER TIPS
IMHO, here is the very minimum you should do. I assume you are using Log4J.
try
{
// open connection
// begin transaction
// my database stuff
// commit transaction
}
catch (SQLException ex)
{
log.Error("An error occured while ...: " + ex.Message, ex);
// do stuff related to SQLException
// rollback transaction
}
catch (IOExcpetion ex)
{
log.Error("An error occured while ...: " + ex.Message, ex);
// do stuff related to IOException
// rollback transaction
}
catch (Exception ex)
{
log.Error("An error occured while ...: " + ex.Message, ex);
// manage this unhandled exception
// rollback transaction
}
finally
{
// close connection
}