System log processing series (1) how to use logging, commons-logging, and log4j to output logs

Source: Internet
Author: User
Tags print format

Various exception information is inevitable during the operation of any system. to easily find the key to the problem, you need to create a log for the system to help analyze and find the problem, there are many methods to create logs. Here we will introduce one of the most popular methods. Logging, commons-logging, and log4 must be introduced. logging is a package that comes with JDK, while commons-logging is the abstraction of log4, we can use commons-logging or log4. Based on the idea of abstract programming, there are more people who choose to use commons-logging, if you need to change the log export package one day, it will be easy to replace. Let's use an instance to demonstrate how to export logs to files or the console. First, create a java project, import the three required packages in the project, and create a logtest test class and log4j. properties log configuration file. LogTest class [java] package com. bjsxt. logTest; import org. apache. commons. logging. log; import org. apache. commons. logging. logFactory; public class logTest {/*** @ param args */public static void main (String [] args) {Log logger = LogFactory. getLog (logTest. class); logger. debug ("DUBUG debugging information"); logger.info ("INFO"); logger. warn ("WARN information"); logger. error ("ERROR message"); logger. fatal ("FATAL information") ;}} log4j. properti Elasticsearch log configuration file [plain] view the CODE piece derived from my CODE piece ### direct log messages to stdout ### log4j. appender. stdout = org. apache. log4j. leleappender log4j. appender. stdout. target = System. out log4j. appender. stdout. layout = org. apache. log4j. patternLayout log4j. appender. stdout. layout. conversionPattern = % d {ABSOLUTE} % 5 p % c {1}: % L-% m % n ### direct messages to file hibernate. log ### log4j. appender. file = org. apache. log4j. fileAppen Der log4j. appender. file. file = d:/oa. log log4j. appender. file. layout = org. apache. log4j. patternLayout log4j. appender. file. layout. conversionPattern = % d {ABSOLUTE} % 5 p % c {1 }: % L-% m % n ### set log levels-for more verbose logging change 'info' to 'debug' ### log4j. rootLogger = warn, stdout, file # log4j.logger.org. hibernate = info # log4j.logger.org. hibernate = debug ### log HQL query parser activity # log4j. logger. Org. hibernate. hql. ast. AST = debug ### log just the SQL # log4j.logger.org. hibernate. SQL = debug ### log JDBC bind parameters ### log4j.logger.org. hibernate. type = info # log4j.logger.org. hibernate. type = debug ### log schema export/update ### log4j.logger.org. hibernate. tool. hbm2ddl = debug ### log HQL parse trees # log4j.logger.org. hibernate. hql = debug ### log cache activity #### log4j.logger.org. hibernate. cach E = debug ### log transaction activity # log4j.logger.org. hibernate. transaction = debug ### log JDBC resource acquisition # log4j.logger.org. hibernate. jdbc = debug ### enable the following line if you want to track down connection ###### leakages when using DriverManagerConnectionProvider ### log4j.logger.org. hibernate. connection. driverManagerConnectionProvider = trace log4j.logger.com. bjsxt = info And some important concepts, including logger, appender, level, and layout. 1. LoggerLogger writes log information to one or more output sources according to the format specified in the layout. Log4j allows developers to define multiple Logger, and each Logger has its own name, logger names are used to indicate the affiliation. 2. Level it also has an important attribute-log Level. Regardless of the log logging tool, it generally includes the following log levels, with the highest priority: DEBUG, INFO, WARN, ERROR, and FATAL. When the log information is printed in a program and the priority level is lower than the level specified in the configuration file, no processing is performed. For example, the priority level specified in the configuration file is WARN, if the program contains the code logger.info (message), the message will not be processed (output to the console or file). In log4j, log4j is used. rootLogger = [level], [appender used] log4.logger. [logger name] = [level], [appender used] to configure logger. If a logger is not configured, the configuration information of rootLogger is used. 3. Appender an Appender indicates an output destination. Appendr can be a console, text file, XML file, or Socket. A Logger can have multiple Appender, that is, it can output information to multiple locations. In log4j, use log4j. appender. [appender name] = [appender class name] log4j. appender. [appender name]. [appender attribute name] = [appender attribute value] to configure appender. 4. The LayoutLayout component is responsible for formatting the output log information. One Appender can only have one Layout. In log4j, use log4j. appender. [appender name]. layout = [layout class name] log4j. appender. [appender name]. layout. [layout attribute name] = [layout attribute name] to configure layout. log4j uses the print format similar to the printf function in C language to format log information. The print parameters are as follows: % m the output priority of the specified message % p in the output code, that is, DEBUG, INFO, WARN, ERROR, FATAL % r: The number of milliseconds that the application started to output the log information % c the category of the output, this is usually the full name % t of the class where the thread name % n that outputs the log event will output a carriage return line break, and the Windows platform will be "\ r \ n ", the Unix platform is "\ n" % d "the date or time at which the log is output. The default format is ISO8601. You can also specify the format after it, for example, % d {yyyyMM dd HH: mm: ss} % l log output The location where the component is located, including the category name, the thread that occurs, and the use of the tool that logs the number of lines in the Code are very simple. It is also an essential part of the system, you need to master several common log processing methods.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.