Writing same package to different log files in different applications - java

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.

Related

JBoss does not launch logging in the deployed applications

According to jboss doc, the settings from server file logging.properties file are
used when the server boots up until the logging subsystem kicks in.
In the same document, it is said that if we want to set logging rules in the deployed application, we should put one of
logging.properties
jboss-logging.properties
log4j.properties
log4j.xml
jboss-log4j.xml
files into META-INF or WEB-INF/classes. META-INF - for EAR, either of these places - for WAR and JAR. (I have JAR). I had put log4j.properties into resources/META-INF, webapp/META-INF (this one appears later in jar on the highest layer), and in webapp/WEB-INF/classes. All of them I made with different patterns so that I would recognize them when they'll appear.
Also, I added all the other 4 files into all these three places. With different patterns, again. All are present in the jar/META-INF and other appropriate places. But none of them is launched when deployed in the server. Even more, when I edit JBoss's logging.properties, the logging changes according to these changes, which means that the server pays no attention at all to any of those logging configurations in the application, even after it "kicks in".
All logging that I see after app deployment, is some server info logging about memory use. Not a line from the app. Even in the case when the browser gets something as error 500, the log is empty.
JBoss AS 6.1

How to read Logback configuration file from path outside the war file?

I have a requirement where I want to place all logback configuration outside the war file.
Presently I have placed my configuration file (logback.xml) on the classpath.
I am using the JBOSS EAP web application server, kindly suggest how to achieve the same.
Go into the jboss startup/run script and add this option:
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
see more at http://logback.qos.ch/manual/configuration.html
You could also use -Dlogging.config=/path/to/logback.xml
This is really handy with java based microservices to provide logging XML externally.

logback xml configuration for web application

I am using logback SLF4J for logging the debug/error statements. Could you please let me know how to use single logback.xml configuration file for multiple environments (dev/qa/prod)? Right now, i am editing xml file for each environment to specify dbname...I appreciate your help.
Couple of options (most of them documented here)
Use properties in the log configuration which are set externally (either java properties or OS environment variables)
Use JNDI settings (creating db datasources is pretty common)
Generate a logback.xml file as part of the deployment process
JMX configurator which allows you to reload the configuration from a named file
Package a WAR file for each environment (don't really recommend this included for completeness)

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.

logback configuration files per jar

I would like to deliver jar files with its own logback logging configuration.
The common way to configure logback is with the default file logback.xml that logback library reads from the classpath's root (works for application servers or not).
You could include another files from the main logback config file (didn't try it), but I don't know which jars will be in the classpath and which ones require the log configuration.
Plus, the jars could be used in a command line application or application servers (shared library or not).
I thought that maybe I could get the filepath to the jar and check if there is a config file there, and try to read the configuration programmatically and load it with JoranConfigurator.
// This is the way I find to get the path to the jar
String path = ClassThatWantsALogger.class.getProtectionDomain().getCodeSource()
.getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");// the path to the jar
But this approach could fail because depends on the security restrictions, maybe fails in Linux or in application servers. It's a hard approach for a problem that probably has a better solution.
Do you imagine a way to manage that jars could have its own logback config file that works for any environment?
I will cite myself to answer your question: Logging configuration is the concern (separation of concerns) of the client application. It's his decision to make where, how and what will be logged. You shall not impose anything on it by our library.

Categories