diff --git a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/resource/log/PipeLogger.java b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/resource/log/PipeLogger.java index 70b494da03270..9d7be703c9c73 100644 --- a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/resource/log/PipeLogger.java +++ b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/resource/log/PipeLogger.java @@ -30,7 +30,7 @@ public class PipeLogger { public static void log( final Consumer loggerFunction, final String rawMessage, final Object... formatter) { - logger.log(loggerFunction, rawMessage, formatter); + logger.log(loggerFunction, "%s", format(rawMessage, formatter)); } public static void log( @@ -40,7 +40,7 @@ public static void log( final Object... formatter) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); throwable.printStackTrace(new PrintStream(out)); - logger.log(loggerFunction, rawMessage + "\n" + out, formatter); + logger.log(loggerFunction, "%s", format(rawMessage, formatter) + "\n" + out); } public static void setLogger(final PipePeriodicalLogger logger) { @@ -51,6 +51,12 @@ private PipeLogger() { // static } + private static String format(final String rawMessage, final Object... formatter) { + return formatter == null || formatter.length == 0 + ? rawMessage + : String.format(rawMessage, formatter); + } + @FunctionalInterface public interface PipePeriodicalLogger { void log(final Consumer loggerFunction, final String rawMessage, final Object... args); diff --git a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/pipe/resource/PipeLoggerTest.java b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/pipe/resource/PipeLoggerTest.java new file mode 100644 index 0000000000000..122dfea7ca161 --- /dev/null +++ b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/pipe/resource/PipeLoggerTest.java @@ -0,0 +1,69 @@ +/* + * 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.commons.pipe.resource; + +import org.apache.iotdb.commons.pipe.resource.log.PipeLogger; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.atomic.AtomicReference; + +public class PipeLoggerTest { + + @Test + public void testLogMessageWithPercent() { + final AtomicReference message = new AtomicReference<>(); + + setStringFormatLogger(); + try { + PipeLogger.log(message::set, "data_{sink.password=%/broken}"); + } finally { + setStringFormatLogger(); + } + + Assert.assertEquals("data_{sink.password=%/broken}", message.get()); + } + + @Test + public void testLogThrowableWithPercentInStackTrace() { + final AtomicReference message = new AtomicReference<>(); + + setStringFormatLogger(); + try { + PipeLogger.log( + message::set, + new RuntimeException("data_{sink.password=%/broken}"), + "Failed to transfer event %s", + "root.sg.d1"); + } finally { + setStringFormatLogger(); + } + + Assert.assertTrue(message.get().contains("Failed to transfer event root.sg.d1")); + Assert.assertTrue(message.get().contains("data_{sink.password=%/broken}")); + } + + private void setStringFormatLogger() { + PipeLogger.setLogger( + (loggerFunction, rawMessage, formatter) -> + loggerFunction.accept(String.format(rawMessage, formatter))); + } +}