log4j: Specify the output level of a logger individually

Source: Internet
Author: User
Tags documentation stringbuffer log4j
log4j: Specify the output level of a logger individually--

Generally for production systems, log levels are adjusted to info to avoid excessive output logs.
But at some point, you need to keep track of specific issues, so you have to turn on the debug log.
But if you open the Log4j.rootlogger, the information you need will be submerged in the log ocean.
At this point, you need to specify that a log level of one or some logger is debug, while the Rootlogger remains info unchanged.
The reference configuration is as follows (specifies the log output of the Com.bs2.test.MyTest class)
########################
# Specify the output level of a logger individually
#######################
Log4j.logger.com.bs2.test.mytest=debug

http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/log4j/log4j.html# Log4j-external-config-file

Figure 1. Logger Output Hierarchy

Http://www.matrix.org.cn/resource/article/1/1426.html

Log4j Use Example
--by Blues (zhaochaohua@sina.com)
Part 1 Introduction
The benefits of log4j are:
1. By modifying the configuration file, you can determine where the log information is exported (console, file,...), or output.
In this way, in the system development phase can print detailed log information to track the operation of the system, and the system can be closed after the log output, so that can track the operation of the system at the same time, but also reduce the garbage code (SYSTEM.OUT.PRINTLN (...) , etc.).
2. The use of log4j requires that the whole system have a unified log mechanism, which is beneficial to the planning of the system.

The use of log4j itself is simple. However, the rational planning of a unified log mechanism for a system needs thoughtful consideration.

Other information about log4j is in the log4j documentation.

Part II configuration file explained in detail
Look at an example of a configuration file first:
1. Examples of configuration files

Log4j.rootlogger=debug
#将DAO层log记录到DAOLog, in Alllog
Log4j.logger.dao=debug,a2,a4
#将逻辑层log记录到BusinessLog, in Alllog
Log4j.logger.businesslog=debug,a3,a4
#A1--Print to the screen
Log4j.appender.a1=org.apache.log4j.consoleappender
Log4j.appender.a1.layout=org.apache.log4j.patternlayout
log4j.appender.a1.layout.conversionpattern=%-5p [%t]%37c%3x-%m%n
#A2--Print to file Daolog--dedicated to DAO layer service
Log4j.appender.a2=org.apache.log4j.dailyrollingfileappender
Log4j.appender.a2.file=daolog
Log4j.appender.a2.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.a2.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.A2.LAYOUT.CONVERSIONPATTERN=[%-5P]%d{yyyy-mm-dd Hh:mm:ss,sss} method:%l%n%m%n

#A3--Print to file Businesslog--specifically record the logical processing layer service log information
Log4j.appender.a3=org.apache.log4j.dailyrollingfileappender
Log4j.appender.a3.file=businesslog
Log4j.appender.a3.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.a3.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.A3.LAYOUT.CONVERSIONPATTERN=[%-5P]%d{yyyy-mm-dd Hh:mm:ss,sss} method:%l%n%m%n

#A4--Print to file Alllog--Log all log information
Log4j.appender.a4=org.apache.log4j.dailyrollingfileappender
Log4j.appender.a4.file=alllog
Log4j.appender.a4.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.a4.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.A4.LAYOUT.CONVERSIONPATTERN=[%-5P]%d{yyyy-mm-dd Hh:mm:ss,sss} method:%l%n%m%n



Use of 2.Appender
A appender represents a place for log information to be written to. There are many types of appender that log4j can use, only 3 are considered here: Consoleappender,fileappender,dailyrollfileappender
2.1 Consoleappender
If you use Consoleappender, log information is written to the console. is to print the information directly to the System.out.
2.2 Fileappender
With Fileappender, log information is written to the specified file. This should be a more frequent use of the situation.
Accordingly, the file name of the log output should be specified in the configuration file. The following configuration specifies that the log file name is Demo.txt
Log4j.appender.a2.file=demo.txt
Note Replace the A2 with the Appender alias in the specific configuration.
2.3 Dailyrollingappender
Use Fileappender to output log information to a file, but it is inconvenient to read if the file is too large. Then you can use Dailyrollingappender. Dailyrollingappender can output log information to a file that is distinguished by date. The following configuration file will produce a log file every day, each log file records only log information of the day:

Log4j.appender.a2=org.apache.log4j.dailyrollingfileappender
Log4j.appender.a2.file=demo
Log4j.appender.a2.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.a2.layout=org.apache.log4j.patternlayout
log4j.appender.a2.layout.conversionpattern=%m%n



3.Layout of configuration
layout specifies the style for log information output.
For more information, see Patternlayout Javadoc.
Example 1: Display date and log information

Log4j.appender.a2.layout=org.apache.log4j.patternlayout
Log4j.appender.a2.layout.conversionpattern=%d{yyyy-mm-dd Hh:mm:ss,sss}%m%n

The printed information is:
2002-11-12 11:49:42,866 SELECT * from role WHERE 1=1 ORDER BY createdate Desc

Example 2: Display date, log occurrence place and log information

Log4j.appender.a2.layout=org.apache.log4j.patternlayout
Log4j.appender.a2.layout.conversionpattern=%d{yyyy-mm-dd hh:mm:ss,sss}%l "#"%m%n



Example 3: Display log level, time, call method, log information

Log4j.appender.a2.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.A2.LAYOUT.CONVERSIONPATTERN=[%-5P]%d{yyyy-mm-dd Hh:mm:ss,sss} method:%l%n%m%n
Log information:
[DEBUG] 2002-11-12 12:00:57,376 method:cn.net.unet.weboa.system.dao.RoleDAO.select (roledao.java:409)



Use of Part 3 log4j
There are 3 log4j steps to use:
3.1. Initialize log4j based on configuration file
The configuration file is described in Part 2. Now it's about how to configure log4j in your program.
Log4j can be initialized using the 3 configurator: Basicconfigurator,domconfigurator,propertyconfigurator
Here's the propertyconfigurator. The use of Propertyconfigurator applies to all systems.
such as the following statement
Propertyconfigurator.configure ("Log4j.properties");
The log4j environment is initialized with Log4j.properties for the configuration file.
Note that this statement only needs to be executed once when the system is started. For example: You can use this in unet Weboa projects:
Called once in the Actionservlet init () method.

public class Actionservlet extends httpservlet{
...
/**
* Initialize Global variables
*/
public void Init () throws Servletexception {
Initializing the Action Resource
try{
Initlog4j ();
...
}catch (IOException e) {
throw new Servletexception ("Load actionres is Error");
}
}
...
protected void initlog4j () {
Propertyconfigurator.configure ("Log4j.properties");
}
...
}//end class Actionservlet


3.2 Get logger instances where you need to use log4j
The following are examples of usage in the Roledao class:
static Logger log = Logger.getlogger ("DAO");
Note that the "DAO" identifier is used here, and the corresponding configuration information in the configuration file is as follows:

#定义DAO Logger
Log4j.logger.dao=debug,a2
Properties for #设置Appender A2

Log4j.appender.a2=org.apache.log4j.dailyrollingfileappender
Log4j.appender.a2.file=demo
Log4j.appender.a2.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.a2.layout=org.apache.log4j.patternlayout
log4j.appender.a2.layout.conversionpattern=%-5p%d{yyyy-mm-dd HH:mm:ss}%l%n%m%n

public class Roledao extends Basedbobject
{
...
static Logger log = Logger.getlogger ("DAO");
...
Public Beancollection SelectAll () throws SQLException
{
StringBuffer sql = new StringBuffer (Sqlbuf_len);
Sql.append ("SELECT * from" + tablename + "ORDER by Roldid");
System.out.println (Sql.tostring ());
Log.debug (SQL);
...
}
...
}


3.3 Use the debug,info,fatal of the Logger object ... Method
Log.debug ("It is the Debug info");

A bug in Attachment 1:log4j
When used in this way, Dailyrollingfileappender is not used correctly:

Public Class Roledao () {

static Logger log = Logger.getlogger ("DAO");

Perform a Configure () operation every time the new Roledao object
Public Roledao (TransactionManager transmgr) throws SQLException
{
...
Propertyconfigurator.configure ("Log4j.properties");
...
}

public void Select () {
...
Log logs using log4j
Log.debug ("...");
...
}
}

How to Solve:
Perform a propertyconfigurator.configure ("Log4j.properties") at system startup;
After that, it is no longer executed.

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.