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