Handled exceptions with db __del__ method. Also fixed a bug with the logging output in db class.