How do I configure log4j per deployed application in Glassfish 3? - java

I'm trying to use log4j to handle the logs for a web-service which is running under Glassfish 3. Most of the guides I've seen using log4j with Glassfish want me to mess around with Glassfish global settings, which I want to avoid as there will be more than one application deployed per instance of Glassfish.
Is there any way for me to have Glassfish execute a piece of code when my web service is deployed which will allow me to call DOMConfigurator and set up log4j using my XML file?
Thanks for any help!
EDIT: The answer is to place the log4j.xml file in WEB-INF/classes. In our case, it looks as if log4j remains un-configured, but logging does still actually work as expected.

Yes. All you need to do is deploy the log4j configuration with the component you're deploying; log4j will use the locally-scoped configuration as long as it's not being referenced in a parent classloader.
Glassfish' global settings won't factor in at all in that case.

Related

Loading log4j in ContextListener

I'm trying to move from log4j 1.x to 2.x and I'm having some trouble. I was using slf4j/log4j1.2.x but am trying to get off the slf4j stuff and just use log4j.
I have a web application that uses two shared jars that have their own log4j configuration files. Previously I was able to load them using DOMConfigurator:
DOMConfigurator.configure(Loader.getResource(sContext.getInitParameter(CoreServicesListener.INIT_SYSLOGGING_FILE)));
Not sure how to do this anymore in log4j2. I've checked several resources on the web and nothing seems to be getting me to the right path. I'm hoping someone can help.
The easiest solution is to replace your custom ServletContextListener with the log4j-web module (cf. documentation).
The log4j-web module uses the servlet context parameter "log4jConfiguration", which accepts string interpolation. If for any reason you can not change the old parameter name, you can set the value of "log4jConfiguration" to ${web:initParam.old_param_name} (cf. Web Lookup).
Remark: Log4j 1.x and 2.x configuration formats are incompatible. You need to convert the configuration files before migrating.

log4j2 Web Lookup not working for IBM WAS9 JVM Custom Property

We have Spring java-based web applications which use log4j2.xml for logging messages to files, etc.
We now need to update our log4j2.xml configs in order to be able to do a ${a.b.c} web lookup inside them so that we can use a JVM custom property value as part of the log file's name which the loggers log messages to. "a.b.c" is a JVM custom property name in IBM WAS9.0. However, when we deploy the apps, the log4j2 configurations fail to recognize any web lookup related stuff. The file created to log messages to is simply created with the name ${a.b.c} and no messages are actually logged in them. I have log4j-api, log4j-core, log4j-web in pom.xml.
I have read various docs online related to log4j2 web lookups when running in 3.0 servlets but I still can't see what the problem might be in our configurations. And I don't know what to look for in the log4j's trace logs in order to see what it is that we are missing.
Our Stack:
Spring 4.3.x
Solaris
Java 8
IBM WAS9.0
log4j-2.17.0 (log4j-api, log4j-core, log4j-web all in classpath)
I get this issue resolved by referencing the IBM WAS JVM custom property by ${sys:condir} in the log4j2.xml file.

Use log4j on a deployed war

I've made a trivial RESTful service on a JBoss server that just says "hello" when it receives any request. This part works and it is already deployed.
To go further with my project I need to log. The other applications on the server use log4j configured by a log4j.xml placed on a specific folder on the server.
For an offline project I'm used to have a runnable main method that in this case I would use to execute DOMConfigurator.configure(filepath) (where filepath is the log4j.xml) and I will be expecting to log with those settings.
However, since this is a deployed service (and since I'm not skilled enough to figure it myself) how would I so such a thing?
The question in my opinion could be interpreted in two ways:
How do I run some code "for sure" when I deploy something (in similar way to a main method) ? Do i need something like spring-boot?
How do I setup log4j on a deployed service on JBoss?
(I don't know if one question excludes the other...)
Thanks for the help!
1) If you want to run some code "for sure" you can create #PostConstruct method in one of your beans. Both Spring and EJB support it.
2)As for log4J configuration it should be enough to put it in classpath and add corresponding dependencies, no explicit configuration of path should be needed.

log4j writes to Systemerr.log on Websphere (configuration from java )

I have a web application, which uses log4j and slf4j as facade for logging. Logging is configured not from log4j.properties file, but from Java code.
Application is running on three different machines using Websphere Application Server.
Issue is that on two instances logging works as expected. But on the third one nothing is written in my logfile. Output goes to SystemErr.log instead (there are messages of ERROR and INFO levels).
Initially I thought that something is with Websphere server configuration, but later I found this link http://www.webagesolutions.com/knowledgebase/waskb/waskb004/ which says that such situation can be when log4j.properties can not be read.
But I am not using property file for this. And configuration form Java works OK on other two instances.
Any ideas what can be the issue?
Thank you in advance!
Please make sure that no alternative slf4j binding (such as simple) exists on the CLASSPATH.

how to set java option per webapplication in weblogic

how can i set a java_option - in my case java.util.logging.manager - per web application in weblogic ?
I cant set in globally, since weblogic logging also uses this same log manager.
I only need the logs from the web applications - to point to a different log manager.
thanks in advance. :)
You can set it in log4j for each application and have it written to different log file. You can specify the file path in log4j.appender.file.File variable

Categories