I have a hibernate standard java application (not a webapp). Logging already works by default. I would like to see the parameters passed in the sql queries. I have researched that in order to that I need to enable logging. It seems as if hibernate uses slf4j. I have downloaded that jar and its accompanying slf4j-log4j jar. I have added these jars to the classpath. I have also added a log4j.properties to the root of the eclipse project.
I can't seem to have the project recognize that it needs to use slf4j and its properties file.
Do I need to add a reference in my hibernate.cfg.xml?
Make sure you have the property hibernate.show_sql set to true.
format_sql set to true will make the SQL be formatted a bit more pretty.
You may also need to log on TRACE level.
For more details have a look here or here.
Related
I have update our code from log4j to log4j 2.17.1 And I want to stored the log file to servers under the Apache tomcat. I am using the log4j2.properties mentioned below.
When I run the code, then the logs file is printed in under code structure(see in below attached screenshot)but I want to print the logs file in QA-Servers under apache tomcat.
Please help me to solve the issue.
TL;DR: use ${sys:catalina.base}.
The property substitution in Log4j 2.x differs from Log4j 1.x (cf. documentation). The most prominent change is that:
in Log4j 1.x ${catalina.base} is looked up in Java system properties and, if the system property does not exist, in the configuration file,
in Log4j 2.x ${catalina.base} is looked up only in the configuration file.
In both cases if the property can not be resolved the placeholder is left unchanged.
In Log4j 2.x all external property lookups must be prefixed using an appropriate prefix. The exact equivalent of Log4j 1.x behavior is ${sys:catalina.base}. Therefore you can use:
# Fallback
property.catalina.base=.
appender.rolling.fileName=${sys:catalina.base}/logs/aseq_wiptmobile_qa-1.applog
I checked log4j setup in my tomcat environment. Although there is no log4j-web.jar file in my webapp or in "common libs" folder, log4j is initialized properly. I only added log4j.properties in my classpath. And that's it, log4j is working.
I am using spring mvc, sl4j-api.jar and sl4j-jcl.jar are in my classpath.
My question is : How does log4j work properly and pickup the configuration?
You say that you used log4.properties so I will be assuming you are using log4j 1.2.x (log4 2 use log4j2.properties)
Log4 perform default initialization when log4j classes are loaded into memory within the static initializer of the LogManager class.
See the section: Default Initialization Procedure in the manual
As soon as you dropped log4j.properties into your classpath it trigger log4j logging.
This procedure will be executed in any environment (tomcat container or other).
if you want to skip this procedure please note item 1 in the procedure:
Setting the log4j.defaultInitOverride system property to any other
value then "false" will cause log4j to skip the default initialization
procedure (this procedure).
Log4Web is an extension for depending on log4j 2 so It's irrelevant in your case.
sl4j-api.jar and sl4j-jcl.jar requires log4j-over-slf4j if you what to migrate existing code to use SLF4J without changing the code itself.
If you are not interest in such migration you may ignore them.
I have crearte log4j.properties file like below:
log4j.logger.org.hibernate=INFO, hb
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.hql=debug
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug
log4j.appender.hb=org.apache.log4j.ConsoleAppender
log4j.appender.hb.layout=org.apache.log4j.PatternLayout
log4j.appender.hb.layout.ConversionPattern=HibernateLog --> %d{HH:mm:ss} %-5p %c - %m%n
log4j.appender.hb.Threshold=TRACE
Can someone help me to how to include it in hibernate.cfg.xml file? I am sorry I actually don't know how log4j works. I create this to display my hibernate query with value instead of ? but still it displays ? nothing changes so what I need to proceed further?
I took reference from here Hibernate show real SQL
The 'show_sql' property in your hibernate.cfg.xml causes the queries to be printed directly to the console.
Log4j allows output to be logged anywhere from console to file to network ports to databases.
But the simple configuration that you have is supposed to print on the console as well. So first remove the show_sql property to see if Log4j puts anything on the console at all.
If this doesn't work, it shows that Log4j is not configured correctly. If you're using hibernate > 3.5, it uses the slf4j api, which uses logback by default instead of log4j.
You can easily switch to log4j by removing logback jar(s) from your classpath, and adding slf4j-log4j12.jar and log4j.jar instead.
The Log4j tracing also prints the queries using '?', but it also prints the parameter bindings, i.e. what the '?' will be replaced with by the database driver or server.
Please refer hibernate reference as per your version. Here is 3.3 Link
Edit :-
In order to setup logging you will need slf4j-api.jar in your
classpath together with the jar file for your preferred binding -
slf4j-log4j12.jar in the case of Log4J.
Put log4j.properties file in your classpath. An example properties file is distributed
with Hibernate in the src/ directory.
Enable following log 4j categories.
org.hibernate.SQL Log all SQL DML statements as they are executed
org.hibernate.type Log all JDBC parameters
In most cases it should be sufficient to include log4j.properties in application's classpath.
See How to initialize log4j properly?.
The link in question already suggests that you should look for bound sql param values not in sql text by in separate log statements like this:
TRACE [BasicBinder] binding parameter [1] as [VARCHAR] - john doe.
But I recommend to stick with log4jdbc. It's pretty simple to use. And it's able to inline query param values in printed sql text.
Add this also
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
your problem will be fixed.
No need to add in hibernate.cfg.xml, just place it to classpath and follow the steps mentioned in this link.
refer this link :- http://makecodeeasy.blogspot.in/2013/03/logger-in-spring-web-application.html
You can't print the values inline. Hibernate will always print the ? when logging the queries. The output of org.hibernate.type logging at the DEBUG level shows the values and types that are used to replace those question marks.
i like sql logging with proxy of jdbcdslog much more than hibernate logging.
Log SQL that is communicated with the database
In my case it wasn't hibernate.cfg.xml, it was log4j.xml file where I put the code given by #jdev
< property name="show_sql">true
Shows SQL queries
< property name="format_sql">true
Format the query shown on console
< property name="use_sql_comments">true
comments will be added to the queries
If your project is having 1000s of queries printing to the console, make others false:
< property name="show_sql">true
< property name="format_sql">false
< property name="use_sql_comments">false
OR write an appender to the log4j.xml which I have done in my project, just to write all SQL queries on separate text file which is specified here:
Configuring Hibernate logging using Log4j XML config file?
I had the following working organization
src/main/resources/log4j.xml
src/main/resources/hibernate.properties
I wanted to reorganize my webapp as follow:
src/main/resources/log/log4j.xml
src/main/resources/orm/hibernate.properties
The Logger.info("foobar") still logs well (after having set the log4jConfigLocation context parameter), and the app still has a working database connection.
The problem is that Hibernate doesn't log anything anymore, even if hibernate.show_sql is set to true. Any idea why? Should specify the new path to the log4j.xml file to Hibernate? If yes, how?
You could run your server with this VM argument:-
-Dlog4j.configuration=/path/to/log4j.xml
I usually tend to place the log4j.xml at the recommended default location unless there's a need to do otherwise... "convention over configuration" is important especially if other peers may be working on the same project in the future.
Log4j first looks for its configuration by looking at the system property "log4j.configuration". If that system property is not set, it looks for a log4j.properties or a log4j.xml file on the classpath.
So, if you really want the log4j.xml at src/main/resources/log/log4j.xml, you will have to set the log4j configuration system property. This is basically what limc does by supplying it as a vm argument.
Also like limc says, you should probably just keep the log4j.xml at the default location.
I have a project A with log4j.jar on its build path. I have a number of classes that have logging statements in the form of:
Logger _log = Logger.getLogger(A.<>.class);
...
_log.info("...");
I am exporting the project as a jar into another project B. Project B already has its own log4j jar and it's own .xml configuration file. I want to configure particular classes from A to log to Console Apender at varying "levels". How do I do this, please?
Well, basically, you shouldn't do that. Think of it this way: if that would be done that way, each library that is included in any application would host its own logging configuration, very potentially overriding any of those that are in the application, and each other, in non-specified order. You wouldn't want that. So do not.
[In case you really, really want to do it, you could have properties file in the jar that could be overriden by an xml file in the main application. See details here. But don't. :) ]
In general, you don't. In general, you want to have one log4j configuration file. But i suppose you could load your configuration explicitly from your code, as described in the log4j documentation
Pick a version of log4j.jar, probably the newer one. You can only use one version. You need to merge project A's and project B's logging configuration. You could do this in log4j classes at runtime, but don't. Just bite the bullet and merge them once in source control.