Summary of methods used to print logs using log4j and previous old print log methods

Source: Internet
Author: User
Tags log log log4j

1 According to reason the Commons-logging component has a method of printing logs, or a logging with Java, such as the following:

Importing logging jar packages is a must, and here are two ways to print the log

Package com.commons.logging;


Import Java.util.logging.Logger;

Import Org.apache.commons.logging.Log;
Import org.apache.commons.logging.LogFactory;

The public class Test {//Two print method results are similar to
	private static log log = Logfactory.getlog (Test.class); This is the log
	static Logger Logger = Logger.getlogger (Test.class.getName ()) in the Commons-logging component, or the Java self-contained log
	public void log ()
	{
		log.debug ("Debug Info.");
		Log.info ("info info");
		Log.warn ("Warn info");
		Log.error ("error info");
		Log.fatal ("Fatal info");
		System.out.println ();
		Logger.info ("Hello");
	}
	public static void Main (string[] args) {
		Test t = new Test ();
		T.log ();
	}

2 But the effect is not good, the General people do not use, the following print log

July 24, 2013 10:24:42 pm com.commons.logging.Test Log
info: Info info

July 24, 2013 10:24:42 pm com.commons.logging.Tes T log
Warning: Warn info
July 24, 2013 10:24:42 pm com.commons.logging.Test Log
serious: Error info
July 24, 2013 10:24: 42 pm Com.commons.logging.Test Log
serious: Fatal info
July 24, 2013 10:24:42 pm com.commons.logging.Test log
information: Hel Lo

3 these days are popular log4j, below look at the use of the log4j process:

1) Import jar Package log4j

2) Add log4j.properties under SRC

  Log4j.rootlogger=debug, stdout, R
  log4j.appender.stdout=org.apache.log4j.consoleappender
  Log4j.appender.stdout.layout=org.apache.log4j.patternlayout

  Log4j.appender.stdout.layout.ConversionPattern =%5p [%t] (%f:%l)-%m%n

  log4j.appender.r=org.apache.log4j.rollingfileappender
  log4j.appender.r.file= GridQualitSupervision.log
  log4j.appender.r.maxfilesize= 300KB

  log4j.appender.r.maxbackupindex=1

  Log4j.appender.r.layout=org.apache.log4j.patternlayout
  log4j.appender.r.layout.conversionpattern=%p%t%c-%m %n   
3) Simple test class

Package Com.wanju.common;

Import Org.apache.log4j.Logger;


public class Loggerutil {public

	static Logger Logger = Logger.getlogger (LoggerUtil.class.getName ());
	
	public static void Main (string[] args) {
		logger.info ("OK");
	}

4) View Test results

INFO [main] (loggerutil.java:11)-OK
It's so simple, it's so cool


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.