Log4j the Appender of all classes under the package

Source: Internet
Author: User
Tags time 0 log4j

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.");
	}


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.