Log4j is the de facto Java standard logging tool. Is it possible to use log4j to a certain extent, it is a standard to measure whether a developer is a qualified Java programmer. If you're a Java programmer, if you're not using log4j, then you really need to read this article. Many friends reflect want to write the program log, but do not know how to write the log to the log file, and Java this aspect of information does not seem much. As described in how to write the Log4j.xml log configuration file. This article describes in detail how to use log4j in your project, and attaches an example of the nature of the code.
Why log4j. Some friends like to use System.out.println (String msg); Statement to debug the code. This is really a good debugging tool. But as the project ends, or the problem is solved, the statements need to be removed by hand. "How to delete all the System.out.println statements at once," the user in this post encountered such a problem.
All removed clean, the problem again: if there are problems in the future how to do it. Do you add again after the problem is resolved to find the deletion again.
The use of log4j can be a good solution to the above problems and contradictions. Log4j has a log level to control whether logging is performed. Its level from low to High is as follows:
All < DEBUG < INFO < WARN < ERROR < FATAL < off The purpose of grading logs is to be flexible. For example, if the log level is limited to debug, then info, WARN, ERROR, FATAL, and off are logged and, if the level is defined as info, the level below info, like the debug log, is not logged. For example, the following code:
View plain Copy to clipboard print? Logger log = Logmanager.getlogger (Helloworld.class); Log.info ("Info via class object to get Logger object"); Log.debug ("Debug uses class object to get Logger object"); Log.warn ("Warn to get Logger object through Class object"); If your log4j is defined as the output to the console, and the log level is defined as INFO, the console outputs the following log information if it is executed to the above statement block:
18:07:37,237 Infohelloworld:32-info to get Logger object by class object
18:07:37,245 Warnhelloworld:34-warn to get Logger object by class object
Specific example of the author has been uploaded to the attachment, interested friends can go to download to see. Limited to time, examples show only how to output log information to the console. Of course, logs can also be exported to files, databases. The author believes that if you figure out the sample code, the other smart readers can fully refer to the log4j API for themselves.
How to log4j. The first step: Download the LOG4J toolkit。
Official website address: http://logging.apache.org/log4j/1.2/index.html. The JavaDoc on the left menu bar below documentation is LOG4J's online API documentation. We clicked get log4j 1.2 Download to enter the download page, the latest version is 1.2.15, click Apache-log4j-1.2.15.zip to download the file, extract the log4j source code, sample code, installation instructions and log4j Jar Package (Log4j-1.2.15.jar).
Step two: Import log4j jar packages into the project。
The author develops the IDE is Eclipse, right-click the project name-> build path-> Configure build path-> Add External JARS-> Find the log4j-1.2.15 you extracted. Jar and select it. Are you sure.
Step Three: Edit the log4j.properties file and configure your log4j。
Under the SRC directory of the project, create a new log4j.properties file. The LOG4J is configured with this interface.
The log4j.properties configuration of the author in the sample code is as follows:
View plain Copy to clipboard print? ### Logger configuration ### #配置根 logger log4j.rootlogger=info,stdout ### Direct log messages to stdout ### (standard terminal output) #控制台输出 Log4j.appender.stdout=org.apache.log4j.consoleappender #自定义输出布局 log4j.appender.stdout.layout= Org.apache.log4j.PatternLayout #输出的格式 log4j.appender.stdout.layout.conversionpattern=%d{absolute}%5p%c{1}:%l-%m% N
Description
"Log4j.rootlogger=info,stdout" sentence: Defines the root logger configuration item. Its syntax is: level, input Terminal 1, output Terminal 2, .... The stdout represents the log output to the console.
The log4j.appender.stdout.layout.conversionpattern=%d{absolute}%5p%c{1}:%l-%m%n statement defines the print format. The printing parameters are as follows:
%M: The message specified in the output code.
%p: Output priority.
%r: Enter the number of milliseconds to consume from the application boot to the output of this log information.
%c: The class that the output belongs to, usually the full name of the class in which it is located.
%t: Output The thread name that generated the log thread.
%n: Output a carriage return line feed. Windows platform is "/r/n" and Unix is "/n".
%d: the date or time of the output log point of time, the default format is ISO8601, the recommended use of "%d{absolute}", this output format, such as: "2007-05-07 18:23:23,500", in line with Chinese habits.
%l: The location of the output log event, including the class name, the thread name, and the number of lines in the code.
Step Fourth: Log code where logging is required in your code。
Log output via Logger log = Logmanager.getlogger (the class of the code you want to log in), get the Logger object, and then call its info, debug, and so on. The sample code is as follows:
View plain Copy to clipboard print? /** * filename:helloworld.java * * version information: * Date: 2010-1-18 * Copyright XXX Corporation 2010 * Copyright * */ package com.defonds.log4j; import org.apache.log4j.logmanager; import org.apache.log4j.logger; /** * * project name:testlog4j * Class name:helloworld * class Description:log4j test class * founder:defonds * Creation time:2010-1-18 pm 02:58:18 * modifier:defonds * Modification time:2010-1-18 PM 02 :58:18 * Modify remarks: * @version * */ public class helloworld { public static void main (String[ ] args) { &Nbsp; logger log = logmanager.getlogger (helloworld.class); log.info ("info via class object to get logger object"); log.debug ("Debug through class objects to get logger objects"); log.warn ("Warn through class objects to get logger Objects "); } }
Again: This example is just to get entry-level knowledge from friends who have not used log4j, log4j powerful logging capabilities not only with this. More log4j of the log function, but also need readers in the project to know firsthand.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.