Several ways to configure log files in log4j

Source: Internet
Author: User
Tags log4j

In the configuration log path, encountered a small problem, that is, the configuration path can have several, I try to share with you.

1. Absolute path mode

Use absolute path that doesn't have to say anything. Direct output to configuration path

Log4j.appender.infofile.File = D:/logs/info.log

2. Use system to set log root path

Log4j.appender.logfile.file=${workdir}/logs/info.log

where "${workdir}/" is a variable that is replaced by the value "Workdir" in the system property. In this way, we can set the root path with System.setproperty before log4j load the configuration file.

3, can use the server environment variable log4j configuration file to support the server's VM environment variables, format similar ${catalina.home} ${catalina.base}

Log4j.appender.logfile.file=${catalina.base}logs/info.log

If the Tomcat middleware container is used, the log is stored in the Tomcat log

4. Implementing a relative path by loading the file property through the servlet initialization init () method

: To be a servlet, when the system is loaded, read the properties file into a properties file. That file's property value (I'm using a relative directory) to get rid of (plus the system's root directory), Let's set the properties object to Propertyconfig so that the log settings are initialized. You don't have to configure it in later use. Typically, during our development project, the log4j log output path is pinned to a folder so that if I change an environment, Log path needs to be modified again, more inconvenient, at present, I used dynamic change log path method to implement relative path save log file (1). When the project starts, mount the initialization class:

public class Log4jinit extends HttpServlet {
static Logger Logger = Logger.getlogger (Log4jinit.class);
Public Log4jinit () {
}

public void init (ServletConfig config) throws servletexception {
String prefix = Config.getservletcontext (). Getrealpath ("/");
String file = Config.getinitparameter ("log4j");
String filePath = prefix + file;
Properties Props = new properties ();
try {
FileInputStream istream = new FileInputStream (FilePath);
Props.load (IStream);
Istream.close ();
Toprint (Props.getproperty ("Log4j.appender.file.File"));
String logFile = prefix + props.getproperty ("Log4j.appender.file.File");/set Path
Props.setproperty ("Log4j.appender.file.File", logFile);
Propertyconfigurator.configure (props)//Mount log4j configuration information
catch (IOException e) {
Toprint ("Could not read configuration file [" + FilePath + "].");
Toprint ("Ignoring configuration file [" + FilePath + "].");
Return
}
}

public static void Toprint (String content) {
SYSTEM.OUT.PRINTLN (content);
}
}

5, using the log configuration method provided by spring

Web.xml Add the following code:

XML code

<context-param>

<param-name>webAppRootKey</param-name>

<param-value>project</param-value>

</context-param>

<context-param>

<param-name>log4jConfigLocation</param-name>

<param-value>WEB-INF/classes/log4j.properties</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

</listener> [XML]

	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>project </param-value>
	</context-param>
	<context-param>
		<param-name> Log4jconfiglocation</param-name>
		<param-value>web-inf/classes/log4j.properties</param-value >
	</context-param>
	<listener>
		<listener-class> Org.springframework.web.util.log4jconfiglistener</listener-class>
	</listener>

Webapprootkey: Configures the alias for the project, which is configured with the alias project. This can be ignored if you are deploying a project that is not in Tomcat, because Tomcat does not have a different Webapproot attribute configured for each application, so if two or more of the same application attribute names are present, the error occurs.

Then modify the configuration of the log4j.properties to change the path configuration of the log file output to:

Log4j.appender.loginfo.file=${project}web-inf/logs/prinfo.log

Loginfo is the name of my appender. ${project} is an absolute path to obtain an application using an application alias in the Web.xml above.

Note: If you do not need to configure the application alias, the Webapprootkey is not configured. You can write this directly:

Log4j.appender.loginfo.file=${webapp.root}web-inf/logs/info.log

Webapp.root is the default property. If there is a configuration webapprootkey will be covered.

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.