Skip to content

Commit 47efe3c

Browse files
committed
8343395: SSLLogger doesn't work for formatted messages
Reviewed-by: weijun
1 parent bdfe05b commit 47efe3c

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

src/java.base/share/classes/sun/security/ssl/SSLLogger.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import sun.security.x509.*;
4747

4848
import static java.nio.charset.StandardCharsets.UTF_8;
49+
import static sun.security.ssl.Utilities.LINE_SEP;
4950

5051
/**
5152
* Implementation of SSL logger.
@@ -62,6 +63,7 @@ public final class SSLLogger {
6263
private static final String property;
6364
public static final boolean isOn;
6465

66+
6567
static {
6668
String p = System.getProperty("javax.net.debug");
6769
if (p != null) {
@@ -190,7 +192,12 @@ private static void log(Level level, String msg, Object... params) {
190192
try {
191193
String formatted =
192194
SSLSimpleFormatter.formatParameters(params);
193-
logger.log(level, msg, formatted);
195+
// use the customized log method for SSLConsoleLogger
196+
if (logger instanceof SSLConsoleLogger) {
197+
logger.log(level, msg, formatted);
198+
} else {
199+
logger.log(level, msg + ":" + LINE_SEP + formatted);
200+
}
194201
} catch (Exception exp) {
195202
// ignore it, just for debugging.
196203
}
@@ -282,7 +289,7 @@ private static class SSLSimpleFormatter {
282289
""",
283290
Locale.ENGLISH);
284291

285-
private static final MessageFormat extendedCertFormart =
292+
private static final MessageFormat extendedCertFormat =
286293
new MessageFormat(
287294
"""
288295
"version" : "v{0}",
@@ -299,15 +306,6 @@ private static class SSLSimpleFormatter {
299306
""",
300307
Locale.ENGLISH);
301308

302-
//
303-
// private static MessageFormat certExtFormat = new MessageFormat(
304-
// "{0} [{1}] '{'\n" +
305-
// " critical: {2}\n" +
306-
// " value: {3}\n" +
307-
// "'}'",
308-
// Locale.ENGLISH);
309-
//
310-
311309
private static final MessageFormat messageFormatNoParas =
312310
new MessageFormat(
313311
"""
@@ -325,7 +323,7 @@ private static class SSLSimpleFormatter {
325323

326324
private static final MessageFormat messageCompactFormatNoParas =
327325
new MessageFormat(
328-
"{0}|{1}|{2}|{3}|{4}|{5}|{6}\n",
326+
"{0}|{1}|{2}|{3}|{4}|{5}|{6}" + LINE_SEP,
329327
Locale.ENGLISH);
330328

331329
private static final MessageFormat messageFormatWithParas =
@@ -423,7 +421,7 @@ private static String formatParameters(Object ... parameters) {
423421
if (isFirst) {
424422
isFirst = false;
425423
} else {
426-
builder.append(",\n");
424+
builder.append("," + LINE_SEP);
427425
}
428426

429427
if (parameter instanceof Throwable) {
@@ -504,10 +502,10 @@ private static String formatCertificate(Certificate certificate) {
504502
if (isFirst) {
505503
isFirst = false;
506504
} else {
507-
extBuilder.append(",\n");
505+
extBuilder.append("," + LINE_SEP);
508506
}
509-
extBuilder.append("{\n" +
510-
Utilities.indent(certExt.toString()) + "\n}");
507+
extBuilder.append("{" + LINE_SEP +
508+
Utilities.indent(certExt.toString()) + LINE_SEP +"}");
511509
}
512510
Object[] certFields = {
513511
x509.getVersion(),
@@ -521,7 +519,7 @@ private static String formatCertificate(Certificate certificate) {
521519
Utilities.indent(extBuilder.toString())
522520
};
523521
builder.append(Utilities.indent(
524-
extendedCertFormart.format(certFields)));
522+
extendedCertFormat.format(certFields)));
525523
}
526524
} catch (Exception ce) {
527525
// ignore the exception
@@ -578,15 +576,15 @@ private static String formatMapEntry(Map.Entry<String, ?> entry) {
578576
// "string c"
579577
// ]
580578
StringBuilder builder = new StringBuilder(512);
581-
builder.append("\"" + key + "\": [\n");
579+
builder.append("\"" + key + "\": [" + LINE_SEP);
582580
int len = strings.length;
583581
for (int i = 0; i < len; i++) {
584582
String string = strings[i];
585583
builder.append(" \"" + string + "\"");
586584
if (i != len - 1) {
587585
builder.append(",");
588586
}
589-
builder.append("\n");
587+
builder.append(LINE_SEP);
590588
}
591589
builder.append(" ]");
592590

src/java.base/share/classes/sun/security/ssl/Utilities.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ final class Utilities {
4040
Pattern.compile("\\r\\n|\\n|\\r");
4141
private static final HexFormat HEX_FORMATTER =
4242
HexFormat.of().withUpperCase();
43+
static final String LINE_SEP = System.lineSeparator();
4344

4445
/**
4546
* Puts {@code hostname} into the {@code serverNames} list.
@@ -150,15 +151,15 @@ static String indent(String source) {
150151
static String indent(String source, String prefix) {
151152
StringBuilder builder = new StringBuilder();
152153
if (source == null) {
153-
builder.append("\n").append(prefix).append("<blank message>");
154+
builder.append(LINE_SEP).append(prefix).append("<blank message>");
154155
} else {
155156
String[] lines = lineBreakPatern.split(source);
156157
boolean isFirst = true;
157158
for (String line : lines) {
158159
if (isFirst) {
159160
isFirst = false;
160161
} else {
161-
builder.append("\n");
162+
builder.append(LINE_SEP);
162163
}
163164
builder.append(prefix).append(line);
164165
}

test/jdk/sun/security/ssl/SSLLogger/DebugPropertyValuesTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ public class DebugPropertyValuesTest extends SSLSocketTemplate {
7070
debugMessages.put("verbose", List.of("Ignore unsupported cipher suite:"));
7171
debugMessages.put("handshake-expand",
7272
List.of("\"logger\".*: \"javax.net.ssl\",",
73+
"\"specifics\" : \\[",
7374
"\"message\".*: \"Produced ClientHello handshake message"));
7475
debugMessages.put("record-expand",
7576
List.of("\"logger\".*: \"javax.net.ssl\",",
77+
"\"specifics\" : \\[",
7678
"\"message\".*: \"READ: TLSv1.2 application_data"));
7779
debugMessages.put("help",
7880
List.of("print the help messages",
@@ -83,7 +85,12 @@ public class DebugPropertyValuesTest extends SSLSocketTemplate {
8385
// "ALL" shouldn't be seen as a valid Level
8486
debugMessages.put("javax.net.debug.logger.ALL", List.of("ALL:"));
8587
debugMessages.put("javax.net.debug.logger",
86-
List.of("FINE: adding as trusted certificates",
88+
List.of("FINE: adding as trusted certificates:"
89+
+ System.lineSeparator() +
90+
" \"certificate\" : \\{",
91+
"FINE: Produced ClientHello handshake message:" +
92+
System.lineSeparator() +
93+
"\"ClientHello\": \\{",
8794
"FINE: WRITE: TLSv1.3 application_data"));
8895
}
8996

0 commit comments

Comments
 (0)