Loading project-specific log information into log files - log4j

I am using Eclipse and I am creating a web project. I am also using SLF4J for storing logs in a log file.
I am putting the log4j.properties file into the WEB-INF/classes folder .
When I access the login page, all of the associated logs and my debug statement log are being loaded into the log file, but I want a specific log (i.e., project specific log) to be loaded into the log file instead of other logs.
On the log file I can see:
10/20/2010 10:16:24 - DEBUG - org.apache.commons.digester.Digester.sax
.
10/20/2010 10:16:24 - DEBUG - org.apache.commons.digester.Digester - [ObjectCreateRule]{resource-config/resource}New org.ajax4jsf.javascript.AjaxScript
.
10/20/2010 10:16:24 - DEBUG - org.apache.commons.digester.Digester - [ObjectCreateRule]{resource-config/resource}New
.
10/20/2010 10:16:24 - DEBUG - org.apache.commons.digester.Digester.sax
.
10/20/2010 10:16:24 - DEBUG - org.apache.commons.digester.Digester - [ObjectCreateRule]{resource-config/resource}New org.ajax4jsf.javascript.AjaxScript
.
10/20/2010 10:16:24 - DEBUG - org.apache.commons.digester.Digester - [ObjectCreateRule]{resource-config/resource}New
.
Can anyone help me to find out how to store specific project related logs into log file?

Have something like the following in your log4j.properties file:
log4j.logger.com.foo.logingubbins=DEBUG, loginfilelogger
log4j.appender.app=org.apache.log4j.RollingFileAppender
log4j.appender.loginfilelogger.File=c:/logs/login.log
log4j.appender.loginfilelogger.layout=org.apache.log4j.PatternLayout
log4j.appender.loginfilelogger.layout.ConversionPattern=%d [%t] %-5p [%c (%F:%L)] %n \t %m %n
That first line tells the logger to use a new logger for com.foo.logingubbins, which you direct to a separate file. You might want to add an additivity statement in there as well if you want it entirely separated.
The log4j manual can give you some useful examples, though I have to admit that the information on additivity is a little hazy.

Thanks for reply. I added the folowing steps into log4j.properties file
log4j.logger.org.apache.commons.digester=ERROR
log4j.category.se.bilprovningen.prippe=DEBUG,R
Now i am able to store project specific log into log file.
Thanks
Arvind

Related

How to configure to print out Ignite logs

I want to see the INFO level logs that Ignite prints during its running(so that it will help me on what Ignite is doing). I am using the following log4j.properties to make Ignite print INFO level logs,but the logs are not printed out.
Are there special configuration to make Ignite logs printed out?
Thanks.
log4j.rootCategory=INFO, stdout , R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p %d - [TS] %c %M(%L) - %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=c:/ioc.ignite.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %d - [TS] %c %M(%L) - %m%n
log4j.logger.org.apache.ignite=INFO
log4j.logger.org.springframework=WARN
Please follow below steps:
e.g. if you are using log4j for logging
configure ignite/config/ignite-log4j.xml in gridLogger of IgniteConfiguration
e.g.
.........
uncomment "CONSOLE" appender in ignite/config/ignite-log4j.xml
copy ignite/libs/optional/ignite-log4j/log4j.jar and ignite/libs/optional/ignite-log4j/ignite-log4j.jar in ignite/libs/ folder
you can also set IGNITE_LOG_HOME environment variable to redirect the logs to any file
You should use <CLASSPATH>/config/ignite-log4j.xml file instead of log4j.properties.

Location of Pentaho 5 log config

I'm trying to connect Pentaho 5.4 (kitchen job runs to be precise) to our Logstash Server. To do that, I need to edit the Log4j config file so it will use the new appender.
I searched every jar file and every folder, but to no avail. Pentaho 6 comes with several log4j.xml files, but none of them seems to be used for the output, either. Also, the -Dlog4j.debug flag does not give me useful any info. Am I barking at the wrong tree?
A typical log looks like this:
Kitchen.bat -Dlog4j.debug /file "samples\jobs\changelog\Process changelog.kjb"
..
2016/04/14 15:24:17 - Kitchen - Start of run.
2016/04/14 15:24:18 - Process changelog - Start of job execution
..
2016/04/14 15:24:21 - General - Change log processing 2 - Finished processing (I=0, ... E=0)
Any help is greatly appreciated!

How to get rid of "Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties" message?

I am trying to suppress the message
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
when i run my Spark app. I've redirected the INFO messages successfully, however this message keeps on showing up. Any ideas would be greatly appreciated.
Even simpler you just cd SPARK_HOME/conf then mv log4j.properties.template log4j.properties then open log4j.properties and change all INFO to ERROR. Here SPARK_HOME is the root directory of your spark installation.
Some may be using hdfs as their Spark storage backend and will find the logging messages are actually generated by hdfs. To alter this, go to the HADOOP_HOME/etc/hadoop/log4j.properties file. Simply change hadoop.root.logger=INFO,console to hadoop.root.logger=ERROR,console. Once again HADOOP_HOME is the root of your hadoop installation for me this was /usr/local/hadoop.
Okay, So I've figured out a way to do this. So basically, I had my own log4j.xml initially, that was being used, and hence we were seeing this property. Once I had my own "log4j.properties" file, this message went away.
If you put a log4j.properties file under both the main/resources and the test/resources this also occurs. In this case, deleting the file from the test/resources and using only the file from the main/resources fixes the issue.
None of the answers above did work for me using SBT. Turns out you need to explicitly define an appender in your log4j.properties, such as:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.rootLogger=WARN, stdout
log4j.logger.org.apache.spark=WARN, stdout
log4j.logger.com.yourcompany=INFO, stdout
Put this in your resources directory and Bob's your uncle!

log4j not logging today, worked fine two days ago

This has been asked multiple times but I have not been able to figure out what is wrong from the past questions that have been answered.
my log4j logging was working fine on Dec 31. Today, Jan 2, my first day back to work nothing is being logged. The data that should have been logged is showing up on the console but not in the log file. My log4j.properties is below. Nothing in it was changed between the last time logging was working and today.
My application is servlet based and I see no errors in either catalina or localhost logs.
I have no idea what could have happened. Does anyone have any ideas?
I just realized that I did an SVN update on the project before starting today. I might have gotten newer version of some jar file. Can a jar 'collision' cause log4j to stop logging?
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.logger.AppLogFile=DEBUG,AppLogFile
log4j.appender.AppLogFile.File=../logs/dbconnect.log
log4j.appender.AppLogFile=org.apache.log4j.RollingFileAppender
log4j.appender.AppLogFile.layout=org.apache.log4j.PatternLayout
log4j.appender.AppLogFile.layout.ConversionPattern=DBCONNECT %-22d{dd/MMM/yyyy HH:mm:ss} - %m%n
# manage logging for packages
log4j.category.org.springframework=ERROR
log4j.category.org.hibernate=ERROR
log4j.category.com.mchange.v2=ERROR
I'm answering my own question here. After four hours of searching I found the answer.
The problem was a third party jar that came down from SubVersion when I did an update. This jar was added yesterday by a developer working on a different part of the application.
Log4j looks for 'log4j.xml' before it looks for 'log4j.properties'. The third party jar contained a log4j.xml so that one was loaded and my log4j.properties was ignored.
This was pretty easy to find once I added the log4j debug command.
So, here is how to do it if you find yourself in this situation.
Stop Tomcat
In %CATALINA_HOME%/bin create a file called setenv.bat. Add these lines to the file:
REM Use this file to set either or both JAVA_OPTS, CATALINA_OPTS
REM Don't change startup.bat
set JAVA_OPTS=-Dlog4j.debug
Save and Restart Tomcat
Log4j will now log useful information, including what config file it is using, to the console.
When Tomcat starts the app Log4j logs the search for a config file. Here are two excerpts; the first showing what was happening to me the second showing what is normal behavior for my situation.
Case 1: Log4j is looking in the right place, /WEB-APPS/emscribe/WEB-INF/classes, but for log4j.xml not log4j.properties
log4j: Trying to find [log4j.xml] using context classloader WebappClassLoader
context: /emscribe
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader: org.apache.catalina.loader.StandardClassLoader#1f7fd168
log4j: Using URL [jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/apache-tomcat-7.0.26/webapps/emscribe/WEB-INF/lib/dynamicreports-examples-3.0.0.jar!/log4j.xml] for automatic log4j configuration.
So, here Log4j found log4j.xml in the recently added third party jar and looked no further.
Case 2: With the third party jar removed log4j found the correct config file:
log4j: Trying to find [log4j.xml] using context classloader WebappClassLoader
context: /emscribe
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:org.apache.catalina.loader.StandardClassLoader#8b10bd5
log4j: Trying to find [log4j.xml] using WebappClassLoader
context: /emscribe
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:org.apache.catalina.loader.StandardClassLoader#8b10bd5class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader WebappClassLoader
context: /emscribe
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader#8b10bd5
.
log4j: Using URL [file:/C:/Program%20Files/Apache%20Software%20Foundation/apache-tomcat-7.0.26/webapps/emscribe/WEB-INF/classes/log4j.properties] for automaticlog4j configuration.
log4j: Reading configuration from URL file:/C:/Program%20Files/Apache%20Software%20Foundation/apache-tomcat-7.0.26/webapps/emscribe/WEB-INF/classes/log4j.properties
I eventually replaced log4j.properties with an equivalent log4j.xml, replaced the third-party jar and my log4j.xml was found before the one in the jar.
I had a different issue. I imported a project where log4j.properties was there with everything configured. There was a log file name too in the properties file, but it did not write a log file during running the project in eclipse.
Putting -Dlog4j.debug in the VM arguments in run configuration helped me to analyze, it printed all the configuration log4j doing during initialization of itself, found what I had to change in my properties file.

How to configure log4j and Selenium Grid?

When I launch a Grid hub using Ant, the logging is all by default on the console. I would like to know if there is a way wherein I can alter the build.xml file of Selenium Grid and include a log4j logger into it. I understand that I can specify a log file to Ant itself using the -logfile option. But this would cause the log file to be overwritten everytime the grid is launched. I want to ensure that the log files are automatically renamed after a threshold is reached for better maintenance. Any help and if possible some examples on how to do it would be greatly appreciated (I am new to using log4j which is why I am asking for some sample for this specific need).
You need to specify the location of the Log4j archive as well as its configuration file when starting up Selenium. In the following example, I've updated the classpath to load the log4j.hub.properties configuration file located in the /etc/selenium directory and the log4j.jar located in the /usr/lib/selenium directory. I've also setup some additional log files:
java -classpath /etc/selenium:/usr/lib/selenium/log4j.jar:/usr/lib/selenium/selenium-server-standalone.jar -Dlog4j.configuration=log4j.hub.properties org.openqa.grid.selenium.GridLauncher -role hub -log /var/log/selenium/hub.debug.log > /var/log/selenium/hub.output.log 2> /var/log/selenium/hub.error.log &
Then you can have something like the following to achieve what you are looking for:
log4j.rootLogger=ALL, file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=/var/log/selenium/hub.log
log4j.appender.file.Append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} <%p> <%t> %m%n
You might want to look at this page for more information about how Selenium logging is working.

Resources