I have a strange issue with log4j.
When calling the
org.apache.log4j.xml.DOMConfigurator.configure(configurationPath);
we are getting the issue that, log4j.xml in class path is getting empty and no log file is generated.
Actually I am trying to update the log level and logging file path in log4j.xml file dynamically using javax.xml.parsers.DocumentBuilderFactory from my web application while tomcat is up and running.
after updating the log4j.xml calling org.apache.log4j.xml.DOMConfigurator.configure(Path/log4j.xml); to incorporate log4j.xml file changes to logger with out restarting the tomcat.
This is working fine in my local windows 7 system and prod linux instances. Where the configuration is Struts 1.3, Tomcat-8, Java - 8, and linux-2.6.18.
Now we are getting issue with logging in new instance having same configurations mentioned above except linux version here it is : Linux-2.6.32.
Issue logged in 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
my log4j.xml file is
<?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>
And ChangeLogMode.java to edit.xml file i.e
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);
}
Note : I am getting this issue only in that new instance, it is working fine with same configurations and same deployment in other instances. Could you please suggest me whether the Linux instance has any dependency on using DOMConfigurator.configure
Could you please some one assist me on this.
Thanks in Advance.