Configure log4j.properties in Intellij - groovy

I have problem configure log4j properties.
I used to use code to configure logger.
But, now I am getting the no appenders could be found WARN from groovyx.net.http.RESTClient which I can't change the code. The WARN is below:
log4j:WARN No appenders could be found for logger (groovyx.net.http.RESTClient).
log4j:WARN Please initialize the log4j system properly.
So I tried to use the log4j.properties file by default to setup the appenders. But, I don't know where I should put the log4j.properties file. I add the -Dlog4j.debug as JVM option and run the test. I am getting the below information:
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader#a6eb38a.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader#a6eb38a class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader#a6eb38a.
log4j: Trying to find [log4j.properties] using sun.misc.Launcher$AppClassLoader#a6eb38a class loader.
log4j: Trying to find [log4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [null].
I am using IntelliJ and running a Spock test.
Any answer will be appreciate. Thank you.

Your log4j config file should be placed in source root.
In IDEA it may be resource folder.
Look at: Where should I put the log4j.properties file?
For Groovy it all the same.

In intellij, you can assign a path to the configuration file as follows:
Open the edit configuration dialog box
Click on the logs tab
Click on the plus sign to add a path to your properties/xml file. You can add an alias for this configuration.
Apply.
To check if your log4j config file has been loaded, you can add the VM Option under run/edit configuration as -Dlog4j.debug. Using this argument log4j will log into the console steps relating to the process of loading the file.

Related

log4j 2.0.2 on slf4j 1.7.7 can't find log4j.xml on startup?

I'm trying to get slf4j 1.7.7 to use log4j 2.0.2 as its implementation. I think I have all the necessary JARs in my WAR and the log4j.xml just gets dropped into the classes directory, but when my webapp starts up, it can't locate the log4j.xml. When I crack open the WAR, I can see it in the WEB-INF/classes, so it should be on the classpath. What am I missing?
meta-inf/manifest.mf
index.html
...
WEB-INF/web.xml
WEB-INF/classes/log4j.xml
...
WEB-INF/lib/log4j-api-2.0.2.jar
...
WEB-INF/lib/slf4j-api-1.7.7.jar
...
WEB-INF/lib/log4j-core-2.0.2.jar
WEB-INF/lib/log4j-slf4j-impl-2.0.2.jar
...
Jetty has this in its stderrout.log file, recording its disappointment...
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Well that is subtle. log4j-core-2.0.2 isn't looking for log4j.xml, its looking for log4j2.xml. The FAQ page http://logging.apache.org/log4j/2.x/faq.html has, in bold, "By default, Log4j looks for a configuration file named log4j2.xml (not log4j.xml) in the classpath." Renaming my file to log4j2.xml gets it found. Now I've got to make it correct.
2014-10-24 10:34:53,542 ERROR Unknown object "property" of type org.apache.logging.log4j.core.config.Property is ignored.
2014-10-24 10:34:53,545 ERROR Unknown object "logger" of type org.apache.logging.log4j.core.config.LoggerConfig is ignored.
2014-10-24 10:34:53,546 ERROR Unknown object "root" of type org.apache.logging.log4j.core.config.LoggerConfig is ignored.

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.

log4j configuration error

When I am debugging my code then It goes to hibernateUtil which I make after it show a following error
og4j:WARN No appenders could be found for logger (com.gidiserver.util.HibernateUtil).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: The requested version 1.5.8 by your slf4j binding is not compatible with [1.6]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.format(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/String;
at org.slf4j.impl.Log4jLoggerAdapter.debug(Log4jLoggerAdapter.java:246)
at org.hibernate.type.BasicTypeRegistry.register(BasicTypeRegistry.java:147)
at org.hibernate.type.BasicTypeRegistry.<init>(BasicTypeRegistry.java:50)
at org.hibernate.type.TypeResolver.<init>(TypeResolver.java:59)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:250)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:302)
at com.gidiserver.serviceimpl.GidiBrainServiceImpl.displaySubjectDetails(GidiBrainServiceImpl.java:554)
at com.gidiserver.serviceimpl.GidiBrainServiceImpl.main(GidiBrainServiceImpl.java:2194)
Just adding sheltem's comment as an answer:
The link telling you what the problem is is actually right in the message: slf4j.org/codes.html#version_mismatch Take care of the slf4j version mismatch first, then worry about the log4j configuration (which isn't the cause of the error, as it's just a warning).
After that, if you still want to use log4j as the slf4j binding (instead of logback), add a either a log4j.properties in src/test/resources (this is all assuming you're using maven or gradle), or {module}-log4j.properties, and defining this properties file as your logging config in the surefire plugin, e.g., a system property in pom.xml (assuming maven):
<log4j.configuration>${project.artifactId}-log4j.properties</log4j.configuration>
Put log4j.properties in to src/main/resources.

Procrun and log4 configuration

I'm using procrun to start a Windows Service for my java process. I can get the service to start, but the log4j configuration that I set up doesn't seem to be working. This is what I see in the stderr file that procrun creates:
log4j:WARN No appenders could be found for logger (com.jar.aa.MainEntry).
log4j:WARN Please initialize the log4j system properly.
Here is the command that I specify to install the service:
%INSTALL_SERVICE_CMD% //IS//SERVICEAGENT --DisplayName="DispName" --Install=%SERVICE_EXE_HOME%\prunsrv.exe --LogPath=%INSTALLER_HOME% --LogLevel=Info --StdOutput=auto --StdError=auto --StartMode=Java --StopMode=Java --Jvm=auto --Startup=auto --JvmMx=512 ++JvmOptions=-XX:MaxPermSize=128m --StartPath=%START_CLASS_PATH%\ --Classpath=%CLASSPATH%;MyJar.jar --StartClass=com.jar.aa.MainEntry --StopClass=com.jar.aa.ExitEntry ++StopParams=--stop ++JvmOptions=-Dfile.encoding=UTF8 ++JvmOptions=-Dlog4j.configuration=log4j.properties ++JvmOptions=-Dorg.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
I have my log4j properties placed in the same directory as this install script.
Can anyone tell me what I might be missing? Any help would be greatly appreciated.
Thanks,
K
I'm not sure if my memories are correct, but i think i had the same problem and solved it by including the log4j configuration file in my jar file. In your case MyJar.jar.
If it still gives troubles try placing your jar at the beginning of your classpath definition. HTH

Cannot set log4j logger level with Tomcat 6

I am using log4j-1.2.16.jar with Tomcat 6. The log4j config is handled with a log4j.xml file in the WEB-INF\classes directory. I turned log4j debug on. When it process a level element for a logger I get output like:
log4j: Level value for root is [WARNING].
log4j: root level set to DEBUG
Therefore all debug messages are logged when not wanted.
Any ideas why this could be happening?
Level should be WARN not WARNING.
(It would be nice if log4j.debug gave an invalid level message.)

Resources