Logback.xml uses the janino library for conditional judgment

rely on

<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>${janino.version}</version>
</dependency>

To configure logstash switch (logback-spring.xml)

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<springProperty scope="context" name="springAppName" source="spring.application.name"/>
<!-- Read from configuration logstash Address -->
<springProperty scope="context" name="ELK" source="cloud-base.log.enable-elk"/>
<springProperty scope="context" name="ELK_HOST" source="cloud-base.log.log-stash-host"/>
<property name="log.path" value="logs/${springAppName}"/>
<property name="log.maxHistory" value="15"/>
<property name="log.colorPattern"
value="%magenta(%d{yyyy-MM-dd HHss}) %highlight(%-5level) %boldCyan(${springAppName:-}) %yellow(%thread) %green(%logger) %yellow(%M) %magenta(%L) %msg%n"/>
<property name="log.pattern" value="%d{yyyy-MM-dd HHss} %-5level ${springAppName:-} %thread %logger %M %L %msg%n"/>
<!-- Output to logstash Of appender-->
<if condition='property("ELK").equals("true")'>
<then>
<appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>${ELK_HOST}</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>\
</appender>
<root level="info">
<appender-ref ref="logstash"/>
</root>
</then>
</if>
<!-- Output to console -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.colorPattern}</pattern>
</encoder>
</appender>
<!-- output to a file -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/info/info.%d{yyyy-MM-dd}.log</fileNamePattern>
<MaxHistory>${log.maxHistory}</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/error/error.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<root level="debug">
<appender-ref ref="console"/>
</root>
<root level="info">
<appender-ref ref="file_info"/>
<appender-ref ref="file_error"/>
</root>
</configuration>

yml To configure

## elk
cloud-base:
log:
enable-elk: false
log-stash-host: 192.168.1.33:4560

title :logback.xml Use janino Condition judgment based on Database
author : jockming112
Address : http://www.itwetouch.com/articles/2021/09/27/1632716363322.html

Please bring the original link to reprint ,thank
Similar articles