Java – do not generate log files until Weblogic is restarted
I am developing an application deployed on Weblogic 10.3 It is packaged as ear and contains a module The application itself works well, but I face problems related to logging
I'm using log4j The library is contained in the ear file, log4j XML is located under the jar module Therefore, the configuration location is as follows:
A. Log4j of ear / b.jar / xml
Log4j is configured as follows:
<?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 custom Weblogic plug-ins) and invoke the application, the log file is not displayed But if I restart Weblogic, everything will be fine
Weblogic uses a single node in domain mode to run under Windows 7
I wonder if there are some ways to display logs without restarting Weblogic (because it may cause problems in the production environment)?
Update: in addition, I want to know why this behavior occurs (that is, why log files are not created immediately after application deployment)? Is this Weblogic, log4j or their coupling problem? I tried to find the answer in the Oracle document, but now I have no luck
Solution
Some notes:
>In prod environment, you may want to put the log configuration outside the application package so that you can change the log level without redeployment. > You should plan production so that you can handle restart We usually have hot and cold servers, so we can balance the load and restart the server during deployment
For this problem, if you like, you can specify a servlet to run when the app starts and configure your log4j It's like:
web. In 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 is also an example on the web about using servlet context listeners
Edit As for why this happens, the Weblogic logging mechanism uses the following types of settings by default at startup:
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
Therefore, if you just redeploy the application without restarting the server, these settings will not be used – > logging will not be started