通过程序覆盖logback.xml配置

时间:2019-09-20 13:42:16

标签: configuration logback

我已经为我的spring项目配置了logback.xml,并且需要通过程序通过读取另一个json文件来覆盖文件中的属性。

下面是我的logback.xml文件

 ->
<appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${AUDIT_LOG_DIR}/${log_name}.log</file>

    <!-- Rolling based on time and size %d refers date RollingPolicy and maxFileSize define each file size -->
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <fileNamePattern>${AUDIT_LOG_DIR}/${log_name}%d{MM-dd-yy-HH-mm}.%i.log</fileNamePattern>   

        <maxFileSize>1KB</maxFileSize>

        <maxHistory>30</maxHistory>
        <totalSizeCap>1GB</totalSizeCap>
    </rollingPolicy>    

    <!-- pattern define the message format %d - to display date of logging, %p - logging priority(INFO,ERROR,WARN), %c  - to print class name, %t - thread,%m - message to be printed -->
    <encoder>
        <pattern>%d %p %c{1.} [%t] %m %n</pattern>
    </encoder>
</appender>

<logger name="auditlog" level="TRACE" additivity="false">
    <appender-ref ref="FILE-ROLLING"/>
</logger>

<root level="OFF">

在我的Java代码中,我有如下主要方法来读取json并获取值

   ObjectMapper mapper = new ObjectMapper();

    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    //JSON file to Java object
    loggingjson = mapper.readValue(new File("resources\\logging.json"),LoggingBaseImpl.class);


    String log_file_name = loggingjson.getLogging().getFiles().getAuditLog().getLogname();

    System.setProperty("log_name",log_file_name);

    Logger  logger  = LoggerFactory.getLogger( "auditlog" );


    for(int i = 0; i < 2000; i++) {
        logger.info("This is the " + i + " time I say 'Hello World'.");
        Thread.sleep(10);
    }

}

在我的json文件中,将具有fileNamePattern,maxFileSize,pattern和所有信息的值。而这些则需要覆盖logback.xml中提到的属性

0 个答案:

没有答案