Вы находитесь на странице: 1из 3

1/3/2016

Log4jexampleusinglog4jxml|Tutorialspointexamples

Tutorialspointexamples
(http://tutorialspointexamples.com)
Learnjavatutorialwithexamplesforbeginnersonline
Search

Log4jExampleUsingLog4jXmlFile
Wecanalsouseanxmlfilenamedlog4j.xmltoconfiguretheLog4j.Letusdiscusstheuseoflog4j.xml
configurationfilewiththehelpofbelowexample.

Example:
Log4jTest.java
importorg.apache.log4j.Logger;

/**
*Thisclassisusedtoshowtheuseof
*Log4jwiththelog4j.xmlfile.
*@authorjavawithease
*/
publicclassLog4jTest{

//GettheLoggerobject.

privatestaticLoggerlog=Logger.getLogger(Log4jTest.class);

publicstaticvoidmain(String[]args){

//loggermessages

log.debug("Log4jdebugmessagetest.");

log.info("Log4jinfomessagetest.");

log.warn("Log4jwarnmessagetest.");

log.error("Log4jerrormessagetest.");

log.fatal("Log4jfatalmessagetest.");

}
}

log4j.xml

http://tutorialspointexamples.com/log4jexampleusinglog4jxmlfile/

1/6

1/3/2016

Log4jexampleusinglog4jxml|Tutorialspointexamples

<?xmlversion="1.0"encoding="UTF8"?>
<!DOCTYPElog4j:configurationSYSTEM"log4j.dtd">
<log4j:configurationxmlns:log4j='http://jakarta.apache.org/log4j/'>

<appendername="CA"class="org.apache.log4j.ConsoleAppender">

<layoutclass="org.apache.log4j.PatternLayout">

<paramname="ConversionPattern"value="%m%n"/>

</layout>

</appender>

<root>

<levelvalue="info"/>

<appenderrefref="CA"/>

</root>
</log4j:configuration>

Output:
Log4jinfomessagetest.
Log4jwarnmessagetest.
Log4jerrormessagetest.
Log4jfatalmessagetest.

Downloadthisexample.(http://tutorialspointexamples.com/wp
content/uploads/2014/09/Log4jExample5.rar)

NextTopic:Logginglevels.(http://tutorialspointexamples.com/logginglevelsinlog4j/)
PreviousTopic:Log4jexampleusinglog4jpropertiesfile.
(http://tutorialspointexamples.com/log4jexampleusinglog4jpropertiesfile/)

RelatedTopics:
Howtobuildjavaprojectusingantineclipse?(http://tutorialspointexamples.com/howtobuildjava
projectusingantineclipse/)
JAXBmarshallingconvertjavaobjecttoxmlexample.(http://tutorialspointexamples.com/jaxb
marshallingconvertjavaobjecttoxmlexampleusingonepojo/)
HowtocreatepdffileinjavausingiTextjar?(http://tutorialspointexamples.com/howtocreatepdffile
injavausingitextjar/)
Genericsclassexample.(http://tutorialspointexamples.com/genericsclassexample/)
OGNLinstruts2.(http://tutorialspointexamples.com/ognlinstruts2/)
HibernateOnetoOneMappingusingxml.(http://tutorialspointexamples.com/hibernateonetoone
mappingusingxml/)
SendinlineimageinemailusingJavaMailAPI.(http://tutorialspointexamples.com/sendinlineimage
inemailusingjavamailapi/)
Quartz2JobListenerexample.(http://tutorialspointexamples.com/quartz2joblistenerexample/)

10

0
0

Share
Share

9
0

http://tutorialspointexamples.com/log4jexampleusinglog4jxmlfile/

Share

Share

2/6

1/3/2016

Log4jexampleusinglog4jxml|Tutorialspointexamples

Log4j Tutorial
Log4joverview.(http://tutorialspointexamples.com/log4joverview/)
Loggerclass.(http://tutorialspointexamples.com/loggerclass/)
Log4jexample.(http://tutorialspointexamples.com/log4jexample/)
Log4jpropertiesfile.(http://tutorialspointexamples.com/log4jexampleusinglog4jpropertiesfile/)
Log4jxmlfile.(http://tutorialspointexamples.com/log4jexampleusinglog4jxmlfile/)
Logginglevels.(http://tutorialspointexamples.com/logginglevelsinlog4j/)
Log4jfileappender.(http://tutorialspointexamples.com/log4jfileappender/)
Log4jmultipleappenders.(http://tutorialspointexamples.com/log4jmultipleappendersexample/)

http://tutorialspointexamples.com/log4jexampleusinglog4jxmlfile/

3/6

Вам также может понравиться