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
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.