简体   繁体   English

如何只迁移消息而不是log4j中的所有行?

[英]How to escape only message instead of all row in log4j?

I have the following PatternLayout: 我有以下PatternLayout:

public class EscapedEnhancedPatternLayout extends EnhancedPatternLayout {
    @Override
    public String format(LoggingEvent event) {
        return StringEscapeUtils.escapeJava(super.format(event));
    }
}

but this escapes full logging row. 但这会逃脱完整的日志行。

I want to have something like this but only for message. 我希望有这样的东西,但仅限于消息。

but LoggingEvent class has not setMessage and setRenderedMessage methods. 但是LoggingEvent类没有setMessagesetRenderedMessage方法。

And I don't see copy constructor at LoggingEvent . 而且我没有在LoggingEvent看到复制构造函数。 If LoggingEvent had copy constructor I can inherited from LoggingEvent and override methods mentioned below. 如果LoggingEvent具有复制构造函数,我可以从LoggingEvent继承并覆盖下面提到的方法。

Please advise me how to solve my issue. 请告诉我如何解决我的问题。

You're right, there is no LoggingEvent(LoggingEvent other) constructor, but you can pass the event's values to the LoggingEvent constructor in your format method like this: 你是对的,没有LoggingEvent(LoggingEvent other)构造函数,但你可以在format方法中将事件的值传递给LoggingEvent构造函数 ,如下所示:

@Override
public String format(LoggingEvent event) {
    Object msgObj = event.getMessage();

    LoggingEvent newEvent = new LoggingEvent(
        event.getFQNOfLoggerClass(),
        event.getLogger(), event.getTimeStamp(),
        event.getLevel(),
        StringEscapeUtils.escapeJava(msgObj != null ? msgObj.toString() : null),
        event.getThreadName(),
        event.getThrowableInformation(),
        event.getNDC(),
        event.getLocationInformation(),
        event.getProperties());

    return super.format(newEvent);
}

This will create a new LoggingEvent from the old one with all values set. 这将从旧的LoggingEvent创建一个新的LoggingEvent ,并设置所有值。 The StringEscapeUtils.escapeJava method can now modify the the message without affecting the other properties and you can still use super.format StringEscapeUtils.escapeJava方法现在可以修改message而不影响其他属性,您仍然可以使用super.format

An alternative approach could be to create a customized LogEventFactory . 另一种方法是创建自定义的LogEventFactory A LogEventFactory is used to generate LogEvents. LogEventFactory用于生成LogEvents。 Applications may replace the standard LogEventFactory by setting the value of the system property Log4jLogEventFactory to the name of the custom LogEventFactory class. 应用程序可以通过将系统属性Log4jLogEventFactory的值设置为自定义LogEventFactory类的名称来替换标准LogEventFactory

The method that will be called to create a instance of LogEvent is as follows: 将调用以创建LogEvent实例的方法如下:

LogEvent createEvent(String loggerName,
               org.apache.logging.log4j.Marker marker,
               String fqcn,
               org.apache.logging.log4j.Level level,
               org.apache.logging.log4j.message.Message data,
               List<Property> properties,
               Throwable t)

and DefaultLogEventFactory is the basic implementation for log4j. DefaultLogEventFactory是log4j的基本实现。 In your case you could extend the basic implementation and escape the message after which you call the default implementation. 在您的情况下,您可以扩展基本实现并转义消息,然后调用默认实现。 It would become something like this: 它会变成这样的:

public class MyCustomLogEventFactory extends DefaultLogEventFactory {

    /**
     * Creates a log event.
     *
     * @param loggerName The name of the Logger.
     * @param marker An optional Marker.
     * @param fqcn The fully qualified class name of the caller.
     * @param level The event Level.
     * @param data The Message.
     * @param properties Properties to be added to the log event.
     * @param t An optional Throwable.
     * @return The LogEvent.
     */
     @Override
    public LogEvent createEvent(final String loggerName, final Marker marker,
                             final String fqcn, final Level level, final Message data,
                             final List<Property> properties, final Throwable t) {
        super.createEvent(loggerName, marker, fqcn, level, StringEscapeUtils.escapeJava(data != null ? data.toString() : null), properties, t);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM