简体   繁体   中英

Hibernate does not log stacktrace

I have hibernate logs configured via log4j:

log4j.logger.org.hibernate=INFO

In case of error I only get exception message not full stacktrace:

[APPNAME] 2014-09-08 15:44:40,487 ERROR [http-bio-808-exec-3] SqlExceptionHelper.logExceptions(146) | ERROR: column user0_.nationality does not exist
  Position: 1365

because SqlExceptionHelper.logExceptions() has:

public void logExceptions(SQLException sqlException, String message) {
        if ( LOG.isEnabled( Level.ERROR ) ) {
            if ( LOG.isDebugEnabled() ) {
                message = StringHelper.isNotEmpty( message ) ? message : DEFAULT_EXCEPTION_MSG;
                LOG.debug( message, sqlException );
            }
            final boolean warnEnabled = LOG.isEnabled( Level.WARN );
            while ( sqlException != null ) {
                if ( warnEnabled ) {
                    LOG.warn( "SQL Error: " + sqlException.getErrorCode() + ", SQLState: " + sqlException.getSQLState() );
                }
                LOG.error( sqlException.getMessage() );
                sqlException = sqlException.getNextException();
            }
        }
    }

Now after I activate DEBUG for whole hibernate, it does print stackTrace then but have a lot of garbage logged. What can I do to log stackTraces only?

Marking logger for package have class SqlExceptionHelper to DEBUG can solve the problem but not sure I'll get all error logs hibernate ever produce.

Any help?

To see the stack trace, you need to use

LOG.error(sqlException.getMessage(), sqlException); 

Can you extend SqlExceptionHelper and override the logExceptions method so that it uses this line instead?

    #log4j.rootLogger=@replaced.log4j.rootLogger@
log4j.appender.logfile.File=@replaced.log4j.appender.logfile@

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender

log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files
log4j.appender.logfile.MaxBackupIndex=3
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
#Pattern to output : date priority [category] - <message>line_separator
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n

log4j.category.net.sf.acegisecurity=INFO
log4j.category.com.dc=DEBUG
log4j.category.org.apache.myfaces=INFO
log4j.category.javax.faces=INFO

# logs the SQL statements
#log4j.logger.org.hibernate.SQL=DEBUG

# Logs the JDBC parameters passed to a query
#log4j.logger.org.hibernate.type=TRACE
# Root logger option
log4j.rootLogger=INFO, stdout

Above log4j settings should help and then All you need to use is

LOGGER.error(e,e) //where e is exception object

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM