[英]Change location of log4j.properties
I want to put all my config files in a /config
subfolder of my application directory. 我想将所有配置文件放在应用程序目录的
/config
子文件夹中。 Log4j is expecting the log4j.properties file in the root folder of my application. Log4j期望在应用程序的根文件夹中找到log4j.properties文件。 Is there a way to tell log4j where to look for the properties file?
有没有办法告诉log4j在哪里寻找属性文件?
Yes, define log4j.configuration
property 是的,定义
log4j.configuration
属性
java -Dlog4j.configuration=file:/path/to/log4j.properties myApp
Note, that property value must be a URL. 请注意,该属性值必须是URL。
For more read section 'Default Initialization Procedure' in Log4j manual . 有关更多信息,请阅读Log4j手册中的 “默认初始化过程”部分。
You can use PropertyConfigurator
to load your log4j.properties wherever it is located in the disk. 您可以使用
PropertyConfigurator
将log4j.properties加载到磁盘中的任何位置。
Example: 例:
Logger logger = Logger.getLogger(this.getClass());
String log4JPropertyFile = "C:/this/is/my/config/path/log4j.properties";
Properties p = new Properties();
try {
p.load(new FileInputStream(log4JPropertyFile));
PropertyConfigurator.configure(p);
logger.info("Wow! I'm configured!");
} catch (IOException e) {
//DAMN! I'm not....
}
If you have an XML Log4J configuration, use DOMConfigurator instead. 如果您具有XML Log4J配置,请改用DOMConfigurator 。
使用PropertyConfigurator: PropertyConfigurator.configure(configFileUrl);
Refer to this example taken from - http://www.dzone.com/tutorials/java/log4j/sample-log4j-properties-file-configuration-1.html 请参考以下示例,该示例取自-http: //www.dzone.com/tutorials/java/log4j/sample-log4j-properties-file-configuration-1.html
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class HelloWorld {
static final Logger logger = Logger.getLogger(HelloWorld.class);
static final String path = "src/resources/log4j.properties";
public static void main(String[] args) {
PropertyConfigurator.configure(path);
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
}
}
To change the logger levels - Logger.getRootLogger().setLevel(Level.INFO);
更改记录器级别
Logger.getRootLogger().setLevel(Level.INFO);
在Eclipse中,您可以将VM参数设置为:
-Dlog4j.configuration=file:///${workspace_loc:/MyProject/log4j-full-debug.properties}
This is my class : Path is fine and properties is loaded. 这是我的课程:路径很好,并且已加载属性。
package com.fiserv.dl.idp.logging;
import java.io.File;
import java.io.FileInputStream;
import java.util.MissingResourceException;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class LoggingCapsule {
private static Logger logger = Logger.getLogger(LoggingCapsule.class);
public static void info(String message) {
try {
String configDir = System.getProperty("config.path");
if (configDir == null) {
throw new MissingResourceException("System property: config.path not set", "", "");
}
Properties properties = new Properties();
properties.load(new FileInputStream(configDir + File.separator + "log4j" + ".properties"));
PropertyConfigurator.configure(properties);
} catch (Exception e) {
e.printStackTrace();
}
logger.info(message);
}
public static void error(String message){
System.out.println(message);
}
}
You must use log4j.configuration
property like this: 您必须像这样使用
log4j.configuration
属性:
java -Dlog4j.configuration=file:/path/to/log4j.properties myApp
If the file is under the class-path (inside ./src/main/resources/ folder), you can omit the file://
protocol: 如果文件在类路径下(位于./src/main/resources/文件夹中),则可以省略
file://
协议:
java -Dlog4j.configuration=path/to/log4j.properties myApp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.