Create a project:
Preparations for using log4j in log4j,
The first jar package, need to log4j-1.2.11.jar package, import to your own project;
2. Create the log4j. properties file and fill in the corresponding configuration. I have filled in two configurations: Console output and file output.
The configuration is as follows:
### Output log information to the console
### Log4j. appender. stdout = org. Apache. log4j. leleappender
### Print the information to system. Out
### Log4j. appender. stdout. Target = system. outlog4j. appender. stdout. layout = org. Apache. log4j. patternlayout
### Specify the output format: Display date and log information
### Log4j. appender. stdout. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS} % m % N
### Output log information to the file F: \ log \ test. Log
### Log4j. appender. File = org. Apache. log4j. fileappender
### Specify the log output file name
### Log4j. appender. file. File = F: \ log \ test. loglog4j. appender. file. layout = org. Apache. log4j. patternlayout
### Specify the output format: Display date, log location, and log information
### Log4j. appender. file. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS} % L % m % N
### Set priorities and output sources ###
### Set priority to debug
### Log4j. rootlogger = debug, stdout, file
Configuration complete! Write a test program !!!
Log4j. Java
Import org. Apache. log4j. Logger;
Public class log4j {
Public static void main (string [] ARGs ){
// Obtain the logger, which controls the log information
Logger logger = logger. getlogger (log4j. Class. getname ());
Try {
// Use the debug and info methods of the logger object to output log information
Logger. debug ("sets the instructor ID. ");
} Catch (illegalargumentexception ex ){
// Use the debug and info methods of the logger object to output log information
Logger.info (ex. getmessage ());
}
}
}
Test result: This is the output result of the console:
21:35:41 set the instructor number.
The file output is not easy to display. I will not display it here. Please try it on your own !!
And this is the related knowledge http://logging.apache.org/log4j/1.2/download.html of log4j.