/*
* Created on Nov 13, 2004
*
* Copyright 2005 CafeSip.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cafesip.jiplet.standalone;
import java.io.File;
import java.net.URI;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.cafesip.jiplet.JipletLoggerPlugin;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl;
import com.sun.org.apache.xerces.internal.dom.ElementImpl;
/**
* @author amit
*
*/
public class Log4jLogger implements JipletLoggerPlugin
{
public static final String CATEGORY = "jiplet";
/**
*
*/
public Log4jLogger(String home, String logConfig) throws Exception
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
docBuilderFactory.setIgnoringComments(true);
docBuilderFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(logConfig);
Element element = doc.getDocumentElement();
// override the log file location
File log_dir = new File(home, "logs");
if (log_dir.exists() == true)
{
File log_file = new File(log_dir, "jiplet.log");
setLogFile(doc, element, log_file);
}
DOMConfigurator.configure(element);
}
private void setLogFile(Document doc, Element e, File logFile)
{
NodeList nodes = e.getElementsByTagName("appender");
for (int i = 0; i < nodes.getLength(); i++)
{
Node node = nodes.item(i);
NamedNodeMap a = node.getAttributes();
Node n = a.getNamedItem("name");
if (n == null)
{
continue;
}
if (n.getNodeValue().equals("rollingAppender") == false)
{
continue;
}
NodeList cnodes = node.getChildNodes();
for (int j = 0; j < cnodes.getLength(); j++)
{
Node cnode = cnodes.item(j);
if (cnode.getNodeName().equals("param") == true)
{
NamedNodeMap atts = cnode.getAttributes();
Node att = atts.getNamedItem("name");
if (att == null)
{
continue;
}
if (att.getNodeValue().equals("file") == false)
{
continue;
}
System.out.println("Found attribute file in the rolling appender, will go with that value");
return;
}
}
// DODO This is probably a bad idea (fiddling with Crimson impl classes).
// Using xerces will solve this problem.
String c = convertFileName(logFile);
ElementImpl el = new ElementImpl((CoreDocumentImpl)doc, "param" );
el.setAttribute("name", "file");
el.setAttribute("value", c);
System.out.println("Adding file atribute value " + c + " to log4j.xml");
node.appendChild(el);
}
}
private String convertFileName(File file)
{
URI uri = file.toURI();
return uri.getPath();
}
public void info(String message)
{
Logger.getLogger(CATEGORY).info(message);
}
public void warn(String message)
{
Logger.getLogger(CATEGORY).warn(message);
}
public void error(String message)
{
Logger.getLogger(CATEGORY).error(message);
}
public void fatal(String message)
{
Logger.getLogger(CATEGORY).fatal(message);
}
public void debug(String message)
{
Logger.getLogger(CATEGORY).debug(message);
}
public boolean isDebugEnabled()
{
return Logger.getLogger(CATEGORY).isDebugEnabled();
}
}