简体   繁体   中英

Control SQL logging level in Catalina.out

Our app runs on Tomcat 8 and Linux. We have log4j.xml shipped with the war that controls the level of logging for our applications logging. The log4j also defines logging levels for SQL.

In hibernate.cfg.xml, the "hibernate.show_sql" is set to true.

<property name="hibernate.show_sql">true</property>

In $CATALINA_BASE/conf/logging.properties

All references to ConsoleAppender:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

Are removed, so nothing is sent to std out except for the SQL statements.

The: "hibernate.show_sql">true, sends all SQL statements being processed to catalina.out – please note that catalina.out is a file created only on Linux/Unix machines.

I read that "hibernate.show_sql">true, logs the SQL statements at DEBUG level. The problem is that I want to log those statements at ERROR level but don't know how to control the level?

I read that that level for SQL logging can be controlled by log4j.xml, but changing it in log4j.xml has no effect on how the catalina.out is being logged. Does anyone know how can I control the SQL logging in catalina.out to be only at ERROR level?

Below is log4j.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="mylog" class="org.apache.log4j.RollingFileAppender">        
    <param name="File" value="${catalina.base}/logs/mylog.log" />
    <param name="Threshold" value="TRACE" />
    <param name="Append" value="true" />
    <param name="MaxFileSize" value="300MB" />
    <param name="MaxBackupIndex" value="10" />
    <layout class="it.openutils.log4j.FilteredPatternLayout">
        <param name="ConversionPattern"
            value="%d{ISO8601} %5p [%t] Executor:%X{Executor} Type:%X{Type}     A:%X{Account} C:%X{Campaign} %c{1}:%L - %m%n" />            
    </layout>
</appender>

<logger name="org.hibernate.SQL" additivity="false">
    <level value="ERROR" />
    <appender-ref ref="mylog" />
</logger>

<logger name="org.hibernate">
        <level value="ERROR"/>
</logger>
<logger name="org.hibernate.jdbc.JDBCContext">
        <level value="ERROR"/>
</logger>

<root>
    <level value="info" />
    <appender-ref ref="mylog" />
</root>

Since I was not able to find anyway to set the level to ERROR for Catalina.out, I decided to turn off the SQL logging all together. So I changed all levels in log4j.xml back to INFO and set this property to false in hibernate.cfg.xml.

<property name="hibernate.show_sql">false</property>

After a while logging, I noticed that there is nothing being logged in Catalina.out unless an error happens, so in this case, the errors and exceptions are being logged into Catalina.out and not the regular SQL statements, which is (almost) something I was trying to achieve at the first place. I do not know why that's the case thou and where/how those logging levels are defined. But hope that helps somebody.

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