Discussions

General J2EE: independent hibernate log file using log4j

  1. independent hibernate log file using log4j (2 messages)

    Hi, This is a beginner question and may be answered elsewhere please let me know where. Objective: To have independent log files at debug level for my app logs and Hibernate log data. I would like to have multiple log files that only allow certain data to be added at the debug level. e.g. my log file rollingLog.txt should only have entries from my classes and hibernateLog.txt should have log entries only from their classes org.hibernate. I do not want the Hibernate debug log data to be written to my log file. Problem: Using my current log4j.properties file if i set the logging level to debug for org.hibernate my application log file get written to by hibernate log data. I can not find a way to have both loggers set to debug and have their log data written to independent files. My current setup is to have the Hibernate log set to WARN so its data is less but i would really like to see DEBUG level data. Thank you in advance! Additional data and config: How i acquire the log object in code private static final Log _heartbeat = LogFactory.getLog("AppLogging"); private static final Log _log = LogFactory.getLog(MyObject.class); log4j.properties file contents: log4j.rootLogger = DEBUG, CONSOLE, com.mycompany, org.hibernate, A2 # Categories log4j.category.org.hibernate = WARN log4j.category.com.mycompany = DEBUG # CONSOLE is set to be ConsoleAppender sending its output to System.out log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.conversionPattern = [%p] [%d{yyyy-MM-dd @ HH:mm:ss}] [%t|%c{1}] %m%n log4j.appender.CONSOLE.threshold = INFO #log4j.logger.org.apache = ALL log4j.logger.org.hibernate = WARN log4j.additivity.org.hibernate = TRUE # Hibernate is set to be independant roll file log4j.appender.org.hibernate = org.apache.log4j.RollingFileAppender log4j.appender.org.hibernate.File = logs/hibernateLogFile.txt # Set the maximum log file size (use KB, MB or GB) log4j.appender.org.hibernate.MaxFileSize = 4096KB # Set the number of log files (0 means no backup files at all) log4j.appender.org.hibernate.MaxBackupIndex = 10 # Append to the end of the file or overwrites the file at start. log4j.appender.org.hibernate.Append = false log4j.appender.org.hibernate.layout = org.apache.log4j.PatternLayout log4j.appender.org.hibernate.layout.ConversionPattern = [%p] [%d{yyyy-MM-dd @ HH:mm:ss}] [%t|%c{1}] %m%n log4j.appender.org.hibernate.threshold = WARN #My application log4j.appender.com.mycompany = org.apache.log4j.RollingFileAppender log4j.appender.com.mycompany.File = logs/rollingLogFile.txt log4j.appender.com.mycompany.MaxFileSize = 4096KB log4j.appender.com.mycompany.MaxBackupIndex = 10 log4j.appender.com.mycompany.layout = org.apache.log4j.PatternLayout log4j.appender.com.mycompany.layout.ConversionPattern = [%p] [%d{yyyy-MM-dd @ HH:mm:ss}] [%t|%c{1}] %m%n log4j.additivity.com.mycompany = TRUE # An extra category to a log file - HEARTBEAT ONLY log4j.appender.WarnAppender = org.apache.log4j.RollingFileAppender log4j.category.AppLogging = A2 log4j.appender.A2.Threshold = INFO log4j.appender.A2 = org.apache.log4j.RollingFileAppender log4j.appender.A2.File = logs/heartbeat.txt log4j.appender.A2.MaxFileSize = 4096KB log4j.appender.A2.MaxBackupIndex = 1 log4j.appender.A2.layout = org.apache.log4j.PatternLayout log4j.appender.A2.layout.ConversionPattern = [%p] [%d{yyyy-MM-dd @ HH:mm:ss}] [%t|%c{1}] %m%n
  2. This is a log4j configuration for : - DEBUG level for only your classes logs into mycompanyLogFile.txt - DEBUG level for only hibernate logs into hibernateLogFile.txt - ERROR level for only other packages logs into errorsLogFile.txt log4j.rootLogger = ERROR, errorsLog # Hibernate logs log4j.logger.org.hibernate = DEBUG, hibernateLog log4j.additivity.org.hibernate = false # my company logs log4j.logger.com.mycompany = DEBUG, mycompanyLog log4j.additivity.com.mycompany = false # HIBERNATE APPENDER log4j.appender.hibernateLog = org.apache.log4j.RollingFileAppender log4j.appender.hibernateLog.File = logs/hibernateLogFile.txt # Set the maximum log file size (use KB, MB or GB) log4j.appender.hibernateLog.MaxFileSize = 4096KB # Set the number of log files (0 means no backup files at all) log4j.appender.hibernateLog.MaxBackupIndex = 10 # Append to the end of the file or overwrites the file at start. log4j.appender.hibernateLog.Append = false log4j.appender.hibernateLog.layout = org.apache.log4j.PatternLayout log4j.appender.hibernateLog.layout.ConversionPattern = [%p] [%d{yyyy-MM-dd @ HH:mm:ss}] [%t|%c{1}] %m%n #MY APPLICATION APPENDER log4j.appender.mycompanyLog = org.apache.log4j.RollingFileAppender log4j.appender.mycompanyLog.File = logs/mycompanyLogFile.txt log4j.appender.mycompanyLog.MaxFileSize = 4096KB log4j.appender.mycompanyLog.MaxBackupIndex = 10 log4j.appender.mycompanyLog.layout = org.apache.log4j.PatternLayout log4j.appender.mycompanyLog.layout.ConversionPattern = [%p] [%d{yyyy-MM-dd @ HH:mm:ss}] [%t|%c{1}] %m%n # ERRORS APPENDER log4j.appender.errorsLog = org.apache.log4j.RollingFileAppender log4j.appender.errorsLog.File = logs/errorsLogFile.txt log4j.appender.errorsLog.MaxFileSize = 4096KB log4j.appender.errorsLog.MaxBackupIndex = 1 log4j.appender.errorsLog.layout = org.apache.log4j.PatternLayout log4j.appender.errorsLog.layout.ConversionPattern = [%p] [%d{yyyy-MM-dd @ HH:mm:ss}] [%t|%c{1}] %m%n I hope this helps you. Fabien
  3. Hi Fabian,

                The solution you provided is indeed helpful. But after applying the same to my project, I can still see the SQL printing on my console rather than to the configured hibernate file. My Log4j.properties looks like following. I am trying to print all the hibernate logs to HIBERNATE.log file.

    log4j.rootCategory=ERROR, ROLLOVER

    log4j.appender.ROLLOVER=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.ROLLOVER.File=C:/workSpace/file_appender/ERROR.log  
    log4j.appender.ROLLOVER.DatePattern='.'yyyy-MM-dd
    log4j.appender.ROLLOVER.layout=org.apache.log4j.PatternLayout
    log4j.appender.ROLLOVER.layout.ConversionPattern=%d{dd MMM yyy HH:mm:ss},%-6p %C:%M:%L %m %n

    log4j.logger.org.hibernate=ERROR, hibernateLog
    log4j.additivity.org.hibernate=false
    log4j.appender.hibernateLog=org.apache.log4j.RollingFileAppender
    log4j.appender.hibernateLog.File=C:/workSpace/file_appender/HIBERNATE.log
    # Set the number of log files (0 means no backup files at all)
    log4j.appender.hibernateLog.MaxBackupIndex = 10
    log4j.appender.hibernateLog.Append=true
    log4j.appender.hibernateLog.layout=org.apache.log4j.PatternLayout
    log4j.appender.hibernateLog.layout.ConversionPattern = [%p] [%d{yyyy-MM-dd @ HH:mm:ss}] [%t|%c{1}] %m%n

    Thank you in advance.

    Amit.