can't log Hibernate queries log4j - java

I am trying to log hibernate queries into a webapp specific log file.
I can log any message but I am still unable to log hibernate queries.
Them are being logged inside the server.log, even in a different file, but I need an app-specific log for each app running on the same server.
I am using log4j 1.2, I am able to create the log files, to log every information passed to console but hibernate queries.
Inside a database.properties file I found the hibernate.show_sql=true that is responsible for the logging output in console.
This is the kind of console's logging output I refer:
16:10:35,827 INFO [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select [...](here is outputted the correct query passed, with the ? of the prepared statement)
Anyway, what I need is to log that output.
I tried different log4j.xml settings, but still with no success, it creates log files but does not log those outputs.
Here is the current setting, that is logging everything inside the same file, when I will be able to log even those information I will go on separating the logs.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="mainFileAppender2" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="100MB" />
<param name="maxBackupIndex" value="50" />
<param name="File" value="${webapp.root}/WEB-INF/logs/mainCORE.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<appender name="journaldev-hibernate2" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${webapp.root}/WEB-INF/logs/hib-queriesCORE.log" />
<param name="Append" value="false"/><!--value="true" /-->
<param name="ImmediateFlush" value="true" />
<param name="MaxFileSize" value="200MB" />
<param name="MaxBackupIndex" value="50" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<root>
<priority value="debug"></priority>
<!-- <appender-ref ref="stdout"/> -->
<appender-ref ref="mainFileAppender2"/>
</root>
This configuration logs everything but those outputs. In the server.log are logged as follows:
16:10:35,827 INFO [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select [...](rest of the query)

Configure the logger for the following categories:
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
The first one will log the SQL-Statement (with ? for parameter values), the second one will print those parameter values if you need them.
So something like this should do the trick in your case:
<category name="org.hibernate.SQL">
<appender-ref ref="journaldev-hibernate2"/>
<priority value="DEBUG"/>
</category>
<category name="org.hibernate.type">
<appender-ref ref="journaldev-hibernate2"/>
<priority value="TRACE"/>
</category>

Solved this way; Anyway it also extracts the values extracted from the query
log = ${jboss.server.log.dir}/log/
#Loggers
log4j.rootLogger = INFO, FILE
log4j.logger.org.hibernate.SQL = DEBUG, HIBERNATE
log4j.logger.org.hibernate.type = TRACE, HIBERNATE
#Appenders
# Main appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=${log}/main.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d %-5p %c{1}:%L %m %n
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=true
log4j.appender.FILE.DatePattern=dd-MM-yyyy
# Hibernate queries appender
log4j.appender.HIBERNATE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HIBERNATE.File=${log}/hib-queries.log
log4j.appender.HIBERNATE.layout=org.apache.log4j.PatternLayout
log4j.appender.HIBERNATE.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.appender.HIBERNATE.ImmediateFlush=true
log4j.appender.HIBERNATE.Threshold=debug
log4j.appender.HIBERNATE.Append=true
log4j.appender.HIBERNATE.DatePattern=dd-MM-yyyy

Related

How to separate web application logging in Tomcat

I have my webapp some-app.war running under Tomcat 8.5. I use Slf4j and Logback for logging within the webapp. When I log using log.info() or similar, the output gets written to catalina.out, the same log file as Tomcat uses for its internal logging. I want the logs from my application to be written to a separate log file. But I do NOT want to use logback's FileAppender to do it. I'd prefer to leave Logback using ConsoleAppender as I may deliver the WAR to others who won't have the same logging needs. I see the location of the log file as an aspect of the deployment environment, not the code.
Is there a way that I can manage per-application log files from the configuration of the Tomcat server itself -- while still using Slf4j and Logback in my code?
You can't know about others' logging needs, so there's not a silver bullet solution for this. However I'm sure the best option is not "I'm gonna leave it configured really badly because someone might log differently".
You can configure it how you wish for yourself, using FileAppenders and document the necessary steps to reconfigure it (e.g. edit the war).
You can also use more advanced configurations such as making use of syslog. Now other users have a standardized configuration option, just as long as they use syslog.
There are other options as well, but the common thread here is that you can't know what others may want, so don't waste time trying to guess it. Just do what works well for you at least.
If you placed logging.properties at [CATALINA_HOME/conf/logging.properties] then it should use it as your root logging properties file. Put you logging.properties or log.xml file in your application at [MyApp/WEB_INF/classes/logging.properties] and configure it like blow.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="stderr" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="Target" value="System.err"/>
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="Target" value="System.out"/>
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="maxFileSize" value="2MB" />
<param name="maxBackupIndex" value="100" />
<param name="file" value="${catalina.base}/logs/MyApp/MyApp.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="stdout" />
<appender-ref ref="stderr" />
<appender-ref ref="file" />
</root>
</log4j:configuration>

Unable to print debug statements in the file

I am using log4j in my java application to log the messages.log4j-1.2.17.jar is the jar file used. Below is the log file.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO" />
<param name="Threshold" value="debug" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="D/logs/projectLog.log"/>
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<root>
<priority value="info"></priority>
<appender-ref ref="fileAppender" />
</root>
</log4j:configuration>
I am using above log file in my application. Log file got generated successfuly but in the log file , debug messages are not getting printed.
In my java class,below are the statements i have included.
LOGGER.info("In TestMVCController, logger info");
System.out.println("is debug enabled" + LOGGER.isDebugEnabled());//always false even when server is started in debug mode
LOGGER.debug("In TestMVCController, logger debug");
LOGGER.error("In TestMVCController, logger errror");
In my log file, I can see only info and error messages being printed. In which scenario does the debug statements print and how can i make my log statements to print debug statements?Any advice would be helpful.
Change the root's priority value from "info" to "debug":
<root>
<priority value="debug"></priority>
<appender-ref ref="fileAppender" />
</root>
This will configure the root logger to send log messages at level "DEBUG" or higher to the appender "fileAppender". The "fileAppender" can then set the threshold it wants to log (DEBUG, INFO, WARNING etc).
Note: The appender's "threshold" parameter is optional. If you omit it, the appender will log at the level defined by the root logger. The "threshold" can be used if there are multiple appenders. For example: DEBUG for a console appender and INFO for a file appender.

Duplicate log4j lines in console

Although similar questions were asked, I couldn't find a solution. I guess because the configuration varies.
I am using a 3rd party jar, but I had the same problem also when using log4j directly.
I am working on Eclipse Mars.1
My log4j.properties is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<root>
<level value="ALL" />
<appender-ref ref="consoleAppender"/>
<appender-ref ref="fileAppender"/>
</root>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender" additivity="false">
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%X{ThreadId}] %X{MethodName} - %m%n" />
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender" additivity="false">
<param name="Threshold" value="DEBUG" />
<param name="append" value="true" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="10" />
<param name="File" value="c:\Users\administrator\Eclipse workspaces\mylog-${current.date}.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%X{ThreadId}] %X{MethodName} - %m%n" />
</layout>
</appender>
</log4j:configuration>
And here is an example of what I see in the eclipse console:
2015-11-16 17:13:33 DEBUG [] - ConferenceId = 4618311
13082 [main] DEBUG com.log.Log4jWrapperSingleton - ConferenceId = 4618311
13082 [main] DEBUG com.log.Log4jWrapperSingleton - ConferenceId = 4618311
13082 [main] DEBUG com.log.Log4jWrapperSingleton - ConferenceId = 4618311
2015-11-16 17:13:33 INFO [] - SLEEP_BETWEEN_CREATE_TO_TERMINATE_SEC waiting 2000 ms...
13083 [main] INFO com.log.Log4jWrapperSingleton - SLEEP_BETWEEN_CREATE_TO_TERMINATE_SEC waiting 2000 ms...
13083 [main] INFO com.log.Log4jWrapperSingleton - SLEEP_BETWEEN_CREATE_TO_TERMINATE_SEC waiting 2000 ms...
13083 [main] INFO com.log.Log4jWrapperSingleton - SLEEP_BETWEEN_CREATE_TO_TERMINATE_SEC waiting 2000 ms...
And each iteration of code one more duplication is added. If I have now 3 duplications of "SLEEP_BETWEEN_CREATE_TO_TERMINATE_SEC waiting", next loop I will have 4 of them...
Can you advise please?
Thank you.
The problem I'd expect is in your custom Log4j wrapper: com.log.Log4jWrapperSingleton
And you should get rid of it. It is very likely serving new instance of it self every time it is called effectivelly adding new logger to the ConsoleAppender (we can only guess until we see the source)
You can find reasoning here: Benefits of Log4j singleton wrapper?

log4j appender priority conflict?

I have this log4j.properties:
# Global logging configuration
log4j.rootLogger=WARN, file
### Direct log messages to a log file
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File= C:\\eclipse\\servers\\apache-tomcat-6.0.39\\logs\\log4j.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
### Console messages Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
### Appender-to-Class definition
log4j.logger.com.XXX.payplatform.test.*=INFO, console
log4j.additivity.com.XXX.payplatform.test.*=false
So, ok, the WARN level information is being registered in the log4j.log file. That's OK.
But, I want to see INFO-level messages in my eclipse console, but i am not getting any response there.
Any solutions? I think the problem is in the "WARN, file" rootLogger configuration, so i tried this:
# Global logging configuration
log4j.rootLogger=INFO, file
But no changes resulted...
Thank you!
Read more Apache log4j 1.2 - Short introduction to log4j and find the samples as well.
Level Inheritance
The inherited level for a given logger C, is equal to the first non-null level in the logger hierarchy, starting at C and proceeding upwards in the hierarchy towards the root logger.
Basic Selection Rule
A log request of level p in a logger with (either assigned or inherited, whichever is appropriate) level q, is enabled if p >= q.
This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL.
It should be like this. Add console here and set default root log level to INFO.
log4j.rootLogger=INFO, file, console
OR
Try with log4j.xml instead of log4j.properties. Read more Configuring Log4j 2
Sample log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="FILE_LOG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/log4j.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true"/>
<param name="DatePattern" value="'.'dd-MM-yyyy-HH"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<category name="com.XXX.payplatform.test">
<priority value="INFO" />
<appender-ref ref="STDOUT"/>
</category>
<root>
<priority value ="ERROR" />
<appender-ref ref="FILE_LOG" />
</root>
</log4j:configuration>

Why is log4j logging to the console?

This is a standalone java application.
I am using the configuration file below and having two problems.
1) I'm getting logs to stdout and I don't know why.
2) I'm getting ALL log messages in my error log even though I have tried to direct only error and higher to the error log.
I am using the BasicConfigurator without specifying an explicit path to the log4j.xml file. The xml file is in the same jar as my classes. It is creating and writing to the appropriate logs in addition to these problems so the configuration is being applied.
3) In addition, I have had no luck having the log4j.xml file outside of the jar so I can change it at runtime. How do I do that?
<!--appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p [%F:%L] - %m%n"/>
</layout>
</appender-->
<!-- working dir is $CATALINA_TMPDIR. send logs to log dir. -->
<appender name="ROLL" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/var/log/company/application.log"/>
<param name="MaxFileSize" value="5MB"/>
<param name="MaxBackupIndex" value="9"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<appender name="ERRORLOG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/var/log/rocketvox/company/error.log"/>
<param name="MaxFileSize" value="5MB"/>
<param name="MaxBackupIndex" value="9"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<category name="com.company">
<priority value="ALL"/>
<appender-ref ref="ROLL"/>
</category>
<category name="com.mattx">
<priority value="ALL"/>
<appender-ref ref="ROLL"/>
</category>
<root>
<priority value="error"/>
<appender-ref ref="ERRORLOG"/>
</root>
Add -Dlog4j.debug to the application's JVM args to see exactly what log4j is doing and which configuration file it uses.
Your problem is using BasicConfigurer - for configuring using a file named log4j.xml, you don't need to use any explicit log4j Configurer in your application code, as the default log4j initialization logic will kick in to pick log4j.xml (or log4j.properties, if no xml is found) from the classpath.
See Default Initialization Procedure from the log4j manual.

Categories

Resources