以编程方式创建logback logger

时间:2012-01-30 07:15:38

标签: java logback

我有关于logback项目的问题。我的要求是我必须动态创建日志属性。让我通过一个例子解释一下。

我的项目与外部系统创建套接字通信,它可能有多个套接字。对于每个套接字,我希望有不同的日志文件,其中包含读取和发送的消息。为此,我以编程方式为套接字创建记录器。问题是当我想基于logback.xml重新配置记录器时(通过添加scan =“true”或重新初始化logback),我创建的记录器变得无法使用。我该如何解决这个问题,还是可以告诉我另一个解决方案?

这是我的配置文件(logback.xml)

<?xml version="1.0" ?>
<configuration>
    <property name="HOME_PATH" value="/data/logs/myapp/" scope="CONTEXT" />
    <property name="MYAPP_LOG_FILE" value="myapp.log" />
    <property name="MYAPP_ROLLING_TEMPLATE" value="%d{yy-MM-dd}" scope="CONTEXT" />
    <property name="MYAPP_OLD_LOG_FILE" value="${MYAPP_LOG_FILE}.%d{yy-MM-dd}" />
    <property name="DEFAULT_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] [%level] %msg%n" scope="CONTEXT" />

    <appender name="myAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${HOME_PATH}${MYAPP_LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${HOME_PATH}${MYAPP_LOG_FILE}.${MYAPP_ROLLING_TEMPLATE}</fileNamePattern>
        </rollingPolicy>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${DEFAULT_PATTERN}</pattern>
        </encoder>
    </appender>

    <logger name="com.myapp" level="DEBUG" additivity="false">
        <appender-ref ref="myAppender" />
    </logger>

    <root level="OFF">
    </root>
</configuration>

在这里你可以看到我如何以编程方式创建记录器(同样,我只对套接字日志这样做)。

public static Logger createLogger(String name) {
        ch.qos.logback.classic.Logger templateLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger("com.myapp");
        LoggerContext context = templateLogger.getLoggerContext();

        String logDir = context.getProperty("HOME_PATH");

        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setPattern(context.getProperty("DEFAULT_PATTERN"));
        encoder.setContext(context);

        DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> timeBasedTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();
        timeBasedTriggeringPolicy.setContext(context);

        TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
        timeBasedRollingPolicy.setContext(context);
        timeBasedRollingPolicy.setFileNamePattern(logDir + name + ".log." + context.getProperty("MYAPP_ROLLING_TEMPLATE"));
        timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedTriggeringPolicy);
        timeBasedTriggeringPolicy.setTimeBasedRollingPolicy(timeBasedRollingPolicy);

        RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
        rollingFileAppender.setAppend(true);
        rollingFileAppender.setContext(context);
        rollingFileAppender.setEncoder(encoder);
        rollingFileAppender.setFile(logDir + name + ".log");
        rollingFileAppender.setName(name + "Appender");
        rollingFileAppender.setPrudent(false);
        rollingFileAppender.setRollingPolicy(timeBasedRollingPolicy);
        rollingFileAppender.setTriggeringPolicy(timeBasedTriggeringPolicy);

        timeBasedRollingPolicy.setParent(rollingFileAppender);

        encoder.start();
        timeBasedRollingPolicy.start();

        rollingFileAppender.stop();
        rollingFileAppender.start();

        ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger(name);
        logbackLogger.setLevel(templateLogger.getLevel());
        logbackLogger.setAdditive(false);
        logbackLogger.addAppender(rollingFileAppender);

        return logbackLogger;
}

这就是我重新初始化logback的方式

private static void initializeLogback() {
    File logbackFile = new File(logFilePath);
    System.setProperty("logback.configurationFile", logbackFile.getAbsolutePath());
    StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton();
    LoggerContext loggerContext = (LoggerContext) loggerBinder.getLoggerFactory();

    loggerContext.reset();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(loggerContext);
    try {
        configurator.doConfigure(logbackFile);
    } catch( JoranException e ) {
        throw new ColumbusRuntimeException(e.getMessage(), e);
    }
}

1 个答案:

答案 0 :(得分:3)

看起来您需要SiftingAppender,其中您的鉴别器将是套接字ID本身,或任何组合变体。我不知道你会遇到什么线程问题(当读取MDC时等),但这应该是一个很好的起点,看起来与你的情况类似。