Log4j2 - 文件中的事务日志记录和控制台上的应用程序日志记录

Log4j2 - Transaction logging in file and application logging on console

我正在开发一个记录器,其中某些事务消息需要记录到一个文件中,一些应用程序日志需要记录到控制台中。目前,交易消息也被记录到控制台上,我想避免这种情况。

例如:

如果我写logger.info("application process started"); 这需要在 console.

上登录

当我写 logger.info(new ObjectArrayMessage("msg1","msg2","msg3")); 这需要记录到 csv 文件.

这是我当前的输出:

19:18:42.230 [main5] INFO  New.CSVlog.App - application process started
19:18:42.233 [main5] INFO  New.CSVlog.App - [msg1,msg2,msg3]

我只想将第一个日志记录到控制台,将第二个日志记录到 CSV 文件。

我有以下属性文件:

appenders = csvFile, console

appender.csvFile.type = RollingFile
appender.csvFile.name = CSVFILE
appender.csvFile.fileName = csvLog.csv
appender.csvFile.filePattern= csvLog-%d{MM-dd-yyyy}-%i.csv
appender.csvFile.layout.type = CsvParameterLayout
appender.csvFile.layout.delimiter = ,
appender.csvFile.layout.header = column1,column2,column3\n
appender.csvFile.policies.type = Policies
appender.csvFile.policies.time.type = TimeBasedTriggeringPolicy
appender.csvFile.policies.time.interval = 1
appender.csvFile.policies.time.modulate = true
appender.csvFile.policies.size.type = SizeBasedTriggeringPolicy
appender.csvFile.policies.size.size=10MB
appender.csvFile.strategy.type = DefaultRolloverStrategy
appender.csvFile.strategy.max = 200

appender.console.type = Console
appender.console.name = consoleLog
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{HH:mm:ss.SSS} [%t5] %-5level %logger{36} - %msg%n


rootLogger.level =debug
rootLogger.appenderRefs = csvFile, consoleLog
rootLogger.appenderRef.csvFile.ref = CSVFILE


rootLogger.appenderRef.console.ref = consoleLog

任何人都可以帮我使用同一个记录器来记录事务和应用程序日志吗? 我需要在属性文件中进行哪些更改?谢谢!

您可以使用 Filters 将 ObjectArrayMessage 消息与其余消息分开(参见 documentation)。虽然没有检查 Message 对象的 class 的 built-in 过滤器,但您可以轻松编写一个:

@Plugin(name = "ObjectArrayMessageFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE)
public class ObjectArrayMessageFilter extends AbstractFilter {

    public static class Builder extends AbstractFilterBuilder<Builder>
            implements org.apache.logging.log4j.core.util.Builder<ObjectArrayMessageFilter> {

        @Override
        public ObjectArrayMessageFilter build() {
            return new ObjectArrayMessageFilter(getOnMatch(), getOnMismatch());
        }
    }

    @PluginBuilderFactory
    public static Builder newBuilder() {
        return new Builder();
    }

    private ObjectArrayMessageFilter(Result onMatch, Result onMismatch) {
        super(onMatch, onMismatch);
    }

    private static Result filter(Object msg) {
        return msg instanceof ObjectArrayMessage ? onMatch : onMismatch;
    }

    @Override
    public Result filter(final LogEvent event) {
        return filter(event.getMessage());
    }

    @Override
    public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
            final Throwable t) {
        return filter(msg);
    }

    @Override
    public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
            final Throwable t) {
        return filter(msg);
    }
}

然后您可以将过滤器附加到附加程序,以便它始终应用于给定的附加程序:

appender.csvFile.type = RollingFile
...
appender.csvFile.filter.omaf.type = ObjectArrayMessageFilter
appender.csvFile.filter.omaf.onMatch = ACCEPT
appender.csvFile.filter.omaf.onMismatch = DENY
appender.console.type = Console
...
appender.console.filter.omaf.type = ObjectArrayMessageFilter
appender.console.filter.omaf.onMatch = DENY
appender.console.filter.omaf.onMismatch = ACCEPT

备注:开发自定义插件时,一些标准规则适用:cf. documentation。基本上插件的代码必须在 log4j-core.

中使用注释处理器进行编译