log4j-Log configuration and path issues

Source: Internet
Author: User
Tags save file log4j

configuration process: imported jar Packages:

Slf4j-api-1.7.6.jar

Slf4j-log4j12-1.7.6.jar log4j.properties

log4j.rootlogger= debug,stdout,d1 log4j.rootlogger=info,stdout,e log4j.appender.stdout=
Org.apache.log4j.ConsoleAppender Log4j.appender.stdout.target=system.out Log4j.appender.stdout.threshold=info Log4j.appender.stdout.layout=org.apache.log4j.patternlayout Log4j.appender.stdout.layout.ConversionPattern =@ 
Author Lyx:-%d{yyyy-mm-dd hh:mm:ss,sss} [%p]-[%c]-%m%n log4j.appender.D1 = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.D1.File = ${springlog.root}/web-inf/logs/error.log Log4j.appender.D1.Append = True
Log4j.appender.D1.Threshold = ERROR Log4j.appender.D1.layout = org.apache.log4j.PatternLayout Log4j.appender.D1.layout.ConversionPattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n LOG4J.APPENDER.E = org.a 
Pache.log4j.DailyRollingFileAppender Log4j.appender.e.file = ${springlog.root}/web-inf/logs/error1.log Log4j.appender.e.append = True Log4j.appender.e.threshold = INFO Log4j.appender.e.layout = Org.apache.log4j.PatternLayout Log4j.appender.e.layout.convErsionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n 
Web.xml
<!--log4j Log configuration-->
	<!--Path variable value (is the path of the server)-->
	<context-param>
		<param-name> webapprootkey</param-name>
		<param-value>springLog.root</param-value>
	</ Context-param>
	<!--log4j configuration file-->
	 <context-param>
		<param-name> Log4jconfiglocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param> 
   
 	 <context-param>
	<!-- 	The refresh interval of the log page-->
		<param-name >log4jRefreshInterval</param-name>
		<param-value>6000</param-value>
	</ Context-param>
	<!--set up log monitor-->
	<listener>
		<listener-class> Org.springframework.web.util.log4jconfiglistener</listener-class>
	</listener>

Java code:
Package Com.controller;
Import java.util.List;

Import Java.util.Map;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.ResponseBody;

Import Com.service.UserService; /** * * * * * @author lyx * * * * * * * * * * *com.controller.usercontroller * TODO/@Controller @RequestMapping ("/
	User ") public class Usercontroller {//private static final Logger log =logger.getlogger (Usercontroller.class);
	private static final Org.slf4j.Logger Logger =org.slf4j.loggerfactory.getlogger (usercontroller.class); 
	
	@Autowired Private UserService Service; The @RequestMapping ("/queryallinfo") @ResponseBody public void Queryallinfo (HttpServletRequest request), HttpServletResponse response) {//Current method name String Currentmethod=thread. CurrentThread (). Getstacktrace () [1].getmethodname ();
		Logger.info ("CurrentThread call Currentmethod is:" +currentmethod);
		Logger.debug ("Debug");	
	Logger.error ("error");
 }
	
}


Content ExplanationOutput mode Appender generally have 5 kinds:
Org.apache.log4j.RollingFileAppender (scrolling file, automatic recording of the latest log)
Org.apache.log4j.ConsoleAppender (console)
Org.apache.log4j.FileAppender (file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)
(2). The priority of the journaling record is priority, and the priority is divided into the high to the low
Off, FATAL, ERROR, WARN, INFO, DEBUG, all.
LOG4J recommends using only the five levels of fatal, ERROR, WARN, INFO, and Debug.
(3). The parameters in the format description layout begin with a%, followed by different parameters representing different formatting information (the parameters are listed alphabetically):
The full name of the class to which the%c output belongs, can be modified to%d{num}, the dimension of the Num class name output (such as: "Org.apache.elathen.ClassName",%c{2} will output elathen. ClassName)
%d output log time in the format of%d{yyyy-mm-dd hh:mm:ss,sss}, you can specify a format such as%d{hh:mm:ss}
%l output Log event occurrence location, including class name, occurrence thread, number of lines in code
%n line break
%m output code to specify information, such as info ("message"), output message
%p output priority, that is, FATAL, ERROR, etc.
%r output the number of milliseconds it takes to display the log information from boot to
%t output The name of the thread that generated the log event
Detailed website: How to use relative paths in log4j.properties files: http://blog.csdn.net/wang12/article/details/6395602
configuration and use of log4j in spring MVC: http://rockelixir.iteye.com/blog/1902352

Configure log4j (verbose): http://blog.csdn.net/azheng270/article/details/2173430 Note: problem with log save path

when my log save file path is written in this way Log4j.appender.D1.File =/logs/error111.log

may be in the following several places:

1. You can pass System.out.println (System.getproperty ("User.dir")); To view the path of the output, you can find the text of the/logs/error111.log in this path. For example, my position is C:\tomcat7.059\bin. (just started looking at several. log files, thought it was here, and then deleted the rerun did not generate. Log logs here)

2.C Packing directory. The programs I run later are all in the C-packing directory.

so save the log path as complete as possible.


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.