LOG4J generates a separate log file for a separate class

Source: Internet
Author: User
Tags log4j

Recent company projects need to go to the network to download pictures, according to different circumstances to save the log to a different file. Specifically, the ordinary log information output to a separate file, the picture download failed to save the log separately to another file for later backtracking.


log4j configuration file

Log4j.rootlogger=info, stdout #Root Logger Log4j.appender.stdout=org.apache.log4j.consoleappender Log4j.appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=[%p]
%-d{yyyy-mm-dd HH:mm:ss} [%c%m]%m%n #============================================================logger S Log4j.logger.commonlog=info, stdout, S Log4j.additivity.CommonLog = False log4j.appender.s=
Org.apache.log4j.RollingFileAppender Log4j.appender.s.file=d:/fbb/myworkspace_dw/common_ganji.log Log4j.appender.s.layout=org.apache.log4j.patternlayout log4j.appender.s.layout.conversionpattern=[%p]%-d{
Yyyy-mm-dd HH:mm:ss} [%c%m]%m%n #============================================================logger B Log4j.logger.errorlog=error, stdout, B Log4j.additivity.ErrorLog = False log4j.appender.b=
Org.apache.log4j.RollingFileAppender Log4j.appender.b.file=d:/fbb/myworkspace_dw/failed_ganji.log Log4j.appender.b.layout=org.apache.log4j.patternlayout Log4j.appender.b.layout.conversionpatteRn=[%p]%-d{yyyy-mm-dd Hh\:mm\:ss} [%c%m]%m%n 


Using the example in your application

Import Org.apache.log4j.Logger;

public class Logutil {
	private static Logger Clogger = Logger.getlogger ("Commonlog");
	private static Logger Elogger = Logger.getlogger ("ErrorLog");
	
	public static void Downerror (Class clz,string message) {
		elogger.error (message+ "[" +clz.getname () + "]");
	}
	
	public static void info (Class clz,string message) {
		clogger.info (message+ "[" +clz.getname () + "]");
	}
	
	public static void Debug (Class clz,string message) {
		clogger.debug (message+ "[" +clz.getname () + "]");
	}
	
	public static void Warn (Class clz,string message) {
		Clogger.warn (message+ "[" +clz.getname () + "]");
	}
	
	public static void error (Class clz,string message) {
		clogger.error (message+ "[" +clz.getname () + "]");
	}


Here's the Commonlog, Errorlog is configured in the log4j configuration file.



Mark it, shout it.




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.