从 Logback 中的 Appender 获取当前布局?

发布于 2024-11-16 15:44:25 字数 539 浏览 1 评论 0原文

考虑以下 logback 的基本配置:

<configuration>
    <appender name="ControlAppender" class="org.quackbot.ControlAppender">
        <pattern>%d{MM/dd/yyy hh:mm:ss a}  %-5p %c - </pattern>
    </appender>
</configuration>

如何使用该布局并格式化行?一些不同的博客文章和邮件列表说 AppenderBase 曾经有一个 getLayout() 和 setLayout() 方法,但它们已经被删除了。

在我的代码中重新实现布局的 getter 和 setter 不起作用,它只是返回 null。 event.getFormattedMessage() 返回原始行,而不是格式化的行。我似乎找不到任何其他方法可以根据布局格式化消息。

有没有办法根据配置中指定的模式来格式化消息?

Consider the following basic configuration for logback:

<configuration>
    <appender name="ControlAppender" class="org.quackbot.ControlAppender">
        <pattern>%d{MM/dd/yyy hh:mm:ss a}  %-5p %c - </pattern>
    </appender>
</configuration>

How can I use that layout and format the line? A few different blog posts and mailing lists said that at one time AppenderBase had a getLayout() and setLayout() method, but they have sense been removed.

Reimplementing the getters and setters for the layout in my code don't work, it just returns null. event.getFormattedMessage() returns the original line, not the formatted one. I can't seem to find any other method that would format the message according to the layout.

Is there any way to format a message according to the pattern specified in the configuration?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2024-11-23 15:44:25

Logback 配置只是将您指定的元素组合在一起。它本身并不构成组件。在上面的配置文件中,您没有指定布局元素,因此它不会被注入到您的 ControlAppender 中。

由于我不知道 ControlAppender 是做什么的,所以我无法建议最合适的解决方案。但是,您至少有两个选择:

1) 让 ControlAppender 接受布局作为参数。您的配置文件看起来类似于:

<configuration>
    <appender name="ControlAppender" class="org.quackbot.ControlAppender">
        <layout class="class="ch.qos.logback.classic.PatternLayout"">
          <pattern>%d{MM/dd/yyy hh:mm:ss a}  %-5p %c - </pattern>
        </layout>
    </appender>
</configuration>

Logback 会将 PatternLayout 实例注入 ControlAppender 中。然后,您可以使用 PatternLayout 实例根据您选择的模式来格式化事件。

2) 如果您总是要使用 PatternLayout,您可以直接从模式创建它。您的配置文件看起来类似于:

<configuration>
    <appender name="ControlAppender" class="org.quackbot.ControlAppender">
      <pattern>%d{MM/dd/yyy hh:mm:ss a}  %-5p %c - </pattern>
    </appender>
</configuration>

您需要 ControlAppender 中的 setter 方法(即 setPattern)将模式注入到 ControlAppender 中。一旦有了模式,您就可以自己创建一个 PatternLayout。例如,

 PatternLayout pl = new PatternLayout();
 pl.setPattern(pattern);
 pl.setContext(context);
 pl.start();

如果您需要进一步的帮助,请调用:在 logback 用户列表上喊叫。

Logback configuration just pieces together the elements that you specify. It does not make up components on its own. In the config file above, you have not specified a layout element and as such it won't get injected to your ControlAppender.

Since I don't know what ControlAppender does I can't suggest the most appropriate solution. However, you have at least two options:

1) Have ControlAppender accept a layout as a parameter. Your config file would look similar to:

<configuration>
    <appender name="ControlAppender" class="org.quackbot.ControlAppender">
        <layout class="class="ch.qos.logback.classic.PatternLayout"">
          <pattern>%d{MM/dd/yyy hh:mm:ss a}  %-5p %c - </pattern>
        </layout>
    </appender>
</configuration>

Logback would inject a PatternLayout instance into ControlAppender. You could then use the PatternLayout instance to format events according the pattern of your choice.

2) If you are always going to use a PatternLayout, you could create it directly from the pattern. Your config file would look similar to:

<configuration>
    <appender name="ControlAppender" class="org.quackbot.ControlAppender">
      <pattern>%d{MM/dd/yyy hh:mm:ss a}  %-5p %c - </pattern>
    </appender>
</configuration>

You would need a setter method in ControlAppender, i.e. setPattern, to have the pattern injected into ControlAppender. Once you have the pattern, you could create a PatternLayout on your own. For example by invoking:

 PatternLayout pl = new PatternLayout();
 pl.setPattern(pattern);
 pl.setContext(context);
 pl.start();

Shout on the logback users list if you need further assistance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文