Log4j logs configuration - java

I successfully configured log4j configuration by placing log4j.properties file inside in src/main/webapp/WEB-INF/classes folder. The logs for the application was working fine but my JUnit test cases logging was not working.
Searched over the internet and found out that I have to place my log4j.properties file inside src/test/resources for my JUnit test cases logging. Did so and logging for JUnit test cases also started working.
Deployed my application in my server after that and the log4j is now reading log4j.properties file from src/test/resources instead of one from WEB-INF/classes directory (the JUnit log4j.properties file creates a different log file for logging, and the logs are being written in that log file).
The application (maven project and not the war file) is being deployed in my weblogic server inside eclipse.
Please let me know how can i resolve this log4j configuration issue, so that my server uses the correct log4j configuration file.
log4j import:
import org.apache.log4j.Logger;
Logger instantiation:
private static Logger logger = Logger.getLogger(TestClass.class);

This is probably your IDE environments' issue. I think your IDE has test packages marked as source directories and when your IDE deploys web app, weblogic has your test packages on classpath.
Log4j seeks for configuration on classpath, so it likely sees test/log4j.properties first.
My 2 suggestions:
try to mark test/src as not exported to weblogic (or exluded from deploy - this is IDE specific)
force log4j configuration file by setting -Dlog4j.configuration=path/to/log4j.properties inside your weblogic startup configuration

Related

How to read log4j properties file out of classpath

I am facing a task to put log4j.properties configuration file to the JBoss server instead of putting this configuration to the project as stated in official JBoss documentation here: https://developer.jboss.org/thread/231514
If I got this right, log4j logger finds configuration within classpath automatically, but what is the way (if there is any) of loading this file from server directory (e.g. wildfly/modules/x/y/log4j.properties)?
You can use -Dlog4j.configuration=file:[path-to-your-external-file] while starting the server.
This is the answer I was looking for.

Writing same package to different log files in different applications

I am exploring options with log4j to write messages with the same package to different log files depending on the application. Both applications are WARs deployed to a web server. They both use some shared packages, say com.ps.foo. My use case is that I would like com.ps.foo messages to write to service.log when its executed withing the service WAR, and web.log when it executed within the web WAR.
We need the log4j configuration to be external to the WAR (in our local filesystem) so we can change logger properties without a redploy/restart of the web server, or else I would simply keep each log4j configuration packaged in each WAR.
I can specify the log4j configuration file as a -D option on the JVM, but then I am relegated to using the same log4j configuration. I cannot use the same log4j configuration file as I would not be able to qualify com.ps.foo with the application, my understanding is that log4j only cares about classes.
My current design is to add a listener to each application that initializes log4j with its own unique log4j configuration when the servlet context initializes. I would then have an application specific log4j configuration and specify com.ps.foo in each.
Any nudge in the right direction would be great.

Different Log4J configuration for Jenkins and production server

I am dumping log files (Log4J) under /logs/MyApp directory on my Linux server and its working fine. Application (EAR) is built using Jenkins and I am writing JUnit test cases for that.
The problem is Log4J file appender's location won't be pointing to Jenkins server filesystem and is relative to my Linux server.
Log4j.appender.App.File = /logs/MyApp/AppLogs.log
This is resulting in errors when test cases are executed from Jenkins as Log4J file location is not present.
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: /logs/MyApp/Applogs.log (No such file
or directory)
How can I configure Log4J to work with Jenkins without changing current configurations that are already setup for my production environment?
There are a few ways of fixing this:
Create the directory in your Jenkins master/slaves. Not sustainable if there is a new slave.
Change the log4j config so that you have different log4j configurations - one for your production, one for local testing and one for Jenkins. (This is what Play framework does and for different profiles you got different log files)
You can pass this is as a parameter for Jenkins job (or set it in env var ) and use it in Jenkins.
Don't use an absolute path in the log4j config if possible.

How to load a specific log4j.properties file if more than 1 exists in the classpath

Below is my scenario..
I am developing a new webapp which would be deployed on a weblogic server.
But there is already an existing webapp up and running on the same weblogic server.
Specific to this existing app, there is a log4j.properties file in an external location.
This location is added to the classpath in the weblogicStart.sh file.
My requirement is to add a new log4j.properties file and my new app should detect that than the old one.
So, are there are configurations that can be done so that my app picks up the new log4j file.
with PropertyConfigurator.configure().
see PropertyConfigurator
Add log4j.jar to you webapp WEB-INF/lib and log4j.properties to WEB-INF/. Make sure that Weblogic Server is configured for prefer-web-inf-classes mode, see http://docs.oracle.com/cd/E15051_01/wls/docs103/programming/classloading.html. This ensures that your webabb loads and initializes its own version of log4j.

tomcat, 2 webapps, 2 log4js, but both apps log to one file

To elaborate on that, I have a Tomcat server version 7.0.27 running Java 1.6.0_27.
I have two wars, each with their own log4j jar, also using slf4j with slf4j-log4j. Each war has it's own configuration file (log4j.xml).
war 1 should use file log-1.log and war 2 should use file log-2.log but both are logging into log-1.log.
I've checked there are no other log4j jars in the tomcat installation so I'm not sure where the problem is. I've also turned off shared class loading but that made no difference. My next step is to turn on verbose class loader logging and/or start debugging log4j but maybe someone here knows why this is and can save me some time. Thanks for any input on this.
Update:
Ok think I got this one. The log4j xml files are fine. After doing a verbose:class I can see that log4j.jar is only getting loaded once and from neither web application.
I'm working with Documentum. They have a runtime jar required to use their libraries that is an empty jar with a manifest file. The manifest points to a bunch of jars. In other words, they don't use maven... Anyway ... one of those jars happens to be logj4 found in the Documentum installation. So it seems both webapps are using that one. I think this is the problem. To be confirmed...
If you are placing Documentum's runtime jar on your top-level classpath, and that runtime jar is referencing log4j.jar, then it will only load once. You don't have to use that runtime jar, though, or you can use it in just your Documentum .war, if one is non-Documentum.
You didn't post your properties file but i can think of some reasons:
You don't have an appender that writes to the different files, i.e you need appender1 to write to log1.log and appender2 writing to log2.txt
You have the appenders set up right but both the applications are using the same logger, so they both write to the same file.
You have 2 loggers, each with its own appender, but in your code you are not initializing the correct logger:
//there is no logger called com.sample, so it defaults to root logger that has appender that writes to log1.txt
Logger logger = Logger.getLogger(com.sample.MyClass.class);
If you post your properties file and your logger init code it'll be easier to help you.

Categories