问题
I'm working on the development of an application that is deployed on WebLogic 10.3. It is packaged as an EAR and includes one module. The application works fine by itself but I am faced with issue related to logging.
I am using Log4j. The library is included in the EAR file and log4j.xml is placed under JAR module. So the config location is the following:
A.ear/B.jar/log4j.xml
Log4j config is following:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CA" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MMM-yyyy-HH:mm:ss} %p %C{1} - %m%n" />
</layout>
</appender>
<appender name="DRFA" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file"
value="servers/AdminServer/logs/EJB.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'-'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MMM-yyyy-HH:mm:ss} %p %C{1} - %m%n" />
</layout>
</appender>
<logger name="com.companyname.ejb" additivity="false">
<level value="DEBUG" />
<appender-ref ref="DRFA" />
<appender-ref ref="CA" />
</logger>
<logger name="com.companyname.results" additivity="false">
<level value="DEBUG" />
<appender-ref ref="DRFA" />
<appender-ref ref="CA" />
</logger>
<logger name="com.companyname.marketdata" additivity="false">
<level value="DEBUG" />
<appender-ref ref="DRFA" />
<appender-ref ref="CA" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="CA" />
</root>
When I build and deploy EAR (using Maven and customized WebLogic plugin) and call application no log file appears. But if I restart WebLogic everything is fine.
WebLogic is running under Windows 7 in domain mode with single node.
I'd like to know if there is some way to make the log appear without weblogic restart (since it can cause issues on production environment)?
Update: Also I'd like to know what is the reason of such behavior (i.e. why the log file is not created right after application deployment)? Is this an issue with Weblogic, log4j or with their coupling? I've tried to find the answer in the Oracle documentation, but no luck for now.
回答1:
Some notes:
- In prod environment, you probably want to have your log configuration outside app packages, so you can change log levels without redeployment.
- You should plan for production to be able to handle restarts. We usually have hot and cold servers, so load can be balanced and servers restarted when doing the deployment.
About the issue, if you want to, you could specify a servlet that is run on startup of app and configures your log4j. Something like:
web.xml
<servlet>
<servlet-name>SomeServlet</servlet-name>
<servlet-class>YourServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
Servlet
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.xml.DOMConfigurator;
public class YourServlet extends HttpServlet
{
@Override
public void init(final ServletConfig config)
throws ServletException
{
final java.net.URL url = Thread.currentThread().getContextClassLoader()
.getResource("Log4j.xml");
DOMConfigurator.configure(url);
}
}
There's also an example on the web about using a servlet context listener.
Edit. as to why this happens, weblogic logging mechanism is initiated by default on startup with these kind of settings:
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dlog4j.configuration=file:<path>/log4j.properties
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dweblogic.log.Log4jLoggingEnabled=true
so if you just redeploy your app without restarting the server, these settings will not be used -> logging will not get initiated.
回答2:
I didn't found any quick configuration solution, so code change was required.
I've added Weblogic Application Lifecycle listener as suggested here. Its postStart() method initializes log4j explicitly through DOMConfigurator and now logs appear right after application deployment.
The are few alternatives how configuration can be initialized. One of them was mentioned in eis' post and another here. But I've choose listener in order to keep single module in EAR and in order to avoid issues with singleton EJB on cluster environment (i.e. I am not sure if Weblogic will create singleton on each node or just one instance per cluster).
Also in order to prevent environment changes for local and dev environment, I am using internal log4j.xml (i.e. placed within ear file) there.
For stage and prod - external config file is specified (in Maven profile).
External file is monitored by log4j so changes will be automatically applied without any redeployments or restarts.
来源:https://stackoverflow.com/questions/13272115/log-file-not-produced-until-weblogic-is-restarted