Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
Expand Down Expand Up @@ -86,22 +85,22 @@ public static List<String> readPartFileContentFromRemote(String filePath,

public static String rollViewLogLines(List<String> lines) {
StringBuilder builder = new StringBuilder();
final int MaxResponseLogSize = 65535;
int totalLogByteSize = 0;
final int maxLogCharSize = 65535;
int totalLogCharSize = 0;
for (String line : lines) {
// If a single line of log is exceed max response size, cut off the line
final int lineByteSize = line.getBytes(StandardCharsets.UTF_8).length;
if (lineByteSize >= MaxResponseLogSize) {
builder.append(line, 0, MaxResponseLogSize)
.append(" [this line's size ").append(lineByteSize).append(" bytes is exceed ")
.append(MaxResponseLogSize).append(" bytes, so only ")
.append(MaxResponseLogSize).append(" characters are reserved for performance reasons.]")
// If a single log line exceeds the character limit, cut off the line.
final int lineCharSize = line.length();
if (lineCharSize >= maxLogCharSize) {
builder.append(line, 0, maxLogCharSize)
.append(" [this line's size ").append(lineCharSize).append(" characters exceeds ")
.append(maxLogCharSize).append(" characters, so only ")
.append(maxLogCharSize).append(" characters are reserved for performance reasons.]")
.append("\r\n");
} else {
builder.append(line).append("\r\n");
}
totalLogByteSize += lineByteSize;
if (totalLogByteSize >= MaxResponseLogSize) {
totalLogCharSize += lineCharSize;
if (totalLogCharSize >= maxLogCharSize) {
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.dolphinscheduler.common.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;

import org.junit.jupiter.api.Test;

class LogUtilsTest {

@Test
void testRollViewLogLinesWithMultibyteCharacters() {
String line = StringUtils.repeat("\u20AC", 22000);

assertEquals(line + "\r\n", LogUtils.rollViewLogLines(Collections.singletonList(line)));
}

@Test
void testRollViewLogLinesTruncatesLongLines() {
for (String character : Arrays.asList("a", "\u20AC")) {
String line = StringUtils.repeat(character, 65536);
String expected = StringUtils.repeat(character, 65535)
+ " [this line's size 65536 characters exceeds 65535 characters, so only "
+ "65535 characters are reserved for performance reasons.]\r\n";

assertEquals(expected, LogUtils.rollViewLogLines(Collections.singletonList(line)));
}
}

@Test
void testRollViewLogLinesStopsAtCharacterLimit() {
String line = StringUtils.repeat("\u20AC", 32768);

assertEquals(line + "\r\n" + line + "\r\n",
LogUtils.rollViewLogLines(Arrays.asList(line, line, "omitted")));
}
}
Loading