我对log4j有一个奇怪的问题。
调用
时org.apache.log4j.xml.DOMConfigurator.configure(configurationPath);
我们遇到的问题是,类路径中的 log4j.xml变空并且没有生成日志文件。
实际上我正在尝试使用我的Web应用程序中的 javax.xml.parsers.DocumentBuilderFactory 动态更新log4j.xml文件中的日志级别和日志文件路径,同时启动并运行tomcat。
更新log4j.xml后调用 org.apache.log4j.xml.DOMConfigurator.configure(Path / log4j.xml); 将log4j.xml文件更改合并到logger而不重新启动tomcat。这在我的本地Windows 7系统和prod linux实例中运行良好。 配置是Struts 1.3,Tomcat-8,Java-8和linux-2.6.18。
现在我们遇到登录上面提到的具有相同配置的新实例的问题,除了Linux版本,它是:Linux-2.6.32。
catalina.out中记录的问题
13 Jul 2017 05:33:10 INFO ChangeLogMode - XML modification Done and new file has placed in the path
13 Jul 2017 05:33:10 INFO ChangeLogMode - Now Logger mode will switch to = INFO
13 Jul 2017 05:33:10 INFO ChangeLogMode - If log4jfile Exists rebooting the log4j.xml to tomcat server == /apache-tomcat-8.0.36/webapps/ShipConsole/WEB-INF/classes/log4j.xml
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
log4j:WARN Fatal parsing error -1 and column -1
log4j:WARN Premature end of file.
log4j:ERROR Could not parse file [/ShipConsole/TEST/aasc/apache-tomcat-8.0.36/webapps/ShipConsole/WEB-INF/classes/log4j.xml].
org.xml.sax.SAXParseException; Premature end of file.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)
at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)
at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:891)
at com.aasc.model.ChangeLogMode.<init>(ChangeLogMode.java:178)
at com.aasc.util.Log4JInitServlet.init(Log4JInitServlet.java:49)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1238)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1151)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1038)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5337)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1816)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
13 Jul 2017 05:33:10 INFO ChangeLogMode - Initialized Log files with new modifications == /ShipConsole/TEST/aasc/apache-tomcat-8.0.36/webapps/ShipConsole/WEB-INF/classes/log4j.xml
我的log4j.xml文件是
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="logFIle" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="DynamicPathFromDB" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true" />
<param name="MaxFileSize" value="10GB" />
<!-- <param name="threshold" value="debug"/> -->
<param name="maxBackupIndex" value="1" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
</layout>
</appender>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="logFIle" />
<appender-ref ref="consoleAppender" />
</root>
</log4j:configuration>
并将ChangeLogMode.java改为edit.xml文件,即
javax.xml.parsers.DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File(configurationPath));
// Get the root element
Node appender = doc.getFirstChild();
Node debugFile = doc.getElementsByTagName("param").item(0);
NamedNodeMap attr = debugFile.getAttributes();
Node nodeAttr = attr.getNamedItem("value");
nodeAttr.setNodeValue(logPath+"ShipConsole.log");
logger.info("debugFile nodeAttr.setNodeValue done from ChangeLogMode == " + nodeAttr);
Node level = doc.getElementsByTagName("level").item(0);
NamedNodeMap levelAttribt = level.getAttributes();
Node nodeLevelAttr1 = levelAttribt.getNamedItem("value");
nodeLevelAttr1.setNodeValue(loggerMode);
logger.info("level nodeLevelAttr1.setNodeValue done from ChangeLogMode == " + nodeLevelAttr1);
// write the content into xml file
javax.xml.transform.TransformerFactory transformerFactory = TransformerFactory.newInstance();
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
javax.xml.transform.dom.DOMSource source = new DOMSource(doc);
FileOutputStream outputStream = new FileOutputStream(new File(configurationPath));
javax.xml.transform.stream.StreamResult result = new StreamResult(outputStream);
transformer.transform(source, result);
logger.info("XML modification Done and new file has placed in the path");
File log4jFile = new File(configurationPath);
if (log4jFile.exists()) {
logger.info("If log4jfile Exists rebooting the log4j.xml to tomcat server == "+configurationPath);
**org.apache.log4j.xml.DOMConfigurator.configure(configurationPath);**
logger.info("Initialized Log files with new modifications == "+configurationPath);
}
注意:我只是在新实例中遇到此问题,它在其他实例中使用相同的配置和相同的部署正常工作。您能请告诉我Linux实例是否依赖于使用DOMConfigurator.configure
请你帮我一个人帮忙。
先谢谢。