Logging log files using log4j
How to use log4j log
Step one: Add the jar file used by log4j in the project
1: Items > Properties: Popup Properties window for item
2:java build path to add external jar: Pop-up selection jar window
3: By selecting the Jar window, find the Log4j-1.2.x.jar and confirm
4: Return to the project's Properties window, click OK
Step Two: Create the Log4j.properties file
1: Choose to use the LOG4J project "right click src" "new"? Other: Pop-up Selection wizard window
2: In the Selection wizard window, select the general?> file?> Next: Pop Up the new file window
3: In the New File window, enter the filename log4j.properties? Finish: Create end of work
Step three: Use log4j to log information
import Org.apache.log4j.Logger;
public class Log4j {
public static void Main (string[] args) {
Logger Logger = Logger.getlogger (AccpTeacherLog4j.class.getName ());//Get logger, this logger will be responsible for controlling log information
try {
Logger.debug ("Sets the number of the instructor.") //Use the Debug and info method of the Logger object to output log information
catch (IllegalArgumentException ex) {
Logger.info (Ex.getmessage ())//output log information using the Debug, info method of the Logger object
}
}
}
Fourth Step: Write Log4j.properties file, configure log information
### output log information to the console ###
log4j.appender.stdout=org.apache.log4j.consoleappender//log information will be written to the console
log4j.appender.stdout.target=system.out//information is printed on the System.out
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=%D{YYYY-MM-DD HH:mm:ss}%m%n//Specify output format: Displaying date and log information
### output log information to file: Accp.log ###
log4j.appender.file=org.apache.log4j.fileappender//log information will be written to the file
log4j.appender.file.file=accp.log//Specifies the file name of the log output
Log4j.appender.file.layout=org.apache.log4j.patternlayout
Log4j.appender.file.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%l%m%n//Specify output format: Display date, where the log occurred and log information
### sets the priority level, as well as the output source ###
Log4j.rootlogger=debug, stdout, file//setting priority level is debug,
Log is output to multiple output sources
Note: Priority from high to low is error, WARN, INFO, DEBUG
Here, if the priority level is set to info, the log information printed using the Debug method will not be output