Where should I put log4j.jar? - java

I create a Tomcat webapp project. If I put the log4j.xx.jar & log4j.properties in the Tomcat lib, I get the log file. If I put the log4j.properties in the WEB-INF/classes, I doesn't get the log file. I try again and again. When I put the log4j.xx.jar in the WEB-INF/lib and put the log4j.properties in the WEB-INF/classes, I get it again. Why? What's the logic?
log4j.rootLogger=INFO,A1, A2
#org.apache.log4j.ConsoleAppender
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.File=D:\\Tomcat2\\logs\\monitor.log
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.A2=org.apache.log4j.ConsoleAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

Both log4j.xx.jar and log4j.properties files have to be loaded by the same classloader. Tomcat lib files are loaded by a different classloader than WEB-INF/lib files. So you have to put both files under Tomcat lib or both files under WEB-INF/lib to make it work.

Put the log4j jar file in WEB-INF/lib
Put log4j.properties anywhere you like eg tomcat/conf . Make sure it is in classpath.
And then read the log4j.properties using the classloader.
YourClass.class.getClassLoader().getResourceAsStream("relative path");
I don't like the idea of putting things to tomcat/lib folders.
Firstly because different apps on tomcat may use different version of log4j.jar. And putting in tomcat/lib will cause a version conflit for some of the applications.
Secondly its not right to put a configuration file in the lib directory. Its right place is the conf folder.

Related

Log4j doesn't write logs to file

I'm using slf4j over log4j.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
The log4j.properties is in WEB-INF folder and has the following content:
log4j.rootLogger=DEBUG, stdout, file
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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\logs\\log4j.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
On another enviroment with same version of Tomcat the same respository works just fine, logging to the file...
You said that this same configuration file was working fine on a different environment. Can you give a little detail on the differences between the environments. e.g. are they both on Windows? The fact that is was working fine on another environment suggests that your configuration is fine, but there is some issue with your environment. Here are a few things to check:
Does the tomcat process has permissions to write to that file - i.e. are administrator rights required to write to D:\logs\log4j.log?
Make sure that there aren't any temporary files lying around tomcat's directories which may be preventing your changes from taking effect. To be sure, stop tomcat, delete the expanded war from %CATALINA_HOME%\webapps, delete the contents of %CATALINA_HOME%\temp and %CATALINA_HOME%\work
Check that you don't have any other log4j config files on your classpath, as these could be overriding your log4j file and preventing it from taking effect. To double check this, you could try temporarily removing your log4j.properties file to see if you get messages saying that the logging system is not initialised properly.
Perhaps your log4j.properties isn't being read at all. If it's not found, then log4j will use a default configuration and you might not even know about it.
Try to pass this line
-Dlog4j.configuration=file:///D:/yourPathToFile/log4j.properties
as a VM Argument in your run configuration and see if it helps.
For a Maven Based Project keep your log4j.properties in src/main/resources
I had the same problem, putting the log4j.log in source work. I thought it was getting compiled in the EAR file, but was not. Assuming it was using default.
Instead of:
log4j.appender.mainAppender.File=mainloggs.log
log4j.appender.mainAppender.Append=true
use:
log4j.appender.file.File=mainloggs.log
log4j.appender.file.Append=true
Your log4j.properties file should be in WEB-INF/classes, not WEB-INF. WEB-INF is not in the web application classpath, but WEB-INF/classes is.
Try with
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.Append=true
If it not working. Please put your web.xml
The issue might be coming when you won't add the log4j.properties file in your classpath. To fix the same follow the steps below:-
Create a folder resources in project and put your log4j.properties in it.
Go to project properties --> Java Build Path -->Click on Add Folder -->Check the resources folder -->Click ok and its done. It should solve your problem.
I had the same problem. It used to work a while ago but not working now.
I guess this is not a problem with log4j2 or slf4j(as was in my case) but instead with the relative path to the config file.
If you have updated java or update IDE or changed some system variable or relative path of the folders then your problem is similar to mine.
Possible fixes/workarounds -
Try including the log4j2 configuration file in the artifact while
building it.
Alternatively you can get config from propertied file and hardcode
the logging in code itself - This was what worked for me.
Try using the scheme when mentioning file paths on windows. Does not need to mention it this way on other platforms but on windows you need to. I have observed issues regarding file paths without scheme.
log4j.appender.file.File=file://d:/logs/log4j.log
So I had a similar issue with the .log file not being written to when writing a web app on tomcat. In the end I changed the file path to be relative with something like .\log.log. Then I noticed that when I ran the program using tomcat 7 I found the log file in the \bin directory instead of the \log directory inside my tomcat installation.
log4j.appender.file.File=${user.home}\\Logs\\mylogfile.log
I had the same issue. Turns out, I have another web application inside Tomcat that contain another log4j.properties file. After removing this application, things are working as expected
I had this problem when migrating log4j1 to log4j2 through the Log4j 1.x bridge (log4j-1.2-api)
My logj1 configuration was correct and the log4j2 and bridge JARs were correctly placed but I forgot this step. So adding this JVM parameter worked:
-Dlog4j1.compatibility=true

Warning in Initializing log4j

I am getting the following error while running a struts application.
13/04/19 12:42:21 log4j:WARN No appenders could be found for logger (org.apache.struts.util.PropertyMessageResources).
13/04/19 12:42:21 log4j:WARN Please initialize the log4j system properly.
13/04/19 12:42:21 log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
My log4j.properties file is:
log4j.threshold=ALL
log4j.rootLogger=ALL,DebugAppender,InfoAppender,RECEIPTAppender
#log4j.rootLogger=DEBUG,DebugAppender
log4j.category.DebugAppender.access=DEBUG
log4j.appender.DebugAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=C:/APWD/ServerLogs/debug/assampwd.log
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d |[%t]| %-5p |%c |- |%m|%n
#log4j.rootLogger=INFO,InfoAppender
log4j.category.InfoAppender.access=INFO
log4j.appender.InfoAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.InfoAppender.Threshold=INFO
log4j.appender.InfoAppender.File=C:/APWD/ServerLogs/debug/assampwd.log
log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.InfoAppender.layout.ConversionPattern=%d |[%t]| %-5p |%c |- |%m|%n
#log4j.logger.com.ctmis.hibernate=ERROR, RECEIPTAppender
log4j.category.RECEIPTAppender.access=ERROR
log4j.appender.RECEIPTAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RECEIPTAppender.Threshold=ERROR
log4j.appender.RECEIPTAppender.File=C:/APWD/ServerLogs/debug/assampwd.log
log4j.appender.RECEIPTAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RECEIPTAppender.layout.ConversionPattern=%d |[%t]| %-5p |%c |- |%m|%n
My log4j.properties file is in WEB-INF folder. I have found similar topics in stackoverflow, but didnot work out for me, so I post it. Would really appreciate if someone can help me out, Its a warning message, but causing some module not working in the application.
Thanks
Your log4j.properties file needs to be in the root of your classpath. Copy the file into the WEB-INF/classes folder inside your war file.
If you are using Maven, this should involve moving your file into the src/main/resources folder of your project before running the packaging command.
The log4j configuration file needs to be in the root of the classpath. In netbeans, when you create a web application project, the path is src\java. You can see the source packages folders with a right click on the project -> Properties -> soruces. You can place the file in src\java folder or make a new folder and add it from the project properties menu.

Log is not working inside the java file. But works inside the servlet

I have issue in executing the logs. I have two projects: one is a servlet and the other one is a simple java file. private static Logger logger=Logger.getLogger(myServiceServlet.class); This was working fine. Its writing log wen i start running the Tomcat server. But the same thing is not achieve in my simple java file, it converted into WS and try to deploy in axis/Tomcat server
Servlet is directly running in Tomcat server.
But java file is converted into Webservice.aar inside the Axis2--->Tomcat server.
Log Properites
# Log levels
log4j.rootLogger=INFO,CONSOLE,R
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.R.File=C:/res backup/apache-tomcat-6.0.35/webapps/mylog/logs/servicelog.log
log4j.appender.R.MaxFileSize=2048KB
# Number of backup files
log4j.appender.R.MaxBackupIndex=50
# Layout for Rolling File Appender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n`
The .aar files are not running in the same context as the JARs in WEB-INF/lib.
They share some kind of context as you can load references to the WEB-INF/lib classes, however its not 100% consistent at this point.
I would recommend you build some kind of wrapper in your .aar files that do nothing else then forwarding the calls into methods of JAR files that are situated in WEB-INF/lib.
You just need to make sure you load the reference from the .aar to the methods in WEB-INF/lib context correctly.
To throw errors in the .aar classes I would recommend you catch them and then throw an "AxisFault".
throw new AxisFault(err.getMessage());
http://axis.apache.org/axis2/java/core/api/org/apache/axis2/AxisFault.html
That way we have have been able to solve almost all our issue with the context of .aar files.
Sebastian

Need to create separate log file for jar file?

I am creating a jar file that i need to provide to client, but my client is asking for separate logger for the jar file as this is doing some integration work.
anyone can suggest how can i create logger for only one jar file, can i put log4j.properties in same jar file.
I am using web-logic server. we will not deploy this jar file instead we will just keep it in domain lib folder.
Thanks
If the properties file is in the jar, then you could do something like this:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/log4j.properties"));
PropertyConfigurator.configure(props);
The above assumes that the log4j.properties is in the root folder of the jar file.
If this does not work for your needs in this case then you can always use:
-Dlog4j.configuration=log4j_for_some_jar.properties
If the other application is using Log4j as well. The easier method would be to just config your log4j file to send anything from your classes in your jar to a new log file like so:
log4j.rootLogger=ERROR, logfile
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.datePattern='-'dd'.log'
log4j.appender.logfile.File=log/radius-prod.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n
log4j.logger.foo.bar.Baz=DEBUG, myappender
log4j.additivity.foo.bar.Baz=false
log4j.appender.myappender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myappender.datePattern='-'dd'.log'
log4j.appender.myappender.File=log/access-ext-dmz-prod.log
log4j.appender.myappender.layout=org.apache.log4j.PatternLayout
log4j.appender.myappender.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n

NTEventLogAppender (Library is already loaded in another ClassLoader)

I have a log4j.properties file like this on my src package:
log4j.rootLogger=DEBUG, CA, EVA
#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#Event Viewer Appender
log4j.appender.EVA=org.apache.log4j.nt.NTEventLogAppender
log4j.appender.EVA.layout=org.apache.log4j.PatternLayout
log4j.appender.EVA.source=MySource
log4j.appender.EVA.layout.ConversionPattern=[%c][%l][%p][%thread]: %m%n
I create the logger on a interface (LogInterface.java) like this:
package components;
import org.apache.log4j.Logger;
public interface LogInterface {
static final Logger logger = Logger.getLogger("MyLogger");
}
I place the NTEventLogAppender.dll under:
C:\Program Files (x86)\IBM\WID7_WTE\runtimes\bi_v7\java\jre\bin
Sometimes I am getting the following error when there is an exception to log:
NTEventLogAppender (Library is already loaded in another ClassLoader)
How can I solve this issue?
Thanks
The solution for this problem was to place the log4j JAR file under:
Application_Server_Install_Path\lib
On the project I added the log4j JAR file to the project's build path as a variable (add variable option)
This solves the JAR reference on the IDE the only thing left to do is to add the previous log4j JAR file reference on the Websphere Application Server's runtime.
To do this I accessed the WAS administration console and added the previous log4j JAR file path under:
Environment -> Shared Libraries
Despite no one seems to have this same problem, here's the solution for future similar problems
Thanks

Categories