`
weigang.gao
  • 浏览: 500619 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

log4j 笔记之PropertyConfigurator

 
阅读更多

org.apache.log4j.PropertyConfigurator

Allows the configuration of log4j from an external file.

允许使用外部文件来配置log4j

 

See doConfigure(String, LoggerRepository) for the expected format.

看 doConfigure(String, LoggerRepository)了解期望的格式

 

It is sometimes useful to see how log4j is reading configuration files. You can enable log4j internal logging by defining the log4j.debug variable.

查看log4j是如何读取配置文件有时候是有帮助,你能够通过定义log4j.debug变量启动log4j内部日志

 

As of log4j version 0.8.5, at class initialization time class, the file log4j.properties will be searched from the search path used to load classes.

从log4j 0.8.5版本起, 在类初始化的时候, log4j.properties文件将从用于加载类的路径中被查找

 

If the file can be found, then it will be fed to the configure(java.net.URL) method.

如果 log4j.properties文件被发现,那将调用configure()方法(来初始化log4j)

 

The PropertyConfigurator does not handle the advanced configuration features supported by the DOMConfigurator such as support for Filters,custom ErrorHandlers, nested appenders such as the AsyncAppender, etc.

PropertyConfigurator 不支持高级配置特征

 

All option values admit variable substitution.The syntax of variable substitution is similar to that of Unix shells. The string between an opening "${" and closing "}" is interpreted as a key. The value of the substituted variable can be defined as a system property or in the configuration file itself. The value of the key is first searched in the system properties, and if not found there, it is then searched in the configuration file being parsed. The corresponding value replaces the ${variableName} sequence. For example, if java.home system property is set to /home/xyz, then every occurrence of the sequence ${java.home} will be interpreted as /home/xyz.

所有可选值允许用变量代替,变量代替的语法与Unix shells是类似的。放在以“${”开头并且以“}”结尾的字符串被解释成key。替换的变量(这里可以理解成key)的值(value)被定义成系统属性或者在系统文件中。key的值首先在系统属性中查找,如果没有找到,它会在被解析的配置文件中查找。然后使用对应的值来替换${variableName}。例如,如果使用java.lang包中的System类的静态方法setProperty设置

System.setProperty("java.home", "/home/xyz"),那么每个${java.home}出现的地方都将被解释成/home/xyz

 

eg1:变量代替

java代码:

import org.apache.log4j.Logger;
public class HelloWorld {
       	
//	private static Logger logger = Logger.getLogger(HelloWorld.class.getName()); 
	public static void main(String[] args) {
                System.setProperty("category", "DEBUG, stdout, logFile");//这条语句需要放到Logger初始化之前	
                /*当Logger初始化时,将查找log4j.properties文件,用来初始化Logger*/
		Logger logger = Logger.getLogger(HelloWorld.class.getName()); 

		logger.info("main method start...........");
		System.out.println("Hello, World!");
		logger.info("main method end...............");

		
	}

}

log4j.properties文件配置

#替换变量
rootLogger=DEBUG, stdout
#变量rootLogger首先会在系统属性中查找,如果没有找到,那么就会到被解析的配置文件中查找。
#使用替换变量
log4j.rootLogger=${rootLogger}
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} || %l || %m%n 

 

eg2 :第二种方式设置替换变量

import org.apache.log4j.Logger;

public class HelloWorld {
	
	public static void main(String[] args) {
                //设置系统属性,替换变量
		System.setProperty("catetory", "info, stdout");//这条语句需要放到Logger初始化之前	
		Logger logger = Logger.getLogger("com.hsp.gao"); 

		logger.info("main method start...........");
		System.out.println("Hello, World!");
		logger.info("main method end...............");

		
	}

}

 log4j.properties

#替换变量
rootLogger=DEBUG, stdout, logFile
log4j.rootLogger = ${rootLogger}
#会继承rootLogger中stdout, logFile
log4j.category.com.hsp.gao = ${catetory}
#表示Logger不会在父Logger的appender里输出,默认为true。 
log4j.additivity.com.hsp.gao = false
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} || %l || %m%n 

log4j.appender.logFile=org.apache.log4j.FileAppender
log4j.appender.logFile.Threshold=WARN
log4j.appender.logFile.ImmediateFlush=true
log4j.appender.logFile.Append=false 
log4j.appender.logFile.File=D:/log/AndStudy.txt
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n 

 

2.log4j.Category

 

 

3.log4j.Logger

 

分享到:
评论

相关推荐

    log4j使用笔记

    #### 四、log4j 配置详解 1. **配置文件**: - `log4j.properties` 或 `log4j.xml` 文件用于定义 log4j 的配置规则。 - 配置文件通常放置在项目的根目录或者类路径下。 - 如果未指定配置文件的位置,则 log4j 会...

    学习Log4j笔记,TXT格式

    Log4j的强大之处在于它提供了丰富的配置选项,使得开发者可以根据不同的需求调整日志行为。 #### 二、Log4j配置文件详解 在上述提供的内容中,主要涉及到了两种不同类型的项目:Java项目和Web项目,并且给出了具体...

    log4j的笔记

    PropertyConfigurator.configure("D:/resin/webapps/log4j/web-inf/classes/log4j.properties"); System.out.println("ok"); } } ``` `log4j.properties` 文件中包含了具体的配置信息,例如定义日志级别、指定 ...

    log4j学习

    5. **自定义Servlet初始化Log4j**:创建一个自定义的Servlet,如`Log4jInit`,在`init()`方法中使用`PropertyConfigurator.configure()`来加载`log4j.properties`配置。 #### 总结 通过以上步骤,我们可以在Java...

    Apache_Log4j_学习笔记

    ### Apache Log4j 学习笔记精要 #### 一、Apache Log4j 概览 **Apache Log4j** 是一款开源的日志框架,由Apache软件基金会所提供,旨在简化Java应用程序中的日志记录过程。它允许开发者在代码中加入日志记录功能,...

Global site tag (gtag.js) - Google Analytics