Adding logging to an application is generally based on three purposes: monitor the changes of variables in the code, and periodically log them to the file for other applications to perform statistical analysis work; Track code runtime traces as a basis for future audits; acting as a debugger in an integrated development environment, Prints debugging information for the code to the file or console. The most common approach is to embed a lot of print statements in your code that can be exported to a console or file, and it's better to construct a log action class that encapsulates such operations rather than having a series of print statements flooding the body of the code.
(a) log4j profileLog4j is an Apache open source project, through the use of log4j, can control the log information delivery destination is the console, files, GUI components, or even a socket server, NT event recorder, UNIX Syslog daemon, etc. can also control the output of each log By defining the level of each log information, we are able to control the log generation process in more granular detail. Most interesting of all, these can be configured flexibly with a single configuration file, without the need to modify the code of the application. In addition, by log4j other language interfaces, you can use log4j in C, c+ +,. Net, PL/SQL programs, with the same syntax and usage as in a Java program, which makes the multilingual distributed system get a consistent Log component module. and by using a variety of third-party extensions, you can easily integrate log4j into a Java EE, JINI, or even SNMP application.
(ii) LOG4J Basic use MethodThe log4j is comprised of three important components:
Priority of log information,
Output destination for log information,
output format for log information。 High-to-low priority for log informationERROR > WARN > INFO > DEBUG&NBSP, respectively, to specify the importance of this log information, the output destination of the log information specifies whether the log will be printed to the console or the file, and the output format controls the display of the log information. 1, define the configuration file You can also configure the LOG4J environment in your code without using the configuration file at all. However, using a configuration file will make your application more flexible. LOG4J supports two configuration file formats, one for XML format and one for Java properties files (Key = value properties file). Here's how to use the Java attribute file as a configuration file: 1) Configure the root Logger with the syntax: Log4j.rootlogger = [level], Appendername, Appendername, ... Level is the priority of logging, which is divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, all, or levels you define. LOG4J recommends using only four levels, with the priority from high to low being ERROR, WARN, INFO, DEBUG. By defining the level here, you can control the switch to the appropriate level of log information in your application. For example, if the info level is defined here, the log information for all DEBUG levels in the application will not be printed. Appendername refers to where the B log information is exported. You can specify multiple output destinations at the same time.  2) configuration log information output destination Appender, whose syntax is: log4j.appender.appenderName = fully.qualified.name.of.appender.class. among them, Log4j offers appender in the following ways: 1.org.apache.log4j.consoleappender (console), 2. Org.apache.log4j.FileAppender (file), 3.org.apache.log4j.dailyrollingfileappender (generates a log file every day), 4. Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size) 5.org.apache.log4j.Writerappender (send log information in stream format to any specified place) 3) Configure the format (layout) of the log information with the syntax: Log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class among them, log4j offers the following layouts: 1.org.apache.log4j.htmllayout (layout in HTML form), 2.org . apache.log4j.PatternLayout (Flexibility to specify layout mode), 3.org.apache.log4j.simplelayout (contains the level of log information and information string), 4. Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on of the log) log4j format the log information in a print format similar to the printf function in C, with the following printing parameters: %m The message specified in the output code%p Output priority, i.e. DEBUG, INFO, WARN, ERROR, FATAL%r output from application boot to output the log information consumes the number of milliseconds that the output belongs to the class, which is usually the full name of the class%t output the thread name that generated the log event%n Output a carriage return newline character, Windows platform is "RN", Unix Platform for "n"%d output log point of time date or time, the default format is ISO8601, can also be specified after the format, such as:%d{yyy MMM dd hh:mm:ss,sss}, output Out similar to: October 18, 2002 22:10:28, 921%l the occurrence of the output log event, including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10) 2. Use LOG4J1 in code) get logger use log4j, the first step is to get the logger, this logger will be responsible for controlling the log information. The syntax is: public static Logger GetLogger (String name) obtains the logger by the specified name and, if necessary, creates a new logger for the name. Name generally takes the name of this classWords, such as: static Logger Logger = Logger.getlogger (ServerWithLog4j.class.getName ()) 2) Read the configuration file when the logger is obtained, the second step configures the LOG4J environment with its syntax is: Basicconfigurator.configure (): Automatically and quickly using the default log4j environment. Propertyconfigurator.configure (String configfilename): Reads a configuration file written using the Java properties file. Domconfigurator.configure (String filename): Reads the configuration file in XML form. 3) Insert record information (format log information) when the two necessary steps are completed, you can easily insert a logging statement with different priority levels into any place where you want to log, with the following syntax: Logger.debug (Object message); Logger.info (Object message); Logger.warn (Object message); Logger.error (Object message)
(iii) instance description Import Org.apache.log4j.logger;import Org.apache.log4j.basicconfigurator;import org.apache.log4j.level; public class Log4jtest { public static void Main (String argv[]) { //Create A logger by the name of Class log4jtest. & Nbsp; logger Logger = Logger.getlogger (log4jtest.class); //Use the default configuration. Basicconfigurator.configure (); //Set the logger level to Level.info logger.setlevel (Level.info); //this request would be disabled since Level.debug < level.info. logger.debug ("This is Debug."); // These requests'll be a enabled. logger.info ("This was an info."); logger.warn ("This is a warning."); logger.error ("This was an error."); logger.fatal ("This is a fatal error."); return; }} Reference Link: http://heavyz.sourceforge.net/homepage/homepage_zh/comp/notes/log4j.htmlhttp://www.sxrczx.com/t/article/ ce422c717bc04db4af42d3ac5d020335.htmhttp://blog.csdn.net/anlina_1984/article/details/5313023
Org.apache.log4j.Logger usage