Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 28 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Sun Dec 26, 2010 7:38 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
I finally could get the Hibernate logging with the sql binding parameters.

For the record, I do need to have the following properties in the Spring Hibernate configuration bean:
Code:
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>

otherwise there is no Hibernate logging output.

Then I need the following setup in the log4j.xml coonfiguration file:
Code:
   <appender name="sqlAppender" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d %-5p  [%c{1}] %m %n" />
      </layout>
   </appender>

   <category name="org.hibernate.SQL" additivity="false">
                <level value="ALL" />
      <appender-ref ref="sqlAppender" />
   </category>

   <category name="org.hibernate.type" additivity="false">
                <level value="ALL" />
      <appender-ref ref="sqlAppender" />
   </category>


Then the pom.xml file shall contain the following dependencies:
Code:
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
      </dependency>

Note that the log4j dependency is not needed.

On a side note, to manually log in, do like:
Code:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static Logger logger = LoggerFactory.getLogger(ContactRefererDaoTest.class);


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Sun Dec 26, 2010 7:40 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
The only glitch remaining is that all sql logging is done twice, once without the binding parameters, and once with.

For example, one output is:
Quote:
2010-12-26 12:38:02,001 DEBUG [SQL]
delete
from
link_category
where
id=?
and version=?
Hibernate:
delete
from
link_category
where
id=?
and version=?
2010-12-26 12:38:02,003 TRACE [BasicBinder] binding parameter [1] as [INTEGER] - 419
2010-12-26 12:38:02,003 TRACE [BasicBinder] binding parameter [2] as [INTEGER] - 0
2010-12-26 12:38:02,005 DEBUG [SQL]


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Sun Dec 26, 2010 7:46 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
And I also solved the logging done twice. It was because the sqlAppender was called twice :-)

So instead of:
Code:
   <category name="org.hibernate.SQL" additivity="false">
        <level value="ALL" />
      <appender-ref ref="sqlAppender" />
   </category>

   <category name="org.hibernate.type" additivity="false">
        <level value="ALL" />
      <appender-ref ref="sqlAppender" />
   </category>


I now have:
Code:
   <category name="org.hibernate.type" additivity="false">
        <level value="ALL" />
      <appender-ref ref="sqlAppender" />
   </category>

and all is well, each sql statement is logged once with the sql binding parameters.


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Fri Mar 15, 2013 6:23 am 
Newbie

Joined: Wed Oct 13, 2010 6:53 am
Posts: 6
Hi,
With the above approach,Bind sql paramaters are not printing in log if log4j.xml is externalized.
Can anybody help how to Bind sql paramaters in logs if log4j is externalized?
regards
Subhashree


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Fri Mar 15, 2013 6:29 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hi,

What do you mean by: is externalized ?

Kind Regards,


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Fri Mar 15, 2013 7:27 am 
Newbie

Joined: Wed Oct 13, 2010 6:53 am
Posts: 6
hi,
Normally Log4j.xml file resides in ../../src/config folder of the project.If someone want to change the log4j.xml setting,then it has to be re build.
But I want to keep it outside the project(external to project).Anyone can change the inside setting for log4j.xml the way they want to represent the logs.In this scenario,binding parameters are not printing in log files it creats.
Hope u understand this.

regards
Subhashree


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Fri Mar 15, 2013 7:58 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
I think the location where the log4j configuration files resides has no side effect on its configuration content. Can you make sure you do not have another such file sitting somewhere that takes precedence ?


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Fri Mar 15, 2013 8:08 am 
Newbie

Joined: Wed Oct 13, 2010 6:53 am
Posts: 6
hi,
in POM.xml file i have excluded the log4j.properties which is inside ../src/main/resources/
See below:
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/log4j.properties</exclude>
</excludes>
</resource>
So there is no point to take precedence.
regards
Subhashree


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Fri Mar 15, 2013 8:16 am 
Newbie

Joined: Wed Oct 13, 2010 6:53 am
Posts: 6
hi,
For your information to implement externalization of log.
i have done below changes:
1)in Spring-presentation layer
<bean id="log4jInitialization"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments" ref="log4jPropPath"/>
</bean>

<bean id="log4jPropPath" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/log4jPropPath" />
</bean>

2) In web.xml file below entry is made
<resource-ref>
<res-ref-name>log4jPropPath</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>

3)Exclude Log4j.properties from pom.xml file:
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/log4j.properties</exclude>
</excludes>
</resource>
4)i have kept my log4.xml file outside the project path that is C:\Users\JTBox\projects\log4j\
5)i am using tomcat server.
In the context file i have made an entry that is <Environment name="log4jPropPath" type="java.lang.String" value="C:\Users\JTBox\projects\log4j\log4j.xml" />


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Fri Mar 15, 2013 10:55 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
And you assure us that if you move that log4j file within your code base then the sql output works fine ?


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Sun Mar 17, 2013 11:37 pm 
Newbie

Joined: Wed Oct 13, 2010 6:53 am
Posts: 6
yes.if i move the log4j file within code base,binding values are printing in log files


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Mon Mar 18, 2013 1:40 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Could it be because when sitting externally, the log4j configuration file is loaded after something else, that is, too late to be picked up ? It's just a wild guess...


Top
 Profile  
 
 Post subject: Re: Bind sql statement parameters with log4j.xml
PostPosted: Mon Mar 18, 2013 8:06 am 
Newbie

Joined: Wed Oct 13, 2010 6:53 am
Posts: 6
Yes. i am also thinking the same.
This is my web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>BDR - iC Referential</display-name>

<!-- Parameters -->

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring/spring.xml</param-value>
</context-param>

<!-- Filters -->

<filter>
<filter-name>SpringCharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>AcegiHTTPRequestSecurityFilter</filter-name>
<filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
<init-param>
<param-name>targetClass</param-name>
<param-value>
org.acegisecurity.util.FilterChainProxy
</param-value>
</init-param>
</filter>

<filter>
<filter-name>ResponseOverrideFilter</filter-name>
<filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class>
</filter>

<filter>
<filter-name>AjaxContentFilter</filter-name>
<filter-class>
com.sgcib.crs.dms.bdr.framework.presentation.filter.AjaxFilter</filter-class>
<init-param>
<param-name>urlPart</param-name>
<param-value>/ajaxExt/</param-value>
</init-param>
</filter>

<!-- Listeners -->

<!--
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
-->

<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!-- Filter mappings -->

<filter-mapping>
<filter-name>SpringCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>AcegiHTTPRequestSecurityFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<filter-mapping>
<filter-name>ResponseOverrideFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>ResponseOverrideFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>AjaxContentFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!-- Servlets -->



<servlet>
<servlet-name>WSServlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>



<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/config/struts/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<jsp-config>
<!-- Taglibs Struts -->
<taglib>
<taglib-uri>struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-logic</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
</taglib>

<!-- DisplayTag -->
<taglib>
<taglib-uri>displaytag</taglib-uri>
<taglib-location>/WEB-INF/tld/displaytag.tld</taglib-location>
</taglib>

<!-- Acegi -->
<taglib>
<taglib-uri>authz</taglib-uri>
<taglib-location>/WEB-INF/tld/authz-1.0.0.tld</taglib-location>
</taglib>

<!-- ctag -->
<taglib>
<taglib-uri>ctag</taglib-uri>
<taglib-location>/WEB-INF/tld/c-1_0-rt.tld</taglib-location>
</taglib>

</jsp-config>
<!--
<servlet> <servlet-name>WSServlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup> </servlet>
-->
<!-- Jawr servlets -->
<servlet>
<servlet-name>JavascriptServlet</servlet-name>
<servlet-class>net.jawr.web.servlet.JawrServlet</servlet-class>
<init-param>
<param-name>mapping</param-name>
<param-value>/js/</param-value>
</init-param>

<init-param>
<param-name>configLocation</param-name>
<param-value>jawr.properties</param-value>
</init-param>
<init-param>
<param-name>configPropertiesSourceClass</param-name>
<param-value>com.sgcib.crs.dms.bdr.framework.presentation.jawr.JawrConfigPropertiesSource</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet>
<servlet-name>ImgServlet</servlet-name>
<servlet-class>net.jawr.web.servlet.JawrServlet</servlet-class>

<init-param>
<param-name>configLocation</param-name>
<param-value>jawr.properties</param-value>
</init-param>
<init-param>
<param-name>configPropertiesSourceClass</param-name>
<param-value>com.sgcib.crs.dms.bdr.framework.presentation.jawr.JawrConfigPropertiesSource</param-value>
</init-param>
<init-param>
<param-name>type</param-name>
<param-value>img</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet>
<servlet-name>CSSServlet</servlet-name>
<servlet-class>net.jawr.web.servlet.JawrServlet</servlet-class>
<init-param>
<param-name>mapping</param-name>
<param-value>/css/</param-value>
</init-param>
<init-param>
<param-name>configLocation</param-name>
<param-value>jawr.properties</param-value>
</init-param>
<init-param>
<param-name>configPropertiesSourceClass</param-name>
<param-value>com.sgcib.crs.dms.bdr.framework.presentation.jawr.JawrConfigPropertiesSource</param-value>
</init-param>
<init-param>
<param-name>type</param-name>
<param-value>css</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<!-- Servlet mappings -->


<servlet-mapping>
<servlet-name>WSServlet</servlet-name>
<url-pattern>/CommercialProspect</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>WSServlet</servlet-name>
<url-pattern>/CommercialCustomList</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>JavascriptServlet</servlet-name>
<url-pattern>/js/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CSSServlet</servlet-name>
<url-pattern>/css/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>ImgServlet</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ImgServlet</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ImgServlet</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ImgServlet</servlet-name>
<url-pattern>*.jpeg</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>

<error-page>
<error-code>403</error-code>
<location>/basics/jsp/errors/403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/basics/jsp/errors/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/basics/jsp/errors/500.jsp</location>
</error-page>

<session-config>
<session-timeout>180</session-timeout>
</session-config>

<resource-ref>
<res-ref-name>jdbc/bdrweb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!-- RTFE Entires -->
<resource-ref>
<res-ref-name>rtfeStore</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>rtfeAppCode</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>rtfeUrl</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>sesamePasswordResetUrl</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<!-- BDR Portal Entry -->
<resource-ref>
<res-ref-name>bdrPortalUrl</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>envName</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<!-- Prospect Ws Entry -->
<resource-ref>
<res-ref-name>prospectWebserviceUserName</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>prospectWebservicePassword</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>prospectWebserviceUrl</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>bdrEnvironment</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>tempDir</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!-- Externalization Log4j Entry -->
<resource-ref>
<res-ref-name>log4jPropPath</res-ref-name>
<res-type>java.lang.String</res-type>
<res-auth>Container</res-auth>
</resource-ref>

</web-app>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 28 posts ]  Go to page Previous  1, 2

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.