Specify the location of the log4j configuration file

Source: Internet
Author: User
Tags custom name log4j

log4j configuration file is not initialized under Classpath

Specify the location of the log4j.properties configuration file, while dynamically setting the output location of the log:

private static Logger Logger = null;
static{
		//Set the environment variable property Workdir as the installation directory, so workdir can get the value of this environment variable in log4j.properties file
		system.setproperty (" Workdir "," xxxxxx ");
		Specifies the location of the log4j.properties configuration file
		propertyconfigurator.configure ("Where the Log4j.properties file is located");
		Logger = Logger.getlogger (Logtest.class);
	}

Then in the Log4j.properties file, you can use Workdir to specify the location of the output:

Log4j.appender.appender2.file=${workdir}/log/logtest.log

Can. This specifies that the log output is to the target location

The default configuration file for log4j is log4j.properties, but if this profile is placed elsewhere in the project for ease of administration, or if the default file name of the profile is modified, then a way to specify the log4j configuration file can be used.

Specific as follows:

If the file management of my project is the following diagram, that is, after compiling, although it is under Classpath, however, Log4j's configuration file is not in the default location.

If this is a Web project, you can initialize the log4j with the following code snippet:

public void Init () throws Servletexception {
String path = Getservletcontext (). Getrealpath ("/");
String file = Getinitparameter ("log4j"). Replace (File.separatorchar, '/');
if (file! = null) {
Propertyconfigurator.configure (path + file);
}
}

Of course, this is only based on the servlet, if it is just a normal class, then the initialization is very similar, nothing more than to get the location of the log4j configuration file, and then

Propertyconfigurator.configure (Log4jconfigerfilepath);

This way, log4j will be able to find the location of the configuration file.

log4j output Multiple custom log files, dynamic configuration path

1. log4j output multiple custom log files

The power of log4j is undeniable, but the actual application inevitably encountered a function need to output a separate log file, how to separate the required content from the original log, forming a separate log file. In fact, as long as the existing log4j based on a little configuration can easily achieve this function.

First look at a common log4j.properties file, which is logging in the console and the Myweb.log file:

Log4j.rootlogger=debug, stdout, logfile

Log4j.category.org.springframework=error

Log4j.category.org.apache=info

Log4j.appender.stdout=org.apache.log4j.consoleappender

Log4j.appender.stdout.layout=org.apache.log4j.patternlayout

log4j.appender.stdout.layout.conversionpattern=%d%p [%c]-%m%n

Log4j.appender.logfile=org.apache.log4j.rollingfileappender

Log4j.appender.logfile.file=${myweb.root}/web-inf/log/myweb.log

log4j.appender.logfile.maxfilesize=512kb

Log4j.appender.logfile.maxbackupindex=5

Log4j.appender.logfile.layout=org.apache.log4j.patternlayout

log4j.appender.logfile.layout.conversionpattern=%d%p [%c]-%m%n

  

If you want to output different files for different classes (take cn.com.Test as an example), first define them in Test.java:

private static Log logger = Logfactory.getlog (Test.class);

Then add in the Log4j.properties:

Log4j.logger.cn.com.test= DEBUG, Test

Log4j.appender.test=org.apache.log4j.fileappender

log4j.appender.test.file=${myweb.root}/web-inf/log/Test.log

Log4j.appender.test.layout=org.apache.log4j.patternlayout

log4j.appender.test.layout.conversionpattern=%d%p [%c]-%m%n

That is, let the logger in cn.com.Test use the configuration made by Log4j.appender.test.

However, if you need to output multiple log files in the same class. In fact, the truth is the same, first defined in Test.java:

private static Log Logger1 = Logfactory.getlog ("MyTest1");

private static Log Logger2 = Logfactory.getlog ("MyTest2");

Then add in the Log4j.properties:

log4j.logger.mytest1= DEBUG, Test1

Log4j.appender.test1=org.apache.log4j.fileappender

log4j.appender.test1.file=${myweb.root}/web-inf/log/Test1.log

Log4j.appender.test1.layout=org.apache.log4j.patternlayout

log4j.appender.test1.layout.conversionpattern=%d%p [%c]-%m%n

  

Log4j.logger.mytest2= DEBUG, Test2

Log4j.appender.test2=org.apache.log4j.fileappender

log4j.appender.test2.file=${myweb.root}/web-inf/log/Test2.log

Log4j.appender.test2.layout=org.apache.log4j.patternlayout

log4j.appender.test2.layout.conversionpattern=%d%p [%c]-%m%n

That is, when using logger, give it a custom name (such as "MyTest1"), and then make the appropriate configuration in log4j.properties. Don't forget to use different logger for different logs (such as output to Test1.log with logger1. info ("abc")).

Another problem is that these custom logs are exported to the logs configured by Log4j.rootlogger at the same time, so how can they only be output to their own specified log? Don't worry, there's a switch here:

Log4j. additivity. MyTest1 = False

It is used to set whether output to the log configured by Log4j.rootlogger and set to false will not be exported elsewhere. Note that the "MyTest1" here is the custom name you gave logger in your program.

If you say, I just do not want to output this log at the same time Log4j.rootlogger configured logfile, stdout I also want to output at the same time. That's good, put your log4j.logger.myTest1 = DEBUG, test1 changed to the next type on OK.

Log4j.logger.mytest1=debug, Test1, stdout

2. Dynamic Configuration Path

If the log path required by the program needs constant change, and it is not possible to change the configuration file every time, then take two methods.

First Kind

LOG4J's configuration file supports Windows environment variables, formatted like velocity:${env}, then we use environment variables to represent paths that may change. "log4j.appender.test1.file=${myweb.root}/web-inf/log/test1.log" has been used above.

The second Kind

This method is not a configuration file, but in the program with code configuration, code is live, so the path must be able to write live. Examples are as follows:

[Java] view plain copy Logger myTest = Logger.getlogger ("MyTest");      Layout layout = new Patternlayout ("%d%p [%c]-%m%n");      Appender Appender = new Fileappender (layout, logfilepath); Mytest.addappender (Appender);

Attached: The format meaning of the Conversionpattern parameter

The full name of the class to which the%c output log information belongs
%d the date or time of the output log time, the default format is ISO8601, or the format can be specified later, for example:%d{yyy-mm-dd HH:mm:ss}, output similar to: 2002-10-18-22:10:28
%f the class name of the class to which the output log information belongs
%l the occurrence of the output log event where the statement that outputs the log information is in the first line of the class it is in
%m the information specified in the output code, such as a message in log (message)
%n output a carriage return line break, Windows platform is "/r/n", UNIX platform is "/n"
%p output priority, or debug,info,warn,error,fatal. Debug if the debug () output is called, and so on
%r the number of milliseconds that the output from the application starts to output this log information
%t output The name of the thread that generated the log event

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.