在 Log4j2 中以编程方式使用 Layout / PatternLayout

发布于 2025-01-14 12:24:34 字数 3130 浏览 0 评论 0原文

我正在按照 迁移指南 ( Apache 的选项 2)。该项目是一个大型且模块化的项目,我不了解每个模块如何工作。在阅读迁移指南(Apache 的选项 2) 时,有人说,我必须更改几个包名称,将 Logger 更改为 LogManager,使用另一种方法设置级别并更改字符串连接的工作方式。但没有提到一大堆模块将不再工作,因为它们包含扩展 布局模式布局。我不想删除这些类,也不想借助属性文件来更改它的实现。我非常肯定,在属性文件的帮助下无法重现该功能。那么Log4j2中的Layout和PatternLayout相当于什么呢?下面是一个从 Layout 扩展的类的小示例:

public class MXMLLayout extends Layout {
@Override
public String format(LoggingEvent event) {
    MXMLHelper helper = new MXMLHelper();
    String message = (String) event.getMessage();
    String[] secondLevel = {};
    String attributes = "";
    String attrNumber = "";
    boolean flagProcessInst = false;
    boolean flagAudit = false;
    boolean flagProcess = false;
    int in = -1;
    String[] firstLevel = null;

    if (message.contains(MXMLCommandCenter.AUDIT_TRAIL_SEP) && (!message
                    .contains(MXMLCommandCenter.PROCESS_INST_SEP)
                    || !message.contains(MXMLCommandCenter.PROCESS_SEP))) {
        firstLevel = message.split(MXMLCommandCenter.AUDIT_TRAIL_SEP);
        flagAudit = true;
    } else if (message.contains(MXMLCommandCenter.PROCESS_INST_SEP)
                    && (!message.contains(MXMLCommandCenter.AUDIT_TRAIL_SEP)
                                    || !message.contains(
                                                    MXMLCommandCenter.PROCESS_SEP))) {
        firstLevel = message.split(MXMLCommandCenter.PROCESS_INST_SEP);
        flagProcessInst = true;
    } else if (message.contains(MXMLCommandCenter.PROCESS_SEP) && (!message
                    .contains(MXMLCommandCenter.AUDIT_TRAIL_SEP)
                    || !message.contains(
                                    MXMLCommandCenter.PROCESS_INST_SEP))) {
        firstLevel = message.split(MXMLCommandCenter.PROCESS_SEP);
        flagProcess = true;
    } else {
        firstLevel = new String[0];
    }

   // [ ... ]

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(MXMLCommandCenter.FILE));
        } catch (Exception e) {
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame,
                            "The MXML log file has been deleted.",
                            "MXMLPlugin Error", JOptionPane.ERROR_MESSAGE);
            frame.dispose();

            MXMLCommandCenter.lockEnvironment();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        }
   // [ ... ]
}

我有几个使用 Layout/PatternLayout 的大型复杂类。因为我想升级日志框架,我是否必须分析所有这些并寻求替代实现?

I'm migrating a large application from Log4j1 to Log4j2 following the Migration Guide (Option 2 by Apache). The project is a large and modularized and I don't have the knowledge on how every module works. When reading the Migration Guide (Option 2 by Apache) it was said, that I have to change a couple package names, change Logger into LogManager, use another way to set the Levels and change how String concatenation works. But nowhere it was mentioned that a whole bunch of modules won't work anymore because they contain classes that extend Layout or PatternLayout. I don't want to delete the classes and I don't want to change it's implementation with the help of the properties file. I'm quite positive that the functionality can't be reproduced with the help of the properties file. So what is the equivalent of Layout and PatternLayout in Log4j2? Here is a small example of a class that extends from Layout:

public class MXMLLayout extends Layout {
@Override
public String format(LoggingEvent event) {
    MXMLHelper helper = new MXMLHelper();
    String message = (String) event.getMessage();
    String[] secondLevel = {};
    String attributes = "";
    String attrNumber = "";
    boolean flagProcessInst = false;
    boolean flagAudit = false;
    boolean flagProcess = false;
    int in = -1;
    String[] firstLevel = null;

    if (message.contains(MXMLCommandCenter.AUDIT_TRAIL_SEP) && (!message
                    .contains(MXMLCommandCenter.PROCESS_INST_SEP)
                    || !message.contains(MXMLCommandCenter.PROCESS_SEP))) {
        firstLevel = message.split(MXMLCommandCenter.AUDIT_TRAIL_SEP);
        flagAudit = true;
    } else if (message.contains(MXMLCommandCenter.PROCESS_INST_SEP)
                    && (!message.contains(MXMLCommandCenter.AUDIT_TRAIL_SEP)
                                    || !message.contains(
                                                    MXMLCommandCenter.PROCESS_SEP))) {
        firstLevel = message.split(MXMLCommandCenter.PROCESS_INST_SEP);
        flagProcessInst = true;
    } else if (message.contains(MXMLCommandCenter.PROCESS_SEP) && (!message
                    .contains(MXMLCommandCenter.AUDIT_TRAIL_SEP)
                    || !message.contains(
                                    MXMLCommandCenter.PROCESS_INST_SEP))) {
        firstLevel = message.split(MXMLCommandCenter.PROCESS_SEP);
        flagProcess = true;
    } else {
        firstLevel = new String[0];
    }

   // [ ... ]

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(MXMLCommandCenter.FILE));
        } catch (Exception e) {
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame,
                            "The MXML log file has been deleted.",
                            "MXMLPlugin Error", JOptionPane.ERROR_MESSAGE);
            frame.dispose();

            MXMLCommandCenter.lockEnvironment();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        }
   // [ ... ]
}

I have several large complex classes that make use of the Layout/PatternLayout. Do I have to analyze them all and seek for alternative implementations because I wanted to upgrade the logging framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文