Open Source Logging tool log4j Use presenter: Wang Shaohua QQ Group No.: 483773664 Learning Goal
Mastering the application of log4j in Java projects
I. Log classification and log4j introduction (i) Log and classification
1st log
It is mainly used to record some important operation information in the system operation.
-
Easy to monitor system operation, help users to detect and avoid possible problems in advance, or to find out the cause after a problem
2. Classification
SQL log: Records SQL statements executed by the system
Exception Log: Records exception events that occur during system operation
Business log: Records the system running process. such as user login, operation record
(ii) Introduction of LOG4J
Log4j is Apache is an open source project.
Control the output level of the log
Control log information Delivery destination is the console, files, etc.
Control the output format of each log
Official website: http://logging.apache.org/log4j/1.2/
The version we are using here is: 1.2.17
Second, use log4j log (a), add log4j jar file to the project
(ii), creation of the Log4j.properties file (iii),Configuring log information
12345678910111213 |
# # # Settings Logger Output level and Output Destination # # log4j.rootlogger=debug, Stdout,logfile # # # # # # # # # # # # # # # # # # # # # # # Log4j.appender.stdout=org.apache.log4j.consoleappender Log4j.appender.stdout.target=system.err log4j.appender.stdout.layout=org.apache.log4j.simplelayout # # # to output log information to a file: Wangsh.log # # # log4j.appender.logfile=org.apache.log4j.fileappender log4j.appender.logfile.file= Wangsh.log log4j.appender.logfile.layout=org.apache.log4j.patternlayout Log4j.appender.logfile.layout.conversionpattern=%d{yyyy-mm-dd hh\:mm\:ss}%l%F%p%m%n |
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/7F/B3/wKioL1cprnHBVFpAAACX-4l8-G8116.png " data_ue_src= "E:\My knowledge\temp\8fe36664-4145-406c-ba1b-5e634ec76d45.png" >
(iv), the use of log4j in the program
1234567891011121314151617181920212223242526 |
public class Test {
private static Logger logger = Logger.getLogger(Test.
class
.getName());
public static void main(String[] args) {
try {
Scanner in =
new Scanner(System.in);
System.out.print(
"请输入被除数:"
);
int num1 = in.nextInt();
logger.debug(
"输入被除数:" + num1);
System.out.print(
"请输入除数:"
);
int num2 = in.nextInt();
logger.debug(
"输入除数:" + num2);
System.out.println(String.format(
"%d / %d = %d"
, num1, num2, num1
/ num2));
logger.debug(
"输出运算结果:"
+ String.format(
"%d / %d = %d"
, num1, num2, num1 / num2));
}
catch (InputMismatchException e) {
logger.error(
"被除数和除数必须是整数"
, e);
}
catch (ArithmeticException e) {
logger.error(e.getMessage());
}
catch (Exception e) {
logger.error(e.getMessage());
}
finally {
System.out.println(
"欢迎使用本程序!"
);
}
}
}
|
From for notes (Wiz)
Learn from Miss Wang (eight): Use case of open source logging tool log4j