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?
Related
In my Tomcat application, I want to use two loggers to log general event informations and errors to two different files.
Logger eventLogger = Logger.getLogger("event");
Logger errorLogger = Logger.getLogger("error");
Now I want to do the following: the logs of the eventLogger should be written to a "events.log" file, and the errorLogger logs should be written in a "errors.log" file. All logs from any other logger (e.g. Tomcat logs) should be written to catalina.out (or any other default file).
How can I achive this with JULI? Or do I need a third party lib?
This can't be achieved with Juli. You might use log4j.
Basically you need appenders. They will manage the logs to go to separate files. Example configuration in xml:
<?xml version="1.0"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.EnhancedPatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%t][%c{1}:%L] %m%n" />
</layout>
</appender>
<appender name="eventFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="#tomcat.home#/logs/event.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<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>
<appender name="errorFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="#tomcat.home#/logs/error.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<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>
<logger name="event" additivity="false">
<level value="INFO" />
<appender-ref ref="eventFILE" />
</logger>
<logger name="error" additivity="false">
<level value="INFO" />
<appender-ref ref="errorFILE" />
</logger>
<root>
<priority value="INFO" />
<appender-ref ref="FILE" />
</root>
</log4j:configuration>
you have to define an appender for each file in your log4j.xml and connect them to your loggers:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p [%c.%M:%L] - %m%n" />
</layout>
</appender>
<appender name="FILE-error" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.base}/logs/error.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="ImmediateFlush" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p [%c.%M:%L] - %m%n" />
</layout>
</appender>
<appender name="FILE-event" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.base}/logs/event.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="ImmediateFlush" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss};%m%n" />
</layout>
</appender>
<!-- Loggers -->
<logger name="error" additivity="false">
<level value="warn" />
<appender-ref ref="FILE-error" />
</logger>
<logger name="event" additivity="false">
<level value="info" />
<appender-ref ref="FILE-event" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="FILE-error" />
</root>
</log4j:configuration>
Tomcat documentation does not provide any way to define more than one file per application when using JULI. Actually it provides the steps to use log4j instead of it because JULI configuration can be too basic.
But again this configuration is at container level:
Note: The steps described in this section are needed when you want to
reconfigure Tomcat to use Apache log4j for its own logging. These
steps are not needed if you just want to use log4j in your own web
application. — In that case, just put log4j.jar and log4j.properties
into WEB-INF/lib and WEB-INF/classes of your web application.
So using a third party library is the logical choice and you can find how to configure it in this SO post
Tomcat JULI logging supports this only via the logging.properties located in the ${catalina.base}/config folder.
#Declares the handlers allowed for use.
handlers = 100catalina.org.apache.juli.FileHandler, 200catalina.org.apache.juli.FileHandler, 300catalina.org.apache.juli.FileHandler
#Install the 'all' hander on the root logger.
.handlers=300catalina.org.apache.juli.FileHandler
#Install the 'event' handler on the 'event' logger and don't report to root.
event.handlers=100catalina.org.apache.juli.FileHandler
event.useParentHandlers=false
#Install the 'error' handler on the 'error' logger and don't report to root.
error.handlers=200catalina.org.apache.juli.FileHandler
error.useParentHandlers=false
#Event handler settings.
100catalina.org.apache.juli.FileHandler.level = ALL
100catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
100catalina.org.apache.juli.FileHandler.prefix = event.
#Error handler settings.
200catalina.org.apache.juli.FileHandler.level = ALL
200catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
200catalina.org.apache.juli.FileHandler.prefix = error.
#Root handler settings.
300catalina.org.apache.juli.FileHandler.level = ALL
300catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
300catalina.org.apache.juli.FileHandler.prefix = all.
This config will not work if you try to install this file in the WEB-INF/classes/logging.properties. This is because the ClassLoaderLogManager doesn't allow you to install handlers on named loggers.
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.
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
I've tried I think all the info I've found in the Internet, with no results. I want to switch off the Hibernate Logging.
My log4j.properties:
log4j.logger.org.hibernate = INFO
log4j.logger.org.hibernate.SQL=INFO, SQL_APPENDER
log4j.additivity.org.hibernate.SQL=false
log4j.rootLogger=INFO
My log4.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<logger name="org.hibernate">
<level value="info"/>
</logger>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false"/>
<param name="file" value="out/learning.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>
Inside my hibernate.cfg.xml:
<property name="hibernate.show_sql">false</property>
<property name="hibernate.generate_statistics">false</property>
<property name="hibernate.use_sql_comments">false</property>
At this moment I don't know what else can I do to stop those disturbing messages on my console.
Thanks in advance for your help.
try to use OFF level for each appender might help
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html
I would encourage you to read the SLF4J Manual, you should only have 1 Binding jar on your classpath.
Remove slf4-simple-1.4.2.jar from your classpath, it may be stopping log4j being used.
slf4j-simple-1.7.5.jar
Binding for Simple implementation, which outputs all events to System.err. Only messages of level INFO and
higher are printed. This binding may be useful in the context of small
applications.
Console is getting filled with Quartz WARN loggers all the time and it's really annoying to developers who work in the project to find other logger messages in console.
[2013-09-14 11:18:35,142] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'system-id'
[2013-09-14 11:18:35,142] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'end-at'
[2013-09-14 11:18:35,143] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'cron-expression'
[2013-09-14 11:18:35,144] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'start-at'
[2013-09-14 11:18:35,144] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'scheduled-job-id'
[2013-09-14 11:18:35,144] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'size'
[2013-09-14 11:18:40,086] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'GNS'
[2013-09-14 11:18:40,087] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'limit'
[2013-09-14 11:18:40,087] WARN {org.quartz.simpl.PropertySettingJobFactory} - No setter on Job class lk.gov.elg.admin.action.detain.DetainJob for property 'tenantId'
I Google and find a way to do but it didn't give the solution.
<logger name="org.quartz">
<level value="info" />
</logger>
We are using log4j.xml instead of log4j.properties.
Here is the snapshot of log4j.xml 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/">
<!--
Read http://logging.apache.org/log4j/docs/api/org/apache/log4j/DailyRollingFileAppender.html
for more information on DaliyRollingFileAppender configuration options.
-->
<appender name="error" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/elg/logs/scandium-error.log"/>
<param name="Threshold" value="error"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p - [%t] [%x] %m%n"/>
</layout>
</appender>
<appender name="debug" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/elg/logs/scandium-debug.log"/>
<param name="Threshold" value="trace"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p [%t] - %c{1} [%x] - %m%n"/>
</layout>
</appender>
<appender name="info" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/elg/logs/scandium-info.log"/>
<param name="Threshold" value="info"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} - [%t] [%x] %m%n"/>
</layout>
</appender>
<appender name="trace" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/elg/logs/scandium-trace.log"/>
<param name="Threshold" value="info"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %c{1} - [%t] [%x] %m%n"/>
</layout>
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="info"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1} - [%t] [%x] %m%n"/>
</layout>
</appender>
<category name="com.opensymphony.xwork2.ognl.OgnlValueStack">
<priority value="error"/>
<appender-ref ref="error"/>
<appender-ref ref="console"/>
</category>
<root>
<priority value="trace"/>
<appender-ref ref="info"/>
<appender-ref ref="trace"/>
<appender-ref ref="debug"/>
<appender-ref ref="error"/>
<appender-ref ref="console"/>
</root>
<logger name="org.quartz">
<level value="info" />
</logger>
</log4j:configuration>
Please help to sort out the issue. Thanks in advance.
WARN is a more severe log level than INFO, and so setting your log level to INFO will still get warnings. The best solution is to figure out why you're getting all those warnings; they're pointing to something Quartz thinks is a real problem (you apparently have a bunch of properties set in a configuration somewhere that the target job type doesn't understand). If you absolutely can't fix the real problem, then you can set the log level to ERROR.