找到log4j appender以应用于我的日志

时间:2017-03-30 21:41:01

标签: java log4j

我在log4j2.xml文件中配置了我的appender

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="${PATTERN}" />
    </Console>
    <Routing name="ConferenceLog">
        <Routes pattern="${ctx:logFileName}">
            <Route>
                <RollingFile name="Rolling-${ctx:logFileName}"
                    fileName="${LOG_DIR}/${ctx:logFileName}.log" 
                    filePattern="${LOG_DIR}/${ctx:logFileName}.%i.log.gz">
                    <PatternLayout pattern="${PATTERN}" />
                    <SizeBasedTriggeringPolicy size="20971520" />
                </RollingFile>
            </Route>
            <Route ref="Console" key="${ctx:logFileName}" />
        </Routes>
    </Routing>
</Appenders>
<Loggers>
    <Logger name="conference.logger" level="DEBUG" additivity="false">
        <AppenderRef ref="ConferenceLog" />
    </Logger>
</Loggers>

在运行时我试图找到并将其应用于特定的日志条目

private void logger(Long meetingId, Log log, Logger logger, Level level, String message, Throwable error) {
    ThreadContext.put(LOG_FILE_NAME, String.valueOf(meetingId));

    Logger rootLogger = Logger.getRootLogger();
    Appender conferenceLoggerAppender = rootLogger.getAppender("ConferenceLog");
    logger.addAppender(conferenceLoggerAppender);
    logger(log, level, message, error);
    logger.removeAppender(conferenceLoggerAppender);

    ThreadContext.remove(LOG_FILE_NAME);
}

但我的conferenceLoggerAppender始终为空。我的逻辑出了什么问题?我试图改变我们目前的做法,即使用conference.logger

Log conferenceLogger = LogFactory.getLog("conference.logger");

然而,这会创建像这样的条目

  

2017-03-30 18:30:11.576 [taskScheduler-24] DEBUG conference.logger

我需要它来使用类名,它来自我们所有其他日志

  

2017-03-30 18:30:11.576 [taskScheduler-24] DEBUG com.foo.bar

我们可以看到appender在那里(在rootLogger中) enter image description here

1 个答案:

答案 0 :(得分:0)

以下是答案

仅供参考:将记录器重命名为conferenceLogger

private void conferenceLogger(Long meetingId, String className, Log log, Level level, String message, Throwable error) {
    addConferenceLogAppender(className);
    ThreadContext.put(LOG_FILE_NAME, String.valueOf(meetingId));
    logger(log, level, message, error);
    ThreadContext.remove(LOG_FILE_NAME);
    removeConferenceLogAppender(className);

}

static void removeConferenceLogAppender(String package_name) {
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configuration configuration = context.getConfiguration();
    Appender appender = configuration.getAppender("ConferenceLog");
    LoggerConfig loggerConfig = configuration.getLoggerConfig(package_name);
    loggerConfig.removeAppender(appender.getName());
    context.updateLoggers();
}

static void addConferenceLogAppender(String package_name) {
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configuration configuration = context.getConfiguration();
    Appender appender = configuration.getAppender("ConferenceLog");
    LoggerConfig loggerConfig = configuration.getLoggerConfig(package_name);
    loggerConfig.addAppender(appender, loggerConfig.getLevel(), null);
    context.updateLoggers();
}