From ddb04d3cbaba402e410f7e7c75d975d0fcf0732b Mon Sep 17 00:00:00 2001 From: "blake.qiu" Date: Sun, 16 Jul 2023 20:49:55 +0800 Subject: [PATCH] feat(#10539): When the operation configuration fails, log. --- .../server/aspect/ConfigOpFailureAspect.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 config/src/main/java/com/alibaba/nacos/config/server/aspect/ConfigOpFailureAspect.java diff --git a/config/src/main/java/com/alibaba/nacos/config/server/aspect/ConfigOpFailureAspect.java b/config/src/main/java/com/alibaba/nacos/config/server/aspect/ConfigOpFailureAspect.java new file mode 100644 index 00000000000..8ddbd04fea2 --- /dev/null +++ b/config/src/main/java/com/alibaba/nacos/config/server/aspect/ConfigOpFailureAspect.java @@ -0,0 +1,70 @@ +/* + * Copyright 1999-2022 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.nacos.config.server.aspect; + +import com.alibaba.nacos.config.server.utils.LogUtil; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.slf4j.Logger; +import org.springframework.stereotype.Component; + +/** + * The pointcut for configuration change operations, it will log when the configuration change fails. + * + * @author blake.qiu + */ +@Aspect +@Component +public class ConfigOpFailureAspect { + + private static final Logger LOGGER = LogUtil.DEFAULT_LOG; + + /** + * Pointcut for all methods from 'configRepositoryInterface'. + */ + @Pointcut("within(com.alibaba.nacos.config.server.service.repository..*)") + public void configRepositoryInterfaceMethods() { + } + + /** + * Log message when a method from 'configRepositoryInterface' throws an exception. + */ + @AfterThrowing(pointcut = "configRepositoryInterfaceMethods()", throwing = "exception") + public void logException(JoinPoint joinPoint, Throwable exception) { + try { + Object[] args = joinPoint.getArgs(); + StringBuilder params = new StringBuilder(); + if (args != null) { + for (int i = 0; i < args.length; i++) { + if (i < args.length - 1) { + params.append(args[i]).append(", "); + } else { + params.append(args[i]); + } + } + } + String methodName = joinPoint.getSignature().getName(); + LOGGER.error("An error occurred while executing method [{}].\n Parameters: [{}].", methodName, params, + exception); + } catch (Exception e) { + LOGGER.error("An error occurred while logging the original exception. method [{}]", + joinPoint.getSignature().getName(), e); + } + } +}