log4j and log4j.properties configuration instructions (Collectors Edition)

log4j and log4j.properties configuration shows the development of reusable components in the stress of today, in addition to its own from start to finish development of a reusable logging operations outside class, Apache provides us with a powerful logging operations package-Log4j.

Log4j is an open source Apache project, through the use of Log4j, we can control the destination of the log information transmission is the console, file, GUI components, or even socket servers, NT's event recorder, UNIX Syslog daemon, etc. ; we can control the output format of each log; by defining the level of each log message, we can more carefully control the log generation process. The most interesting is that these can be a configuration file to be configured flexibly, without modifying application code.

In addition, Log4j other language interface, you can C, C + +,. Net, PL / SQL procedure using Log4j, its grammar and usage with the Java program the same, making multi-language distributed system to get a uniform and consistent log group pieces of the module. Moreover, by using a variety of third-party extensions, you can easily be integrated into the Log4j J2EE, JINI or even SNMP applications.

Note: The following is divided into three parts, the first to explain how to configure log4j, log4j.properties configuration file on the second part of the various attributes of the talks, the third part of the log4j detailed explanations, if only want to configure the log4j, then Just look at the first two parts can, if you want to log4j-depth understanding of the need to see the third part.

1, Log4j configuration

The first step: Add log4j-1.2.8.jar to the lib next.

Step Two: In the CLASSPATH established under log4j.properties. As follows:

log4j.rootCategory=INFO, stdout , R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender log4j.appender.R.File=D:\Tomcat 5.5\logs\qc.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
log4j.logger.com.neusoft=DEBUG
log4j.logger.com.opensymphony.oscache=ERROR
log4j.logger.net.sf.navigator=ERROR
log4j.logger.org.apache.commons=ERROR
log4j.logger.org.apache.struts=WARN
log4j.logger.org.displaytag=ERROR
 log4j.logger.org.springframework=DEBUG
log4j.logger.com.ibatis.db=WARN
log4j.logger.org.apache.velocity=FATAL
log4j.logger.com.canoo.webtest=WARN
log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN
log4j.logger.org.hibernate=DEBUG
log4j.logger.org.logicalcobwebs=WARN

The third step: modify the corresponding attributes must know before changes are doing, in the second part of the explanation.

Step Four: Output log in to add a class related statement:

Custom attribute: protected final Log log = LogFactory.getLog (getClass ());
In the corresponding method:
if (log.isDebugEnabled ())
(
log.debug ("System ... ..");
)

Second, Log4j Help

1 log4j.rootCategory = INFO, stdout, R

Sentence to the level of output for the INFO log messages to stdout and R the two destinations, stdout, and R, defined in the following code, which could be named. Level can be divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL, OFF if not play any configuration information, if configured to INFO This show only INFO, WARN, ERROR in the log information, DEBUG messages will not be displayed , can refer to the third part, explain the definition of the configuration file logger.

3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender

Sentence for the definition of named stdout the output of what type, can be

org.apache.log4j.ConsoleAppender( Console  ),

org.apache.log4j.FileAppender( File  ),

org.apache.log4j.DailyRollingFileAppender( Generates a log file every day  ),

org.apache.log4j.RollingFileAppender( The file size reaches the specified size, when a new file  )

org.apache.log4j.WriterAppender( To log information to stream format sent to any designated  )

Can refer to the third part, explain the definition of the configuration file Appender.

4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

Sentence for the definition of named stdout the output of the layout is what type, can be

org.apache.log4j.HTMLLayout( The layout in HTML table format  ),

org.apache.log4j.PatternLayout( The flexibility to specify the layout mode  ),

org.apache.log4j.SimpleLayout( The level that contains the log information and the information string  ),

org.apache.log4j.TTCCLayout( Contains the log generated by the thread of time.  . Categories and more information  )

Can refer to the third part, explain the definition of the configuration file Layout.

5 log4j.appender.stdout.layout.ConversionPattern= [QC] %p [%t] %C.%M(%L) | %m%n

If you use a pattern layout of the printed information must specify the specific format ConversionPattern, print parameters are as follows:

Quote

Specified output code% m message

% P output priority, that is DEBUG, INFO, WARN, ERROR, FATAL

% R output from the application starts to output the log information on the number of milliseconds spent

% C output respective categories, usually the full name of the host class

% T output generated the log event thread name

% N output a carriage return line feed, Windows platform for the "rn", Unix platform "n"

% D output log time date or time, the default format for the ISO8601, you can specify the format in the following example:% d (yyyy MMM dd HH: mm: ss, SSS), output similar to: October 18, 2002 22:10:28,921

% L output location of the log events, including the category name, place of thread, and the number of lines in the code.

[QC] is the beginning of log information, for any character, generally referred to for the project.

Output information

[TS] DEBUG [main] AbstractBeanFactory.getBean(189) | Returning cached instance of singleton bean 'MyAutoProxy'

Can refer to the third part, explain the definition of the configuration file formatted log information.

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender

Sentence with the same line 3. Called R, the definition of the type of output generated per day in a log file.
log4j.appender.R.File = D: \ Tomcat 5.5 \ logs \ qc.log

Sentence called for the definition of R, the output file named D: \ Tomcat 5.5 \ logs \ qc.log

You can modify.
log4j.appender.R.layout = org.apache.log4j.PatternLayout

With the same line 4.
log4j.appender.R.layout.ConversionPattern =% d-[TS]% p% t% c -% m% n

With the same line 5.
log4j.logger.com. neusoft = DEBUG

Designated com.neusoft all classes under package hierarchy to DEBUG.

Can com.neusoft to their package name used in the project.
log4j.logger.com.opensymphony.oscache = ERROR
log4j.logger.net.sf.navigator = ERROR

These two are two packages under this error level set to ERROR, if the project is not configured EHCache, you do not need these two.
log4j.logger.org.apache.commons = ERROR
log4j.logger.org.apache.struts = WARN

These two is the struts package.
log4j.logger.org.displaytag = ERROR

The message is displaytag package. (QC issues list, used in)
log4j.logger.org.springframework = DEBUG

Sentence for the Spring of the package.
log4j.logger.org.hibernate.ps.PreparedStatementCache = WARN
log4j.logger.org.hibernate = DEBUG

The two are hibernate package.

These packages can set the actual circumstances of the project on their own custom.

3, log4j Xiangjie

1, define the profile

Log4j supports two configuration file format, a file format is XML, a Java properties file is log4j.properties (key = value). Here are a log4j.properties file as a configuration file to use the method:

① , configure the root Logger

Logger responsible for most of the logging operation.

The syntax is:

log4j.rootLogger = [level], appenderName, appenderName, ...

One, level is the priority of the log records are divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or a custom level. Log4j recommended to use only four levels, from high to low priority level are ERROR, WARN, INFO, DEBUG. Defined by the level here, you can control the application of the appropriate level of log messages switch. For example, in the definition of the INFO level here is only equal to and above this level before processing, the application of all the DEBUG level log messages will not be printed. ALL: Print all of the logs, OFF: turn off all the log output. appenderName is the designated place to which the log information output. Can also specify multiple output destinations.

② , configure the log information output destination Appender

Appender is responsible for controlling the output of the logging operation.

The syntax is:

log4j.appender.appenderName = fully.qualified.name.of.appender.class

log4j.appender.appenderName.option1 = value1

-

log4j.appender.appenderName.optionN = valueN

Here appenderName defined in ① where can arbitrarily named.

Which, Log4j appender provides the following:

Quote

org.apache.log4j.ConsoleAppender (console),

org.apache.log4j.FileAppender (file)

org.apache.log4j.DailyRollingFileAppender (create a log file every day),

org.apache.log4j.RollingFileAppender (file size reaches the specified size when create a new file), set by log4j.appender.R.MaxFileSize = 100KB file size, can also be set by log4j.appender.R.MaxBackupIndex = 1 To save a backup file.

org.apache.log4j.WriterAppender (stream format the log message to send to any designated place)

For example: log4j.appender.stdout = org.apache.log4j.ConsoleAppender

Define a named stdout the output destination, ConsoleAppender for the console.

③ , configure the log message format (layout) Layout

Layout Appender is responsible for formatting the output.

The syntax is:

log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class

log4j.appender.appenderName.layout.option1 = value1

-

log4j.appender.appenderName.layout.optionN = valueN

Which, Log4j layout provides the following:

org.apache.log4j.HTMLLayout( The layout in HTML table format  ),

org.apache.log4j.PatternLayout( The flexibility to specify the layout mode  ),

org.apache.log4j.SimpleLayout( The level that contains the log information and the information string  ),

org.apache.log4j.TTCCLayout( Contains the log generated by the thread of time.  . Categories and more information  )

2, formatted log information

Quote

Log4J similar to C language printf function to format the log message print format, print parameters are as follows:
Specified output code% m message
% P output priority, that is DEBUG, INFO, WARN, ERROR, FATAL
% R output from the application starts to output the log information on the number of milliseconds spent
% C output respective categories, usually the full name of the host class
% T output generated the log event thread name
% N output a carriage return line feed, Windows platform for the "rn", Unix platform "n"
% D output log time date or time, the default format for the ISO8601, you can specify the format in the following example:% d (yyyy MMM dd HH: mm: ss, SSS), output similar to: October 18, 2002 22:10:28,921
% L output location of the log events, including the category name, place of thread, and the number of lines in the code.


3, in the code to use Log4j

We need to output the log information in the class to do the following three tasks:
1, import all required commongs-logging categories:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

2, in their own class to define a class org.apache.commons.logging.Log private static class members:
private final Log log = LogFactory.getLog (getClass ());
LogFactory.getLog () method of the parameters used in the current class of the class.

3, using the org.apache.commons.logging.Log class member method output the log information:

if (log.isDebugEnabled())
{
log.debug("111");
}

if (log.isInfoEnabled())
{
log.info("222");
}

if (log.isWarnEnabled())
{

log.warn("333");
}

if (log.isErrorEnabled())
{

log.error("444");
}

if (log.isFatalEnabled())
{

log.fatal("555")

}
分类:Java 时间:2010-09-20 人气:196
分享到:
blog comments powered by Disqus

相关文章

  • java program to run a detailed analysis of mechanisms 2010-07-14

    JVM (Java Virtual Machine) was used to compute the standard equipment can be used in different ways (software or hardware) to be achieved. Compile virtual machine instruction set and compiler microprocessor instruction set very similar. Java Virtual

  • java program in the exception handling experiences (2) log4j error logging 2010-05-12

    1.1 Related English Xiangjie log: log debug: debug error: error warn: warning info: news 1.2 What is log4j? Why should log4j? log4j Definition: log4j is a popular, good logging tool, it can output a variety of flexible methods of log information; Why

  • Java: log4j and log4j.properties configuration instructions 2010-07-29

    Java: log4j and log4j.properties configuration instructions [Source: Java | author: Java | Time: 2007-7-20 | to Forum] - development of reusable components in the stress of today, in addition to its own from start to finish development of a reusable

  • Use Log4j for logging operations 2010-04-12

    Outline 1.1. Background Add in the application log records based on three general purposes: to monitor changes in the code variable, periodic records to a file for other applications for statistical analysis; tracking code to run when the track, as t

  • [Transfer] Apache Log4j configuration instructions 2010-12-14

    1.Log4j Introduction Apache Log4j is an open source project that allows developers to output log information in any interval. Log4j components mainly composed of three categories: 1) Logger-responsible for the output log information and log informati

  • log4j.properties configuration instructions 2010-07-13

    1 log4j.rootCategory = INFO, stdout, R Sentence to the level of output for the INFO log messages to stdout and R the two destinations, stdout, and R, defined in the following code, which could be named. Level can be divided into OFF, FATAL, ERROR, WA

  • oracle stored procedure call java program 2011-02-28

    oracle stored procedure call java program Write java program to be called: import java.io.FileWriter; public class Procedure2JavaTest { public static void sendMsg (java.lang.String title, java.lang.String content) throws Exception { String str = titl

  • Java program to save the state and the setting Properties File 2009-01-20

    Java program to save the state and the setting Properties File Using the Properties file to save. Properties of the nature of the file is a text file, use the attributes and values to store data such as: abc.name = Colin. Use Porperites file to save

  • log4j configuration instructions (found a relatively long) 2008-07-21

    Log4j is an open-source Apache project, through the use of Log4j, we can control the destination of the log information transmission is the console, file, GUI components, and even socket servers, NT event logger, UNIX Syslog daemon, etc. ; we can als

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.760 (s). 12 q(s)