Write logs with log4j

Source: Internet
Author: User

1. Download the import Package Commons-logging.jar and Log4j-1.2.9.jar
2, write the configuration file log4j.properties, the content is as follows: Log4j.rootcategory=info, LOGFILE//logfile indicates that the log is written to a file, and if it is console, writes the log to the console Log4j.logger.org.apache.axis2.enterprise=fatal Log4j.logger.de.hunsicker.jalopy.io=fatal Log4j.logger.httpclient.wire.header=fatal Log4j.logger.org.apache.commons.httpclient=fatal Log4j.appender.logfile=org.apache.log4j.dailyrollingfileappender
///The following two sentences are set to generate a log file each day, the log file name is "Log_", the day before each day will be automatically renamed to "Log_ date. Log" //To test whether it is correct, you can generate "Log_", then modify the system time to generate the log, the day before the "Log_" will be renamed, while generating a new "Log_" log4j.appender.logfile.file=log/log_//the directory and log file names of the log files log4j.appender.logfile.datepattern=yyyy-mm-dd '. Log '
Log4j.appender.logfile.append=true//true indicates that the log is written in Append mode, and False indicates the overwrite Log4j.appender.logfile.layout=org.apache.log4j.patternlayout log4j.appender.logfile.layout.conversionpattern=%d [%t]%-5p%c%x-%m%n
3. Java code calls import Org.apache.log4j.Logger; import Org.apache.log4j.PropertyConfigurator; Public class Testlog { Public static void Main (String[]args) { // TestLog.class.getName () returns the name of the class, which is actually a string that can be substituted with any string, such as "Error Location: Testlog class" Logger Logger = Logger.getlogger (TestLog.class.getName ()); //Set configuration file location, if not set to put the configuration file to the class directory or root directory propertyconfigurator.configure ("./config/log4j.properties"); logger.info ("Log content .....") ");  } }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Write logs with log4j

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.