Seperate logs for different jars in war file - java

I have a war file which is deployed on jboss server. This war contains some jar files.
What i need is to create seperate logs for different jars.
I am using log4j for logging purpose and also intergrated log4j properties file in each jar.
My log4j for every jar have different paths.
Still the logs are generated in server.log of jboss.
I need help to generate seperate logs for every jar.

If you want to make different log file for different jar, you will have to configure it in your log4j.properties file related to every jar(I assume that every jar have different package naming convention).
Here is an example like if you want to log message in different file whose package name starts with org.jar1 and org.jar2.
log4j.debug=true
log4j.logger.org.jar1=DEBUG,jar1logger
log4j.logger.org.jar2=DEBUG,jar2logger
log4j.additivity.org.jar1=false
log4j.additivity.org.jar2=false
log4j.appender.jar1logger=org.apache.log4j.RollingFileAppender
log4j.appender.jar1logger.layout=org.apache.log4j.PatternLayout
log4j.appender.jar1logger.layout.ConversionPattern=%d [%t]<%-5p> %c -> %m%n
log4j.appender.jar1logger.File=/var/log/mylog/jar1.log
log4j.appender.jar1logger.Append=true
log4j.appender.jar1logger.MaxFileSize=1024KB
log4j.appender.jar1logger.MaxBackupIndex=5
log4j.appender.jar2logger=org.apache.log4j.RollingFileAppender
log4j.appender.jar2logger.layout=org.apache.log4j.PatternLayout
log4j.appender.jar2logger.layout.ConversionPattern=%d [%t]<%-5p> %c -> %m%n
log4j.appender.jar2logger.File=/var/log/mylog/jar2.log
log4j.appender.jar2logger.Append=true
log4j.appender.jar2logger.MaxFileSize=1024KB
log4j.appender.jar2logger.MaxBackupIndex=5
Based on your requirement whether you want to create log on daily basis or on size basis you have to change your configuration.
Keep in mind if any jar whose package name in any jars start with org.jar1 then all logs will be written into jar1.log file.

Related

log4j2 logging happening in the wrong path

I have an ear deployed in weblogic with the log4j2.xml logPath set as
<Property name="logPath">some_path_1<Property>
and a logger defined as
<Logger name="a.b.c.d" level="INFO" />
Inside this ear/lib there is a jar containing a log4j2.xml and the logPath property is defined as
<Property name="logPath">some_path_2<Property>
and a logger with a very similar packaging structure
<Logger name="a.b.c" level="INFO" />
Both log4j2.xml's are configured to have a different log file name as well.
But the logs for both are coming in the path some_path_2 and with the file name defined in that jar
How to ensure both logging outputs happens separately as defined?
My ear structure is like
ear
|--lib
|--|--abc.jar
|--|--|--log4j2.xml //the one thats getting loaded
|--xyz.war
|--|--WEB-INF
|--|--|--classes
|--|--|--|--log4j2.xml // the one I want
Edit1: I added the following in the containing war's web.xml but it did not help
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j2.xml</param-value>
</context-param>
Edit2: I also tried this and found that this is working intermittently
ear
|--lib
|--|--abc.jar
|--|--|--log4j2.xml //the one thats getting loaded
|--|--xyz.jar
|--|--|--log4j2.xml //the one I want.. works but not always.Does classloader loads the jars alphabetically?
|--xyz.war
|--|--WEB-INF
|--|--|--classes
|--|--|--|--log4j2.xml // the one I want
Log4j is initialized only once by using the configuration file which is first found by the log4j bootstrapper. All other (possible present) configuration files will not be taken into account. Learn more on the precedences of log4j auto configuration in the appropriate tutorials.
As you probally already know your problem is that only one configuration-file gets loaded and which one depends on the classloader. So you should not rely on that.
There are only two possible soulutions for your problem, but today I do not have the time to work it out for you - sorry. You have to remove or rename the config-file in abc.jar
Remove log4j2.xml from abc.jar
Do it with your build-script in ANT/MAVEN/GRADLE or whatever. Copy-paste the interessting parts of the config-file in your config-file.
Rename log4j2.xml inside abc.jar
Do it with your build-script in ANT/MAVEN/GRADLE or whatever. Now you could include the whole config-file in your log4j2.xml. This is called CompositeConfiguration.
I hope this little advice will help you.
Good luck!
Can you remove the XMLs from the jar/war and use only one xml file at the ear level which has details of both the logging paths/properties. You can try to reconfigure log4j2 in code with a specific configuration file.
You can also dynamically write to separate log files, but I personally haven't tried this so I wouldn't be able to guide more on this.
Are you sure you want to use "log4.xml" file to be use by log4j2. I don't think, log4j2 automatic configuration mechanism try to find out configuration file with name "logj.xml".
From official documentation,
Log4j has the ability to automatically configure itself during
initialization. When Log4j starts it will locate all the
ConfigurationFactory plugins and arrange them in weighted order from
highest to lowest. As delivered, Log4j contains four
ConfigurationFactory implementations: one for JSON, one for YAML, one
for properties, and one for XML.
Log4j will inspect the "log4j.configurationFile" system property and,
if set, will attempt to load the configuration using the
ConfigurationFactory that matches the file extension.
If no system property is set the properties ConfigurationFactory will look for log4j2.properties or log4j2.json or log4j2.xml files in classpath.
Possibly you should try to rename the file "xyz.war//WEB-INF/classes/log4j.xml" to "xyz.war//WEB-INF/classes/log4j2.xml". Obviously content of this file must confirm to log4j configuration xsd.

Cannot find log file in my project logs folder in spring mvc project

I use log4j in my spring mvc project and I create log file uing log4j.properties in the following.
# Root logger option
log4j.rootLogger= INFO, info, file, stdout, error
# Direct log messages to a log file [INFO]
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.File=.logs\\mylog.log
log4j.appender.info.Threshold=INFO
log4j.appender.info.MaxFileSize=1Kb
log4j.appender.info.MaxBackupIndex=10
log4j.appender.info.DatePattern='.'yyyy-MM-dd
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I want it to be stored in myapp/logs folder in my web application . what part i should for the file.
here is the directory
Right now it will be saved in the current working directory of your IDE (Eclipse or STS, I guess). Its not a good idea to put the file in the project structure, because that structure wont exist, once you deployed the app anywhere.
In Spring you can define a property in youe web.xml, to make the location variable:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>mywebapp.root</param-value>
</context-param>
Then add a variable to your config:
log4j.appender.info.File=${mywebapp.root}/logs/mylog.log
If you have to store it in the application, you can also define that programmatically, by makeing use of "ServletContext.getRealPath(".") to find out where your app is deployed, and then set the path to the file.
In the normal scenario web-app checks for your log file at the root of your class path. To load a file from custom location , have a look at below posts
Change location of log4j.properties
How to use custom file instead of log4j.properties
EDIT :
From op's comment to generate your log file in the projects path , where you have placed your logs directory . you can simply specify it as ,
log4j.appender.file.File= logs/mylog.log

No log statement is printing using websphere 8.0

I have one application running in WAS8.
we have a jar - commons-logging-1.1.1.jar in WEB-INF/lib
we have one properties file - commons-logging.properties
the content of the file is
priority=1
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
we have org.apache.commons.logging.LogFactory file in WebContent/META-INF/services
the content of the file is
org.apache.commons.logging.impl.Log4jFactory
The log files are created but nothing is written in it. It is not showing any error in the log files in log of appserver.
Please let me know if I am missing something.
Please note: if I keep commons-logging.properties in /opt/IBM/WebSphere/80/AppServer/profiles/AppSrv01/properties, then it works perfectly fine. It is writing in the log files. But as I heard it is not standard practice so I can't keep the file in that place. I have to find some alternative way.
Please help me.
If it is not a requirement to have separate log files for your web application, you can simply remove the commons-log jar and properties from your module (war) and your log statements will write to SystemOut.log according to the log level settings in the WebSphere console (which can be changed at runtime, by the way).
If you must separate application logging, you can refer to this infocenter article that lays out the combination of commons-log jar location, commons-log properties values, and application classloader settings (Parent-First, Parent-Last, commons-log.jar bundled or not, etc) to achieve the desired results.

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.

Two projects | Two log files

Project1 is using classes from project2 and project3.
Project 2 and project 3 has seperate log4j.properties file and logging classes (Pro2.java & Pro3.java with info,debug methods) in their src folder.
Project1 has a main method that calls
Pro2.logInfo("This is a log for project2");
Pro3.logInfo("This is a log for project3");
I want these logs to be logged in two seperate log files as I set in their log4j.properties file.
In general, you can only have one active log4j.properties file (there are exceptions in J2EE environments).
what you want to do, is configure the logging for both projects in a single properties file. You'd need to define two appenders, and specify the categories for each project to use their own appender.
Log4j will automatically look for and use config files it finds on the classpath. It looks for files called log4j.properties and log4j.properties and possibly others.
Alternatively you can programatically load config using;
String filename = "/path/to/config/Project1log4j.properties";
PropertyConfigurator.configure(filename);

Categories