This is an automated email from the ASF dual-hosted git repository. jimin pushed a commit to branch 2.x in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.x by this push: new e8a1d40e71 optimize: seata-server log configuration (#7259) e8a1d40e71 is described below commit e8a1d40e71c83d9175163d00c8fa63ebad3a2b90 Author: YoWuwuuuw <2216348...@qq.com> AuthorDate: Wed Apr 2 17:51:11 2025 +0800 optimize: seata-server log configuration (#7259) --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 1 + server/src/main/resources/application.example.yml | 11 +++ .../main/resources/application.raft.example.yml | 11 +++ server/src/main/resources/application.yml | 11 +++ server/src/main/resources/logback-spring.xml | 66 ++++++++++++--- .../src/main/resources/logback/kafka-appender.xml | 15 ++-- .../apache/seata/server/logging/AppenderTest.java | 93 ++++++++++++++++++++++ 8 files changed, 193 insertions(+), 16 deletions(-) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 28563be795..e9e609198b 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -76,6 +76,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#7229](https://github.com/apache/incubator-seata/pull/7229)] update Notice - [[#7234](https://github.com/apache/incubator-seata/pull/7234)] discover the raft leader node from the naming server - [[#7242](https://github.com/apache/incubator-seata/pull/7242)] optimize: optimize ratelimit bucketTokenNumPerSecond config +- [[#7259](https://github.com/apache/incubator-seata/pull/7259)] transfer the logback appender configuration to the yml configuration - [[#6998](https://github.com/apache/incubator-seata/pull/6998)] skip sending some request if client-version is v0 - [[#7250](https://github.com/apache/incubator-seata/pull/7250)] compatible for client_protocol_version > server_protocol_version - [[#7242](https://github.com/apache/incubator-seata/pull/7242)] optimize ratelimit bucketTokenNumPerSecond config diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 51939ed8f6..66777e00a2 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -76,6 +76,7 @@ - [[#7229](https://github.com/apache/incubator-seata/pull/7229)] 更新 Notice - [[#7234](https://github.com/apache/incubator-seata/pull/7234)] 优化raft对接namingserve时的服务发现逻辑 - [[#7242](https://github.com/apache/incubator-seata/pull/7242)] 更改参考案例下的ratelimit配置 +- [[#7259](https://github.com/apache/incubator-seata/pull/7259)] 将logback appender配置转移到yml配置 - [[#6998](https://github.com/apache/incubator-seata/pull/6998)] 跳过协议版本v0不支持的request - [[#7250](https://github.com/apache/incubator-seata/pull/7250)] 适配 client_protocol_version > server_protocol_version场景 - [[#7232](https://github.com/apache/incubator-seata/pull/7232)] 增加 license header diff --git a/server/src/main/resources/application.example.yml b/server/src/main/resources/application.example.yml index aedae28712..e48ed6833b 100644 --- a/server/src/main/resources/application.example.yml +++ b/server/src/main/resources/application.example.yml @@ -28,10 +28,21 @@ logging: path: ${log.home:${user.home}/logs/seata} extend: logstash-appender: + # off by default + enabled: false destination: 127.0.0.1:4560 kafka-appender: + # off by default + enabled: false bootstrap-servers: 127.0.0.1:9092 topic: logback_to_logstash + producer: + acks: 0 + linger-ms: 1000 + max-block-ms: 0 + metric-appender: + # off by default + enabled: false seata: config: # support: nacos 、 consul 、 apollo 、 zk 、 etcd3 diff --git a/server/src/main/resources/application.raft.example.yml b/server/src/main/resources/application.raft.example.yml index 67e43d207e..6e60b757f0 100644 --- a/server/src/main/resources/application.raft.example.yml +++ b/server/src/main/resources/application.raft.example.yml @@ -28,10 +28,21 @@ logging: path: ${log.home:${user.home}/logs/seata} extend: logstash-appender: + # off by default + enabled: false destination: 127.0.0.1:4560 kafka-appender: + # off by default + enabled: false bootstrap-servers: 127.0.0.1:9092 topic: logback_to_logstash + producer: + acks: 0 + linger-ms: 1000 + max-block-ms: 0 + metric-appender: + # off by default + enabled: false seata: config: # support: nacos 、 consul 、 apollo 、 zk 、 etcd3 diff --git a/server/src/main/resources/application.yml b/server/src/main/resources/application.yml index be6345ef13..478f3be024 100644 --- a/server/src/main/resources/application.yml +++ b/server/src/main/resources/application.yml @@ -28,10 +28,21 @@ logging: path: ${log.home:${user.home}/logs/seata} extend: logstash-appender: + # off by default + enabled: false destination: 127.0.0.1:4560 kafka-appender: + # off by default + enabled: false bootstrap-servers: 127.0.0.1:9092 topic: logback_to_logstash + producer: + acks: 0 + linger-ms: 1000 + max-block-ms: 0 + metric-appender: + # off by default + enabled: false seata: config: diff --git a/server/src/main/resources/logback-spring.xml b/server/src/main/resources/logback-spring.xml index 07b344f845..880cb91011 100644 --- a/server/src/main/resources/logback-spring.xml +++ b/server/src/main/resources/logback-spring.xml @@ -33,6 +33,11 @@ <springProperty name="LOG_BASH_DIR" source="spring.config.additional-location" defaultValue="" /> <springProperty name="APPLICATION_NAME" source="spring.application.name" defaultValue="seata-server"/> + <!-- Appender enable properties --> + <springProperty name="LOGSTASH_APPENDER_ENABLED" source="logging.extend.logstash-appender.enabled" defaultValue="false"/> + <springProperty name="KAFKA_APPENDER_ENABLED" source="logging.extend.kafka-appender.enabled" defaultValue="false"/> + <springProperty name="METRIC_APPENDER_ENABLED" source="logging.extend.metric-appender.enabled" defaultValue="false"/> + <if condition='property("LOG_BASH_DIR").equals("")' > <then> <!-- console-appender --> @@ -42,21 +47,50 @@ <include resource="logback/file-appender.xml"/> <!-- logstash-appender: off by default --> - <!--<include resource="logback/logstash-appender.xml"/>--> + <if condition='property("LOGSTASH_APPENDER_ENABLED").equals("true")'> + <then> + <include resource="logback/logstash-appender.xml"/> + </then> + </if> + <!-- metric-appender: off by default --> - <!--<include resource="logback/metric-appender.xml"/>--> + <if condition='property("METRIC_APPENDER_ENABLED").equals("true")'> + <then> + <include resource="logback/metric-appender.xml"/> + </then> + </if> + <!-- kafka-appender: off by default --> - <!--<include resource="logback/kafka-appender.xml"/>--> + <if condition='property("KAFKA_APPENDER_ENABLED").equals("true")'> + <then> + <include resource="logback/kafka-appender.xml"/> + </then> + </if> </then> <else> <include file="${LOG_BASH_DIR}/logback/console-appender.xml"/> <include file="${LOG_BASH_DIR}/logback/file-appender.xml"/> + <!-- logstash-appender: off by default --> - <!--<include file="${LOG_BASH_DIR}/logback/logstash-appender.xml"/>--> + <if condition='property("LOGSTASH_APPENDER_ENABLED").equals("true")'> + <then> + <include resource="logback/logstash-appender.xml"/> + </then> + </if> + <!-- metric-appender: off by default --> - <!--<include file="${LOG_BASH_DIR}/logback/metric-appender.xml"/>--> + <if condition='property("METRIC_APPENDER_ENABLED").equals("true")'> + <then> + <include resource="logback/metric-appender.xml"/> + </then> + </if> + <!-- kafka-appender: off by default --> - <!--<include file="${LOG_BASH_DIR}/logback/kafka-appender.xml"/>--> + <if condition='property("KAFKA_APPENDER_ENABLED").equals("true")'> + <then> + <include resource="logback/kafka-appender.xml"/> + </then> + </if> </else> </if> @@ -98,14 +132,28 @@ <appender-ref ref="ASYNC_FILE_ALL"/> <appender-ref ref="ASYNC_FILE_WARN"/> <appender-ref ref="ASYNC_FILE_ERROR"/> + <!-- logstash-appender: off by default --> - <!--<appender-ref ref="LOGSTASH"/>--> + <if condition='property("LOGSTASH_APPENDER_ENABLED").equals("true")'> + <then> + <appender-ref ref="LOGSTASH"/> + </then> + </if> <!-- kafka-appender: off by default --> - <!--<appender-ref ref="KAFKA"/>--> + <if condition='property("KAFKA_APPENDER_ENABLED").equals("true")'> + <then> + <appender-ref ref="KAFKA"/> + </then> + </if> <!-- metric-appender: off by default --> - <!--<appender-ref ref="METRIC"/>--> + <if condition='property("METRIC_APPENDER_ENABLED").equals("true")'> + <then> + <appender-ref ref="METRIC"/> + </then> + </if> + </root> <logger name="org.springframework.security.config.annotation.web.builders.WebSecurity" level="ERROR"/> <logger name="org.springframework.security.web.DefaultSecurityFilterChain" level="ERROR"/> diff --git a/server/src/main/resources/logback/kafka-appender.xml b/server/src/main/resources/logback/kafka-appender.xml index 17f3f697c6..e6372f3cb2 100644 --- a/server/src/main/resources/logback/kafka-appender.xml +++ b/server/src/main/resources/logback/kafka-appender.xml @@ -19,10 +19,11 @@ --> <included> <!-- kafka-appender properties --> - <springProperty name="KAFKA_BOOTSTRAP_SERVERS" source="logging.extend.kafka-appender.bootstrap-servers" - defaultValue="127.0.0.1:9092"/> - <springProperty name="KAFKA_TOPIC" source="logging.extend.kafka-appender.topic" - defaultValue="logback_to_logstash"/> + <springProperty name="KAFKA_BOOTSTRAP_SERVERS" source="logging.extend.kafka-appender.bootstrap-servers" defaultValue="127.0.0.1:9092"/> + <springProperty name="KAFKA_TOPIC" source="logging.extend.kafka-appender.topic" defaultValue="logback_to_logstash"/> + <springProperty name="KAFKA_ACKS" source="logging.extend.kafka-appender.producer.acks" defaultValue="0"/> + <springProperty name="KAFKA_LINGER_MS" source="logging.extend.kafka-appender.producer.linger-ms" defaultValue="1000"/> + <springProperty name="KAFKA_MAX_BLOCK_MS" source="logging.extend.kafka-appender.producer.max-block-ms" defaultValue="0"/> <appender name="KAFKA" class="com.github.danielwegener.logback.kafka.KafkaAppender"> <encoder> @@ -45,8 +46,8 @@ <keyingStrategy class="com.github.danielwegener.logback.kafka.keying.NoKeyKeyingStrategy"/> <deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy"/> <producerConfig>bootstrap.servers=${KAFKA_BOOTSTRAP_SERVERS}</producerConfig> - <producerConfig>acks=0</producerConfig> - <producerConfig>linger.ms=1000</producerConfig> - <producerConfig>max.block.ms=0</producerConfig> + <producerConfig>acks=${KAFKA_ACKS}</producerConfig> + <producerConfig>linger.ms=${KAFKA_LINGER_MS}</producerConfig> + <producerConfig>max.block.ms=${KAFKA_MAX_BLOCK_MS}</producerConfig> </appender> </included> diff --git a/server/src/test/java/org/apache/seata/server/logging/AppenderTest.java b/server/src/test/java/org/apache/seata/server/logging/AppenderTest.java new file mode 100644 index 0000000000..5ff3a7b06d --- /dev/null +++ b/server/src/test/java/org/apache/seata/server/logging/AppenderTest.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.server.logging; + +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.Appender; +import com.github.danielwegener.logback.kafka.KafkaAppender; +import net.logstash.logback.appender.LogstashTcpSocketAppender; +import org.apache.seata.server.logging.logback.appender.MetricLogbackAppender; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.slf4j.impl.StaticLoggerBinder; +import org.springframework.boot.test.context.SpringBootTest; + +import java.lang.reflect.Field; +import java.util.Iterator; + + +@SpringBootTest +public class AppenderTest { + + @BeforeAll + public static void init() { + System.setProperty("logging.extend.logstash-appender.enabled", "true"); + System.setProperty("logging.extend.kafka-appender.enabled", "true"); + System.setProperty("logging.extend.kafka-appender.topic", "test"); + System.setProperty("logging.extend.metric-appender.enabled", "true"); + } + + @Test + public void testAppenderEnabled() { + LoggerContext lc = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory(); + Iterator<Appender<ILoggingEvent>> appenderIterator = lc.getLogger("ROOT").iteratorForAppenders(); + + while (appenderIterator.hasNext()) { + Appender<ILoggingEvent> appender = appenderIterator.next(); + if (appender.getName().equals("KAFKA")) { + KafkaAppender<ILoggingEvent> kafkaAppender = (KafkaAppender<ILoggingEvent>) appender; + + try { + // use reflection to obtain the "protect topic" fields of the abstract class inherited by the appender instance + Class<?> kafkaAppenderClass = kafkaAppender.getClass(); + Field topicField = getDeclaredFieldRecursive(kafkaAppenderClass, "topic"); + topicField.setAccessible(true); + String topic = (String) topicField.get(kafkaAppender); + + Assertions.assertEquals("test", topic); + Assertions.assertInstanceOf(KafkaAppender.class, appender); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + if (appender.getName().equals("METRIC")) { + Assertions.assertInstanceOf(MetricLogbackAppender.class, appender); + } + + if (appender.getName().equals("LOGSTASH")) { + Assertions.assertInstanceOf(LogstashTcpSocketAppender.class, appender); + } + } + } + + private static Field getDeclaredFieldRecursive(Class<?> clazz, String fieldName) + throws NoSuchFieldException { + try { + return clazz.getDeclaredField(fieldName); + } catch (NoSuchFieldException e) { + Class<?> superClass = clazz.getSuperclass(); + if (superClass == null) { + throw e; + } else { + return getDeclaredFieldRecursive(superClass, fieldName); + } + } + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org