2

log4j动态添加appender

 2 years ago
source link: https://lichuanyang.top/posts/42764/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

log4j动态添加appender

发表于

2017-06-30 更新于 2021-09-23

阅读次数: 179 Valine: 0

除了通过properties,xml等格式的配置文件对log4j进行配置外,log4j还提供了各种接口,可以用代码动态修改log4j的配置,例如给一个logger增加一个appender。方法很简单,就是新建一个appder,然后添加到logger上,示例代码如下:

KafkaLog4jAppender kafkaAppender = new KafkaLog4jAppender();
kafkaAppender.setBrokerList(broker);
kafkaAppender.setTopic(topic);
kafkaAppender.setCompressionType("gzip");
kafkaAppender.setSyncSend(false);
kafkaAppender.setLayout(new PatternLayout(layout));
kafkaAppender.activateOptions();
logger.addAppender(kafkaAppender);
logger.setLevel(Level.INFO);

这里以一个kafkaappender做例子,其他的,例如DailyRollingFileAppender等,都是类似的。

欢迎关注我的其它发布渠道


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK