1 Star 1 Fork 3

zyc代码/log4j2-customPatternLayout

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CustomRegexReplaces.java 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
package org.xx.log.plugin;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.pattern.RegexReplacement;
import org.apache.logging.log4j.status.StatusLogger;
/**
* 自定义标签replaces, 用于多个正则表达式替换
*
* @author zhangyanchun
* @date 2017-07-27
*/
@Plugin(name = "replaces", category = "Core", printObject = true)
public final class CustomRegexReplaces {
private static final Logger LOGGER = StatusLogger.getLogger();
// replace标签,复用log4j已有plugin, replaces 下可以0,1,多个replace
private final RegexReplacement[] replaces;
private CustomRegexReplaces(RegexReplacement[] replaces) {
this.replaces = replaces;
}
/**
* 格式化输出日志信息, 此方法会执行多个正则表达式匹配与替换
*
* @param msg
* @return
*/
public String format(String msg) {
for (RegexReplacement replace : replaces) {
msg = replace.format(msg);
}
return msg;
}
/**
* 实现pluginFactory, 用于生成pugin
*
* @param replaces
* @return
*/
@PluginFactory
public static CustomRegexReplaces createRegexReplacement(
@PluginElement("replaces") final RegexReplacement[] replaces) {
if (replaces == null) {
LOGGER.info("no replaces is defined");
return null;
}
if (replaces.length == 1) {
LOGGER.warn("have the replaces , but no replace is set");
return null;
}
return new CustomRegexReplaces(replaces);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/zyc1981/log4j2-customPatternLayout.git
git@gitee.com:zyc1981/log4j2-customPatternLayout.git
zyc1981
log4j2-customPatternLayout
log4j2-customPatternLayout
master

搜索帮助