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.ne

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
log4j2是Java中一个流行的日志框架,可以帮助我们方便地记录应用程序的日志。 PropertyConfigurator是log4j2中的一个类,它可以通过读取属性文件来配置log4j2的日志记录器。在配置文件中,我们可以指定日志的输出目标、级别、格式等。 下面是一个示例配置文件: ``` log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %c{1}:%L - %m%n ``` 这个配置文件定义了一个名为“stdout”的日志输出目标,它将日志输出到控制台。日志的级别为INFO,表示只记录INFO级别及以上的日志信息。日志的格式由PatternLayout类定义,可以根据需要进行修改。 在Java代码中,我们可以使用以下代码来加载这个配置文件: ```java import org.apache.log4j.PropertyConfigurator; import java.util.Properties; public class TestLog4j2 { public static void main(String[] args) { Properties props = new Properties(); props.load(TestLog4j2.class.getResourceAsStream("/log4j.properties")); PropertyConfigurator.configure(props); Logger logger = LogManager.getLogger(TestLog4j2.class); logger.info("Hello, log4j2!"); } } ``` 在这个代码中,我们首先读取配置文件,并将其传递给PropertyConfigurator来配置log4j2。然后,我们使用LogManager获取一个Logger对象,并使用它来记录一条日志。由于日志级别为INFO,因此这条日志将被输出到控制台。 需要注意的是,log4j2框架的使用可能会因为版本差异而存在一些细微的差别,因此在实际使用中,需要参考相应的文档来进行配置和使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值