The path problem of dom4j and log4j

Source: Internet
Author: User

dom4j with log4j The path problem

 

When we write Java code, sometimes we need to read and write to the file, especially to read and write to some configuration files, such as the log4j.properties of log configuration file, and put it in the directory underground to properly configure log4j. Also, when we generate or read an XML document, we can write in the program to read the XML file or to put the XML file in the right place.

Let me give you a few examples

1. log4j the configuration file path issue.

First look at the directory structure of my program:

As in the procedure written propertyconfigurator.configure ("Log4j.properties");

The position of the file Log4j.properties in the following image will be executed correctly.

This means that the current directory in the source code refers to the root directory of the project. If I want to build a directory of config under the root directory and then put the configuration file log4j.properties under this directory, we have to change the code of the program to

"Propertyconfigurator.configure ("). /config/log4j.properties ");" Pay attention to the first/front one. ", otherwise the program will not run correctly.

OK, here is a list of my log4j.properties, use the source code of LOG4J, finally give out the log file output, so that everyone convenient to use.

Log4j.properties's File:

#####################################################################

# Categories and Levels

#####################################################################

Log4j.rootcategory=debug, Fileapp, Conapp

Log4j.category.de.jayefem=debug, Fileapp, Conapp

#####################################################################

# appenders

#####################################################################

# Conapp is set to be a consoleappender.

Log4j.appender.conapp=org.apache.log4j.consoleappender

Log4j.appender.conapp.target=system.out

Log4j.appender.conapp.layout=org.apache.log4j.patternlayout

Log4j.appender.conapp.layout.conversionpattern=%d{absolute}%5p%c{1}:%l-%m%n

# Fileapp

Log4j.appender.fileapp=org.apache.log4j.rollingfileappender

Log4j.appender.fileapp.file=./log/debug.log

log4j.appender.fileapp.maxfilesize=500kb

# Keep One backup file

Log4j.appender.fileapp.maxbackupindex=1

Log4j.appender.fileapp.layout=org.apache.log4j.patternlayout

log4j.appender.fileapp.layout.conversionpattern=%d [%t]%-5p%c-%m%n

The log output directory is the log below the engineering root directory

Log file output:

2006-08-07 05:12:58,109 [main] Debug niu.xml.ModifyXml-Start Debugging Modifyxml

2006-08-07 05:12:58,359 [main] DEBUG niu.xml.ModifyXml-Modify XML file success

2006-08-07 05:13:01,125 [main] Debug niu.xml.ModifyXml-Start Debugging Modifyxml

2006-08-07 05:13:01,156 [main] DEBUG niu.xml.ModifyXml-Modify XML file success

2006-08-07 05:22:28,109 [main] Debug niu.xml.ModifyXml-Start Debugging Modifyxml

2006-08-07 05:22:28,359 [main] DEBUG niu.xml.ModifyXml-Modify XML file success

Use log4j's source code:

Package niu.xml;

Import Java.io.File;

Import Java.io.FileWriter;

Import java.io.IOException;

Import Org.apache.log4j.Logger;

Import Org.apache.log4j.PropertyConfigurator;

Import org.dom4j.Document;

Import org.dom4j.DocumentException;

Import Org.dom4j.io.OutputFormat;

Import Org.dom4j.io.SAXReader;

Import Org.dom4j.io.XMLWriter;

Import org.eclipse.swt.events.SelectionEvent;

Import Org.eclipse.swt.events.SelectionListener;

/**

* @author YongJie

*

*/

public class Formatxml implements Selectionlistener

{

static Logger Logger = Logger.getlogger (formatxml.class);

public void widgetselected (Selectionevent arg0)

{

Niu_todo Automatic Generation Method stub

propertyconfigurator.configure ("./config/log4j.properties");

logger.debug (" start Debugging Formatxml");

Saxreader reader=new Saxreader ();

Document Document=null;

Try

{

Document=reader.read (New File ("Test.xml"));

logger.debug (" read source file to object document");

OutputFormat Format=outputformat.createprettyprint ();

Format.setencoding ("GB2312");

logger.debug (" set character encoding to GB2312");

Try

{

XMLWriter writer=

New XMLWriter (New FileWriter ("Testformat.xml"), format);

Writer.write (document);

Writer.close ();

Logger.debug ("Format file write succeeded");

catch (IOException E)

{

Niu_todo automatically generate catch blocks

Logger.debug ("Format file write Failed");

E.printstacktrace ();

}

catch (Documentexception e)

{

Niu_todo automatically generate catch blocks

Logger.debug ("read source file failed");

E.printstacktrace ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.