Log4j has three main components: Loggers, Appenders, and Layouts, which can be easily understood as log categories, where logs are output from the logs, and logs. Comprehensive use of these three components can easily record the type and level of information, and can control the style and location of the log output at runtime. The following describes the three components:
1, Loggers
Loggers Components are divided into five levels in this system: Debug, Info, Warn, Error and Fatal. These five levels are sequential, Debug Java programs are for example: // Establish an instance of Logger, named "com.foo" Logger logger = logger.getlogger ("com.foo"); // Set the level of the logger. The level of logger is usually not set in the program. Generally set in the configuration file. Logger.SetLevel (Level.info); Logger Barlogger = logger.getlogger ("com.foo.bar"); // This request is available because Warn> = Info Logger.warn ("Low Fuel Level."); // The following request is not available because Debug Logger.debug ("Starting Search for NEAREST GAS Station); // Instance Barlogger named "com.foo.bar" will inherit the level of instance "com.foo". Therefore, the request is available below, because info> = info Barlogger.info ("Located Nearest Gas Station); // The following request is not available because Debug Barlogger.debug ("EXITING GAS STATION SECH"); The meaning of "whether it is available" is whether Logger information can be output. When naming the Logger instance, there is no restriction, you can take the name of your own interest. Under normal circumstances, it is recommended to name the Logger instance with the location of the class, which is currently a relatively effective logger naming method. This makes each class build your own log information for easy management. such as: Static logger logger = logger.getlogger (Clientwithlog4j.class.getname ()); 2, Appenders Disabling and using log requests are just a small place in log4j, and the log4j log system allows logs to be output to different places, such as console, files (files), generate new files according to the number of days or file size, The form of flow is sent to other places. Its syntax is: Org.apache.log4j.consoleappender (console), Org.apache.log4j.fileAppender (file), org.apache.log4j.dailyRollingFileAppender (a log file is generated), org.apache.log4j.rollingFileAppender generates a new file when specifying the specified size, org. How to use when configured: Apache.log4j.writeraplender (send log information to any specified place): Log4j.Appender.Appendername = flly.qualified.name.of.Appender.class Log4j.appender.Appendername.Option1 = value1 ... Log4j.appender.Appendername.Option = VALUEN This provides a considerable convenience for the output of the log. 3, Layouts Sometimes users want to format their log output according to their own preferences. LOG4J can add Layouts to the back of Appenders to complete this feature. Layouts provides four log output styles, such as based on HTML styles, freely specify styles, contains log levels and information styles and styles including log time, thread, category, and more. Its syntax is: Org.apache.log4j.htmlLayout (layout in HTML table), org.apache.log4j.patternlayout (can be flexibly specified), org.apache.log4j.simplelayout (level and information string containing log information), Org.apache.log4j.ttccLayout (including information, thread, category, etc.) The mode is used in the configuration: log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class log4j.appender.appenderName.layout.option1 = value1 ... log4j.appender.appenderName.layout.option = valueN above illustrates the principle of respect from Log4j Using the method, the log4j can be used in the specific Java programming using the log4j. 2, read the configuration file: After obtaining the logger instance, the next will be configured using the log4j usage environment: Syntax representation: BasicConfigurator.configure (): Automatically use the default Log4j environment automatically. PropertyConfigurator.configure (String configFileName): Read the configuration file written by using the characteristic file using Java. Domconfigurator.configure (String FileName): Read the configuration file in the XML form. Actual use: PropertyConfigurator.configure ("ServerWithlog4j.properties"); Syntax representation: logger.debug (Object Message); logger.info (Object Message); logger.rror (Object Message); actual use: logger.info ("Serversocket Before Accept: Server ); When actually programming, enabling log4j to actually define the configuration file in the system. The definition step is to use the Logger, Appender and Layout, as follows: 1, configure the root logger, its syntax is: log4j.rootlogger = [level], appendername, appendername, ... Here Level refers to the priority of Logger, appendername is a log The output of information can be specified at the same time to specify a plurality of output. Such as: log4j.rootlogger = INFO, A1, A22, configuration log information output destination, its syntax is: log4j.qualified.Appendername = full.qualified.name.Of.Appender.class can specify the five destinations described above one of.