Skip to content

Commit c1ff507

Browse files
committed
fix: Add public setter methods to ExtendedPatternLayout.Builder and make attributes non-static
1 parent 2b99459 commit c1ff507

File tree

2 files changed

+81
-16
lines changed

2 files changed

+81
-16
lines changed

src/main/java/de/rub/nds/modifiablevariable/logging/ExtendedPatternLayout.java

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ private ExtendedPatternLayout(
101101
boolean disableAnsi,
102102
boolean noConsoleNoAnsi,
103103
String headerPattern,
104-
String footerPattern) {
104+
String footerPattern,
105+
boolean initNewLine,
106+
boolean prettyPrinting) {
105107
super(
106108
config,
107109
charset,
@@ -113,6 +115,8 @@ private ExtendedPatternLayout(
113115
.setDisableAnsi(disableAnsi)
114116
.setNoConsoleNoAnsi(noConsoleNoAnsi)
115117
.setPattern(headerPattern)
118+
.setInitNewLine(initNewLine)
119+
.setPrettyPrinting(prettyPrinting)
116120
.build(),
117121
newSerializerBuilder()
118122
.setConfiguration(config)
@@ -122,6 +126,8 @@ private ExtendedPatternLayout(
122126
.setDisableAnsi(disableAnsi)
123127
.setNoConsoleNoAnsi(noConsoleNoAnsi)
124128
.setPattern(footerPattern)
129+
.setInitNewLine(initNewLine)
130+
.setPrettyPrinting(prettyPrinting)
125131
.build());
126132
conversionPattern = eventPattern;
127133
this.patternSelector = patternSelector;
@@ -135,6 +141,8 @@ private ExtendedPatternLayout(
135141
.setNoConsoleNoAnsi(noConsoleNoAnsi)
136142
.setPattern(eventPattern)
137143
.setDefaultPattern("%m%n")
144+
.setInitNewLine(initNewLine)
145+
.setPrettyPrinting(prettyPrinting)
138146
.build();
139147
}
140148

@@ -339,6 +347,8 @@ public static ExtendedPatternLayout createLayout(
339347
.withNoConsoleNoAnsi(noConsoleNoAnsi)
340348
.withHeader(headerPattern)
341349
.withFooter(footerPattern)
350+
.withInitNewLine(false)
351+
.withPrettyPrinting(false)
342352
.build();
343353
}
344354

@@ -404,12 +414,8 @@ public static final class Builder
404414
@PluginBuilderAttribute private boolean noConsoleNoAnsi;
405415
@PluginBuilderAttribute private String header;
406416
@PluginBuilderAttribute private String footer;
407-
408-
@PluginBuilderAttribute("initNewLine")
409-
private static boolean initNewLine;
410-
411-
@PluginBuilderAttribute("prettyPrinting")
412-
private static boolean prettyPrinting;
417+
@PluginBuilderAttribute private boolean initNewLine;
418+
@PluginBuilderAttribute private boolean prettyPrinting;
413419

414420
private Builder() {
415421
super();
@@ -541,6 +547,28 @@ public ExtendedPatternLayout.Builder withFooter(String footer) {
541547
return this;
542548
}
543549

550+
/**
551+
* Sets whether to start byte array output on a new line.
552+
*
553+
* @param initNewLine Whether to initialize byte array output on a new line
554+
* @return This builder instance
555+
*/
556+
public ExtendedPatternLayout.Builder withInitNewLine(boolean initNewLine) {
557+
this.initNewLine = initNewLine;
558+
return this;
559+
}
560+
561+
/**
562+
* Sets whether to format byte arrays with spaces between bytes for readability.
563+
*
564+
* @param prettyPrinting Whether to pretty print byte arrays
565+
* @return This builder instance
566+
*/
567+
public ExtendedPatternLayout.Builder withPrettyPrinting(boolean prettyPrinting) {
568+
this.prettyPrinting = prettyPrinting;
569+
return this;
570+
}
571+
544572
/**
545573
* Builds a new ExtendedPatternLayout instance with the configured settings.
546574
*
@@ -565,7 +593,9 @@ public ExtendedPatternLayout build() {
565593
disableAnsi,
566594
noConsoleNoAnsi,
567595
header,
568-
footer);
596+
footer,
597+
initNewLine,
598+
prettyPrinting);
569599
}
570600
}
571601

@@ -655,6 +685,8 @@ public SerializerBuilder() {
655685
private boolean alwaysWriteExceptions;
656686
private boolean disableAnsi;
657687
private boolean noConsoleNoAnsi;
688+
private boolean initNewLine;
689+
private boolean prettyPrinting;
658690

659691
/**
660692
* Builds a serializer for formatting log events according to the configured settings.
@@ -685,7 +717,7 @@ public AbstractStringLayout.Serializer build() {
685717
noConsoleNoAnsi);
686718
PatternFormatter[] formatters = list.toArray(new PatternFormatter[0]);
687719
return new ExtendedPatternLayout.ExtendedPatternLayoutSerializer(
688-
formatters, replace);
720+
formatters, replace, initNewLine, prettyPrinting);
689721
} catch (RuntimeException var4) {
690722
throw new IllegalArgumentException(
691723
"Cannot parse pattern '" + pattern + "'", var4);
@@ -786,18 +818,47 @@ public ExtendedPatternLayout.SerializerBuilder setNoConsoleNoAnsi(boolean noCons
786818
this.noConsoleNoAnsi = noConsoleNoAnsi;
787819
return this;
788820
}
821+
822+
/**
823+
* Sets whether to start byte array output on a new line.
824+
*
825+
* @param initNewLine Whether to initialize byte array output on a new line
826+
* @return This builder instance
827+
*/
828+
public ExtendedPatternLayout.SerializerBuilder setInitNewLine(boolean initNewLine) {
829+
this.initNewLine = initNewLine;
830+
return this;
831+
}
832+
833+
/**
834+
* Sets whether to format byte arrays with spaces between bytes for readability.
835+
*
836+
* @param prettyPrinting Whether to pretty print byte arrays
837+
* @return This builder instance
838+
*/
839+
public ExtendedPatternLayout.SerializerBuilder setPrettyPrinting(boolean prettyPrinting) {
840+
this.prettyPrinting = prettyPrinting;
841+
return this;
842+
}
789843
}
790844

791845
private static final class ExtendedPatternLayoutSerializer
792846
implements AbstractStringLayout.Serializer, LocationAware {
793847
private final PatternFormatter[] formatters;
794848
private final RegexReplacement replace;
849+
private final boolean initNewLine;
850+
private final boolean prettyPrinting;
795851

796852
private ExtendedPatternLayoutSerializer(
797-
PatternFormatter[] formatters, RegexReplacement replace) {
853+
PatternFormatter[] formatters,
854+
RegexReplacement replace,
855+
boolean initNewLine,
856+
boolean prettyPrinting) {
798857
super();
799858
this.formatters = formatters;
800859
this.replace = replace;
860+
this.initNewLine = initNewLine;
861+
this.prettyPrinting = prettyPrinting;
801862
}
802863

803864
@Override
@@ -831,11 +892,13 @@ public String toSerializable(LogEvent event) {
831892
* </ul>
832893
* </ol>
833894
*
834-
* <p>The byte array formatting is controlled by two static configuration options:
895+
* <p>The byte array formatting is controlled by two configuration options:
835896
*
836897
* <ul>
837-
* <li>{@link Builder#prettyPrinting} - Whether to format with spaces between bytes
838-
* <li>{@link Builder#initNewLine} - Whether to start byte arrays on a new line
898+
* <li>{@link ExtendedPatternLayoutSerializer#prettyPrinting} - Whether to format with
899+
* spaces between bytes
900+
* <li>{@link ExtendedPatternLayoutSerializer#initNewLine} - Whether to start byte arrays
901+
* on a new line
839902
* </ul>
840903
*
841904
* @param event The LogEvent to serialize
@@ -870,9 +933,7 @@ public StringBuilder toSerializable(LogEvent event, StringBuilder builder) {
870933
builder.indexOf(Arrays.toString((byte[]) param))
871934
+ Arrays.toString((byte[]) param).length(),
872935
DataConverter.bytesToHexString(
873-
(byte[]) param,
874-
Builder.prettyPrinting,
875-
Builder.initNewLine));
936+
(byte[]) param, prettyPrinting, initNewLine));
876937
}
877938
}
878939
}

src/test/java/de/rub/nds/modifiablevariable/logging/ExtendedPatternLayoutTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ void testBuilderWithAllOptions() {
417417
.withNoConsoleNoAnsi(true)
418418
.withHeader(header)
419419
.withFooter(footer)
420+
.withInitNewLine(false)
421+
.withPrettyPrinting(false)
420422
.build();
421423

422424
assertNotNull(layout);
@@ -441,6 +443,8 @@ void testSerializerBuilderWithAllOptions() {
441443
builder.setAlwaysWriteExceptions(true);
442444
builder.setDisableAnsi(true);
443445
builder.setNoConsoleNoAnsi(true);
446+
builder.setInitNewLine(false);
447+
builder.setPrettyPrinting(false);
444448

445449
AbstractStringLayout.Serializer serializer = builder.build();
446450
assertNotNull(serializer);

0 commit comments

Comments
 (0)