Specific items of Linux services

Source: Internet
Author: User
Tags log4j

log4j

log4j Introduction

LOG4J is an Apache open source project, through the use of log4j, we can control the log information delivery destination is the console, files, GUI components, even the socket server, NT Event recorder, UNIX syslog daemon, etc. We can also control the output format of each log, and by defining the level of each log information, we can control the log generation process more carefully. Most interesting of all, these can be configured flexibly with a single configuration file, without the need to modify the code of the application.

Log4j--log for Java (log in Java)
In emphasizing the development of reusable components today, in addition to developing a reusable log operation class from beginning to end, Apache provides us with a powerful log operation package-log4j.
In addition, by log4j other language interfaces, you can use log4j in C, C + +,. Net, PL/SQL programs with syntax and usage as in Java programs, enabling a unified, consistent Log component module for multilingual distributed systems. and by using a variety of third-party extensions, you can easily integrate log4j into a Java EE, Jini, or even SNMP application.

Use of License:apache License V2.0

Configuration log4j Four Steps

The first step is to add Log4j-1.2.8.jar (you can choose a higher version of log4j) to Lib.

The second step is to establish log4j.properties under Classpath. Refer to the file under/usr/local/cache-bin 14

The third step modifies this file-related property

Fourth Step

Include the relevant statement in the class to output the log: Define properties: Static Logger Logger = Logger.getlogger (Logdemo.class); Logdemo for the relevant class in the appropriate method: if (logger.isdebugenabled ()) {Logger.debug ("System ....");} example of a matching sample 1Log4j.rootcategory=info, stdout, RThis sentence is the output of the log information of level info to stdout and R, the definition of stdout and r in the following code, can be arbitrarily named. Level can be divided into off, FATAL, error, WARN, INFO, DEBUG, all, if the configuration off does not play any information, if configured as info so only display info, WARN, error log information, and DEBUG information will not be displayed, Refer to the third section to define the logger in the configuration file. 3log4j.appender.stdout= Org.apache.log4j.ConsoleAppender This sentence defines what type of output is named stdout, which can be org.apache.log4j.ConsoleAppender (console), Org.apache.log4j.FileAppe NDEr (file), Org.apache.log4j.DailyRollingFileAppender (generates a log file every day), Org.apache.log4j.RollingFileAppender (generates a new file when the file size reaches the specified size) Org.apac He.log4j.WriterAppender (send log information in stream format to any specified place) specifically, refer to the third section to define the Appender in the configuration file. 4log4j.appender.stdout.layout= Org.apache.log4j.PatternLayout This sentence defines the type of layout for the output named StdOut, which can be org.apache.log4j.HTMLLayout (layout in HTML table Form), org.apache.log4j . Patternlayout (flexibility to specify layout mode), Org.apache.log4j.SimpleLayout (contains the level and information string for log information), Org.apache.log4j.TTCCLayout (contains the time, thread, category, and so on) specifically, you can refer to the third section to define layout in the configuration file. 5log4j.appender.stdout.layout.conversionpattern= [QC]%p [%t]%c.%m (%l) | %m%n If you use the pattern layout to specify the specific format of the print information Conversionpattern, the printing parameters are as follows:%M the message specified in the output code;%m Output the method name of the log;%p output priority, Debug,info,warn, The error,fatal;%r output is the number of milliseconds that the log information is consumed from the application boot to output, and the class that the%c output belongs to is usually the full name of the class, the%t outputs the name of the thread that generated the log event,%n outputs a carriage return line break, the Windows platform is "RN", and the UNIX platform is "n ";%d the date or time of the output log time, the default format is ISO8601, or you can specify the format later, for example:%d{yyyy-mm-dd hh:mm:ss,sss}, output similar to: 2002-10-18 22:10:28,921;%l The location of the output log event, and the number of lines in the code; [QC] is the beginning of the log information, which can be any character, generally referred to as the project abbreviation. Output information [TS] DEBUG [main] Abstractbeanfactory.getbean (189) | Returning cached instance of singleton bean ' myautoproxy ' specific explanation you can refer to the third section to define the formatting log information in the configuration file. 7Log4j.appender.r=org.apache.log4j.dailyrollingfileappender This sentence is the same as the 3rd line. The type that defines the output that is named R produces a log file every day. 8Log4j.appender.r.file=d:\\tomcat 5.5\\logs\\qc.log This sentence to define the output end of the name R is D:\\tomcat 5.5\\logs\\qc.log can modify itself. 9Log4j.appender.r.layout=org.apache.log4j.patternlayout is the same as line 4th. TenLog4j.appender.r.layout.conversionpattern=%d-[ts]%p%t%c-%m%n is the same as line 5th. Alog4j.logger.com. Neusoft =debug Specifies the rank of all classes under the Com.neusoft package as Debug. You can change the Com.neusoft to the package name that you use for your project. -Log4j.logger.com.opensymphony.oscache=error -Log4j.logger.net.sf.navigator=error this is to set the error level below the two packages to errors, and if there is no Ehcache configured in the project, you do not need these two sentences. theLog4j.logger.org.apache.commons=error -Log4j.logger.org.apache.struts=warn These two sentences are struts's package. -Log4j.logger.org.displaytag=error This sentence is Displaytag's bag. (used by the QC Problem List page) -Log4j.logger.org.springframework=debug This sentence is spring's package. -Log4j.logger.org.hibernate.ps.preparedstatementcache=warn -Log4j.logger.org.hibernate=debug These two sentences are Hibernate's package. The above package settings can be customized according to the actual situation of the project.

Defining a configuration file

LOG4J supports two configuration file formats, one for XML (an application under standard Universal Markup Language), and one for Java attribute file Log4j.properties (key = value). The following describes how to use the Log4j.properties file as a configuration file: Configuring the root logger Configure log information output destination Appender Configuration Log Information Format (layout) layout

Application

So far, this article is about how to apply the log4j in application, and Java is now the mainstream of the application is EE and J2ME. Now, let's take a look at how to apply log4j in the development of Java EE. In fact, the application of log4j in Web application is also very simple, unlike applying log4j in application, the log4j is initialized before all code log4j is applied. So, we need to separate the LOG4J initialization work in the Web application, and put it in the servlet. Below, let's look at an example.

What is the relationship between SLF4J and log4j?
are two sets of interfaces for logging (log), Slf4j.jar and log4j, which are all compliant with common log standards. Can be said to be a competitive relationship, relatively speaking, log4j used relatively more. Hibernate logs are slf4j, but you can change the API to replace it with log4j, which is a lot of people.
SLF is hibernate provides a log interface, it can be log4j method to achieve, can also be common-logging method to achieve, and so on! Just an interface with the method implementation of the relationship!

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.