logback.xml使用janino库进行条件判断

  |   0 评论   |   0 浏览

依赖

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

配置 logstash 开关(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"/>
	<!-- 读取配置中的logstash地址-->
	<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 HH🇲🇲ss}) %highlight(%-5level) %boldCyan(${springAppName:-}) %yellow(%thread) %green(%logger) %yellow(%M) %magenta(%L) %msg%n"/>
    <property name="log.pattern" value="%d{yyyy-MM-dd HH🇲🇲ss} %-5level ${springAppName:-} %thread %logger %M %L %msg%n"/>

<!--输出到 logstash的 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>

<!--输出到控制台-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>${log.colorPattern}</pattern>
    </encoder>
</appender>

<!--输出到文件-->
<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 配置

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

标题:logback.xml使用janino库进行条件判断
作者:jockming112
地址:http://www.itwetouch.com/articles/2021/09/27/1632716363322.html