diff --git a/java/fory-core/src/main/java/org/apache/fory/config/Config.java b/java/fory-core/src/main/java/org/apache/fory/config/Config.java index be1eec2157..e80be242e8 100644 --- a/java/fory-core/src/main/java/org/apache/fory/config/Config.java +++ b/java/fory-core/src/main/java/org/apache/fory/config/Config.java @@ -63,6 +63,8 @@ public class Config implements Serializable { private final boolean deserializeNonexistentEnumValueAsNull; private final boolean serializeEnumByName; private final int bufferSizeLimitBytes; + private final ExceptionLogMode exceptionLogMode; + private final int logSampleStep; public Config(ForyBuilder builder) { name = builder.name; @@ -99,6 +101,16 @@ public Config(ForyBuilder builder) { deserializeNonexistentEnumValueAsNull = builder.deserializeNonexistentEnumValueAsNull; serializeEnumByName = builder.serializeEnumByName; bufferSizeLimitBytes = builder.bufferSizeLimitBytes; + exceptionLogMode = builder.exceptionLogMode; + logSampleStep = builder.logSampleStep; + } + + public ExceptionLogMode getExceptionLogMode() { + return exceptionLogMode; + } + + public int getLogSampleStep() { + return logSampleStep; } /** Returns the name for Fory serialization. */ diff --git a/java/fory-core/src/main/java/org/apache/fory/config/ExceptionLogMode.java b/java/fory-core/src/main/java/org/apache/fory/config/ExceptionLogMode.java new file mode 100644 index 0000000000..d6809ac0e5 --- /dev/null +++ b/java/fory-core/src/main/java/org/apache/fory/config/ExceptionLogMode.java @@ -0,0 +1,26 @@ +package org.apache.fory.config; + +/* + * 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. + */ + +public enum ExceptionLogMode { + ALL_PRINT, + SAMPLE_PRINT, + NONE_PRINT +} diff --git a/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java b/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java index cd3955a1ce..d7c79213e1 100644 --- a/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java +++ b/java/fory-core/src/main/java/org/apache/fory/config/ForyBuilder.java @@ -86,6 +86,8 @@ public final class ForyBuilder { boolean serializeEnumByName = false; int bufferSizeLimitBytes = 128 * 1024; MetaCompressor metaCompressor = new DeflaterMetaCompressor(); + ExceptionLogMode exceptionLogMode = ExceptionLogMode.ALL_PRINT; + int logSampleStep; public ForyBuilder() {} @@ -145,6 +147,18 @@ public ForyBuilder serializeEnumByName(boolean serializeEnumByName) { return this; } + /** exception log level choose the log level, print the error message. */ + public ForyBuilder withExceptionLogMode(ExceptionLogMode exceptionLogMode) { + this.exceptionLogMode = exceptionLogMode; + return this; + } + + public ForyBuilder withExceptionLogMode(ExceptionLogMode exceptionLogMode, int logSampleStep) { + this.exceptionLogMode = exceptionLogMode; + this.logSampleStep = logSampleStep; + return this; + } + /** * Whether ignore reference tracking of all time types registered in {@link TimeSerializers} when * ref tracking is enabled. diff --git a/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java b/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java index dd63992345..5926ccacc1 100644 --- a/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java +++ b/java/fory-core/src/main/java/org/apache/fory/util/ExceptionUtils.java @@ -20,6 +20,7 @@ package org.apache.fory.util; import java.lang.reflect.Field; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.fory.Fory; @@ -57,15 +58,35 @@ public static StackOverflowError trySetStackOverflowErrorMessage( public static RuntimeException handleReadFailed(Fory fory, Throwable t) { if (fory.getRefResolver() instanceof MapRefResolver) { - ObjectArray readObjects = ((MapRefResolver) fory.getRefResolver()).getReadObjects(); - // carry with read objects for better trouble shooting. - List objects = Arrays.asList(readObjects.objects).subList(0, readObjects.size); - throw new DeserializationException(objects, t); + List exceptionObjects = getExceptionObjects(fory); + throw new DeserializationException(exceptionObjects, t); } else { Platform.throwException(t); throw new IllegalStateException("unreachable"); } } + public static List getExceptionObjects(Fory fory) { + ObjectArray readObjects = ((MapRefResolver) fory.getRefResolver()).getReadObjects(); + // carry with read objects for better trouble shooting. + List objects = Arrays.asList(readObjects.objects).subList(0, readObjects.size); + switch (fory.getConfig().getExceptionLogMode()) { + case NONE_PRINT: + return new ArrayList<>(); + case SAMPLE_PRINT: + return systematicSample(objects, Math.max(1, fory.getConfig().getLogSampleStep())); + default: + return objects; + } + } + + public static List systematicSample(List list, int step) { + List result = new ArrayList<>(); + for (int i = 0; i < list.size(); i += step) { + result.add(list.get(i)); + } + return result; + } + public static void ignore(Object... args) {} }