Log4j.properties
#指定某个包下的所以类调用指定的appender
Log4j.logger.com.coderdream.log4j=debug,appender1
Log4j.logger.com.coderdream.log4jdao=info,appender1,appender2
#输出到控制台
Log4j.appender.appender1=org.apache.log4j.consoleappender
#设置输出样式
Log4j.appender.appender1.layout=org.apache.log4j.patternlayout
#自定义样式
#%r Time 0
#%t Method Name Main
#%p Priority Debug/info/error
# The full name of the class to which the%c belongs (including the package name)
# where the%l takes place, in a row of a class
#%M The messages specified in the output code, such as the message in log
#%n output a newline symbol
LOG4J.APPENDER.APPENDER1.LAYOUT.CONVERSIONPATTERN=[%D{YY/MM/DD Hh:mm:ss:sss}][%c-%m]%m%n
#输出到文件 (default here is append)
Log4j.appender.appender2=org.apache.log4j.fileappender
#设置文件输出路径
# "1" text file
Log4j.appender.appender2.file=c:/w/t.log
#是否追加
#log4j. Appender.appender2.append=false
#设置文件输出样式
Log4j.appender.appender2.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.APPENDER2.LAYOUT.CONVERSIONPATTERN=[%D{HH:MM:SS:SSS}][%C-%M]-%m%n
Hellolog4j.java
package com.coderdream.log4j;
Import Org.apache.commons.logging.Log;
Import org.apache.commons.logging.LogFactory;
Import Org.apache.log4j.Logger;
Import Test. TestLog1;
public class Hellolog4j {
private static Log Logger=logfactory.getlog (hellolog4j.class);
/**
* @param args
*
/public static void main (string[] args) {
//System.out.println ("This is println m Essage. ");
if (logger.isdebugenabled ()) {
//log debug level information
logger.debug ("This is debug message.");
}
Record info level information
if (logger.isinfoenabled ()) {
Logger.info ("This is info.");
}
Record Error level information
if (logger.iserrorenabled ()) {
Logger.error ("This is error message.");}
}
Package Com.coderdream.log4jDao;
Import Org.apache.log4j.Logger;
public class Hellodao {
private static Logger Logger = Logger.getlogger (hellodao.class);
/**
* @param args
*
/public static void main (string[] args) {
//logging Debug level information
if ( Logger.isdebugenabled ()) {
Logger.debug ("This is the debug message from Dao. Ah big");
}
Record info level information
if (logger.isinfoenabled ()) {
Logger.info ("This is info messages from Dao. Ada");
}
Log information for the error level
Logger.error ("This is error message from Dao.");
}