diff --git a/external-service-impl/rest/pom.xml b/external-service-impl/rest/pom.xml
index fc064d28254ec..ec1b7b1f59a10 100644
--- a/external-service-impl/rest/pom.xml
+++ b/external-service-impl/rest/pom.xml
@@ -34,6 +34,16 @@
UTF-8
+
+ ch.qos.logback
+ logback-classic
+ test
+
+
+ ch.qos.logback
+ logback-core
+ test
+ org.glassfish.jersey.injectjersey-hk2
diff --git a/external-service-impl/rest/src/main/java/org/apache/iotdb/rest/protocol/v1/handler/ExceptionHandler.java b/external-service-impl/rest/src/main/java/org/apache/iotdb/rest/protocol/v1/handler/ExceptionHandler.java
index 62f3da75f639e..635572b4ef739 100644
--- a/external-service-impl/rest/src/main/java/org/apache/iotdb/rest/protocol/v1/handler/ExceptionHandler.java
+++ b/external-service-impl/rest/src/main/java/org/apache/iotdb/rest/protocol/v1/handler/ExceptionHandler.java
@@ -27,6 +27,7 @@
import org.apache.iotdb.db.exception.StorageEngineException;
import org.apache.iotdb.db.exception.metadata.DatabaseNotSetException;
import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.utils.ErrorHandlingUtils;
import org.apache.iotdb.rest.protocol.exception.RequestLimitExceededException;
import org.apache.iotdb.rest.protocol.v1.model.ExecutionStatus;
import org.apache.iotdb.rpc.TSStatusCode;
@@ -89,7 +90,7 @@ public static ExecutionStatus tryCatchException(Exception e) {
responseResult.setMessage(e.getMessage());
responseResult.setCode(TSStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
}
- LOGGER.warn(e.getMessage(), e);
+ ErrorHandlingUtils.logQueryException(LOGGER, e.getMessage(), responseResult.getCode(), e);
return responseResult;
}
diff --git a/external-service-impl/rest/src/main/java/org/apache/iotdb/rest/protocol/v2/handler/ExceptionHandler.java b/external-service-impl/rest/src/main/java/org/apache/iotdb/rest/protocol/v2/handler/ExceptionHandler.java
index 9ed5111c7e7ce..1ca0c48bd4896 100644
--- a/external-service-impl/rest/src/main/java/org/apache/iotdb/rest/protocol/v2/handler/ExceptionHandler.java
+++ b/external-service-impl/rest/src/main/java/org/apache/iotdb/rest/protocol/v2/handler/ExceptionHandler.java
@@ -27,6 +27,7 @@
import org.apache.iotdb.db.exception.StorageEngineException;
import org.apache.iotdb.db.exception.metadata.DatabaseNotSetException;
import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.utils.ErrorHandlingUtils;
import org.apache.iotdb.rest.protocol.exception.RequestLimitExceededException;
import org.apache.iotdb.rest.protocol.model.ExecutionStatus;
import org.apache.iotdb.rpc.TSStatusCode;
@@ -89,7 +90,7 @@ public static ExecutionStatus tryCatchException(Exception e) {
responseResult.setMessage(e.getMessage());
responseResult.setCode(TSStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
}
- LOGGER.warn(e.getMessage(), e);
+ ErrorHandlingUtils.logQueryException(LOGGER, e.getMessage(), responseResult.getCode(), e);
return responseResult;
}
diff --git a/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/handler/ExceptionHandlerTest.java b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/handler/ExceptionHandlerTest.java
new file mode 100644
index 0000000000000..2677c5214cabd
--- /dev/null
+++ b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/handler/ExceptionHandlerTest.java
@@ -0,0 +1,162 @@
+/*
+ * 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.iotdb.rest.protocol.handler;
+
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.SemanticException;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.read.ListAppender;
+import org.antlr.v4.runtime.misc.ParseCancellationException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+@RunWith(Parameterized.class)
+public class ExceptionHandlerTest {
+ @Parameterized.Parameters(name = "REST v{0}")
+ public static Collection