Log4j.properties:
# define the DEBUG priority, R for the purpose of the log output
Log4j.rootlogger= DEBUG, R
# Set the log output type to file type
Log4j.appender.r= Org.apache.log4j.FileAppender
# Set the location of the log files
Log4j.appender.r.file=. /visitemanage/logs/logrecord.log
# each time a new log message is written at the end of the file
log4j.appender.r.append= true
# Log Output Information format type
log4j.appender.r.layout= Org.apache.log4j.PatternLayout
# The log output Information format is newline, date, priority, [Full-class name], log information, line break
Log4j.appender.r.layout.conversionpattern=%n%d%p [%l]%m%n
Test code:
Package com.zhongren.Test;
Import Org.apache.log4j.Logger;
public class Test {
public static void Main (string[] args) {
Logger Logger = Logger.getlogger (Test.class.getName ());//Get logger, this logger will be responsible for controlling log information
try {
int i=10/0;
} catch (Exception e) {
Logger.info (E.getmessage ());//output log information using the Debug, info method of the Logger object
}
}
}
Test results:
Log4j program encountered error printing log to file