2

我想在 logback.xml 的 appender 中设置我需要使用哪个类的文件路径(连同文件名)。请帮忙。以下是我的附加程序

<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
    <discriminator class="com.hotwaxmedia.logger.ThreadNameBasedDiscriminator"/>
    <sift>
        <appender class="ch.qos.logback.core.FileAppender">
            <file>runtime/logs/app-${threadName}.html</file>
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>%p %d ${threadName} [%logger{0} : %-5level] %msg%n%rEx</pattern>
            </layout>
        </appender>
    </sift>
</appender>
4

4 回答 4

2

您可以以编程方式加载附加程序

LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
for (Logger logger : context.getLoggerList()) {
  for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders();     index.hasNext();) {
     Appender<ILoggingEvent> appender = index.next();
  }
}

然后您可以验证 is appenderaFileAppender并且调用getFile(). 看看文档http://logback.qos.ch/apidocs/ch/qos/logback/core/FileAppender.html

于 2013-05-29T11:41:43.100 回答
0

此问题与在 logback 版本 1.0.12 中修复的LOGBACK-833相关。因此,您需要升级到 logback 版本 1.0.12 或更高版本。鉴别器实例必须将“threadName”定义为要导出的键。否则,${threadName} 无法解析。这是修改后的鉴别器:

<discriminator class="com.hotwaxmedia.logger.ThreadNameBasedDiscriminator">
  <!-- the key to export needs to be defined -->
  <key>threadName</key>
</discriminator>
于 2013-05-29T14:38:32.287 回答
0

您必须在“fileNamePattern”标签中使用用户路径,例如:

    <Discriminator>

        <Key>userId</Key>

        <DefaultValue>general</DefaultValue>

    </Discriminator>



    <sift>

        <appender name="FILE-MODULENAME-${userId}" class="ch.qos.logback.core.rolling.RollingFileAppender">

                <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                  <!-- rollover daily -->
                  <fileNamePattern>/opt/webligic12/user_projects/domains/myDomain/servers/AdminServer/logs/%d{yyyy-MM-dd}/byUser/modulename/${userId}-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
                  <timeBasedFileNamingAndTriggeringPolicy
                        class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <!-- or whenever the file size reaches maxFileSize -->
                    <maxFileSize>2MG</maxFileSize>
                  </timeBasedFileNamingAndTriggeringPolicy>
                </rollingPolicy>


            <Append>true</Append>

            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">

                <Pattern>%d %X{user} %level %msg %n</Pattern>

            </encoder>

        </appender>

    </sift>

</appender>
于 2015-06-15T06:21:41.340 回答
0

请参阅 fGo 的答案,您可以获得这样的日志文件路径(假设您的附加程序名称是“SIFT”):

LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
for (Logger logger : context.getLoggerList()) {
  for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders();     index.hasNext();) {
     Appender<ILoggingEvent> appender = index.next();
     if (appender.getName().equals("SIFT")) {
        FileAppender<ILoggingEvent> fileAppender = (FileAppender) appender;
        System.out.println(fileAppender.getFile());
     }
  }
}
于 2021-11-04T07:10:43.807 回答