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")

}
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of log4j and log4j.properties configuration instructions (Collectors Edition)

  • A classic english Rails AJAX sorting and paging sample

    This is a classic (in my opinion ^ _ ^) is used to display, ajax call to sort, search and paging example. This sample is characterized by the process of writing put considerable wordily, designed to kill Mother. So very good understanding, very primary, v

  • [Portal] with Ruby call ICE interface

    Ice is not introduced, http://www.zeroc.com/ Ruby is not introduced. I try ah, the steps I will be clearly recorded, One to definitely not let the people Society, Second, I am afraid I have forgotten the past two days, brain hypoxia, only three days to re

  • Java Design Pattern Part of the abstract factory pattern

    Abstract factory pattern is an abstract factory pattern than an even higher degree of model. In short, the abstract factory class and the factory class of the same principle, only factory class is back in the general category and examples; and return the

  • Getting Started Programming spring Ten Questions

    1, how to study Spring? You can study through the following means spring: (1) spring package download doc directory under MVC-step-by-step and the sample directory are relatively good examples of spring development examples. (2) AppFuse integrates a numbe

  • jdbc even ORACLE, SQLServer2000, mysql ways

    According to other people say can make a ride to one of the clear step by step, not as easy to write your own ideas! 1. Even the oracle The direct use of the oracle are provided give a jar package jdbc: oracle alone is installed, and 10g of oracle on ...

  • Software engineer must know the concept of the 10

    35 kilometers wrote "excellent software engineers make the best use of design patterns and diligent code Reconstruction, the preparation of unit testing, and has easy religious pursuit. In addition to these, excellent software engineers but also prof

  • log4j easy application in java

    JAVA development, frequently used the log output, in a so-called most of the software company will have its own set of configuration style, re-read the configuration file to initialize property of the log, it will be good, but sometimes may not need to fu

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries