Skip to content
This repository was archived by the owner on Sep 13, 2023. It is now read-only.

Commit 07576bf

Browse files
committed
merged truncate feature from extras to trunk
git-svn-id: https://svn.apache.org/repos/asf/logging/log4j/trunk@1481783 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0356b92 commit 07576bf

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</properties>
2222
<body>
2323
<release version="1.2.18" date="2012-??-??" description="Maintenance release">
24+
<action action="update" dev="grobmeier">Merged truncate feature to FormattingInfo from extras</action>
2425
<action issue="54845" action="fix" dev="ggregory" due-to="Robert Balent">Typo in log4j 1.2 manual</action>
2526
<action issue="53299" action="update" dev="grobmeier">Simplify log4j build</action>
2627
<action issue="44370" action="fix" dev="grobmeier">MANIFEST.MF broken in log4j-1.2.15.jar</action>

src/main/java/org/apache/log4j/pattern/FormattingInfo.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,46 @@ public final class FormattingInfo {
5656
*/
5757
private final boolean leftAlign;
5858

59+
/**
60+
* Right truncation.
61+
* @since 1.2.17
62+
*/
63+
private final boolean rightTruncate;
64+
5965
/**
6066
* Creates new instance.
6167
* @param leftAlign left align if true.
6268
* @param minLength minimum length.
6369
* @param maxLength maximum length.
70+
* @deprecated since 1.2.17
6471
*/
6572
public FormattingInfo(
66-
final boolean leftAlign, final int minLength, final int maxLength) {
73+
final boolean leftAlign,
74+
final int minLength,
75+
final int maxLength) {
6776
this.leftAlign = leftAlign;
6877
this.minLength = minLength;
6978
this.maxLength = maxLength;
79+
this.rightTruncate = false;
80+
}
81+
82+
/**
83+
* Creates new instance.
84+
* @param leftAlign left align if true.
85+
* @param rightTruncate right truncate if true.
86+
* @param minLength minimum length.
87+
* @param maxLength maximum length.
88+
* @since 1.2.17
89+
*/
90+
public FormattingInfo(
91+
final boolean leftAlign,
92+
final boolean rightTruncate,
93+
final int minLength,
94+
final int maxLength) {
95+
this.leftAlign = leftAlign;
96+
this.minLength = minLength;
97+
this.maxLength = maxLength;
98+
this.rightTruncate = rightTruncate;
7099
}
71100

72101
/**
@@ -85,6 +114,15 @@ public boolean isLeftAligned() {
85114
return leftAlign;
86115
}
87116

117+
/**
118+
* Determine if right truncated.
119+
* @return true if right truncated.
120+
* @since 1.2.17
121+
*/
122+
public boolean isRightTruncated() {
123+
return rightTruncate;
124+
}
125+
88126
/**
89127
* Get minimum length.
90128
* @return minimum length.
@@ -111,7 +149,11 @@ public void format(final int fieldStart, final StringBuffer buffer) {
111149
final int rawLength = buffer.length() - fieldStart;
112150

113151
if (rawLength > maxLength) {
114-
buffer.delete(fieldStart, buffer.length() - maxLength);
152+
if(rightTruncate) {
153+
buffer.setLength(fieldStart + maxLength);
154+
} else {
155+
buffer.delete(fieldStart, buffer.length() - maxLength);
156+
}
115157
} else if (rawLength < minLength) {
116158
if (leftAlign) {
117159
final int fieldEnd = buffer.length();

src/main/java/org/apache/log4j/pattern/PatternParser.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,22 @@ public static void parse(
304304
case '-':
305305
formattingInfo =
306306
new FormattingInfo(
307-
true, formattingInfo.getMinLength(),
307+
true,
308+
formattingInfo.isRightTruncated(),
309+
formattingInfo.getMinLength(),
308310
formattingInfo.getMaxLength());
311+
break;
309312

313+
case '!':
314+
formattingInfo =
315+
new FormattingInfo(
316+
formattingInfo.isLeftAligned(),
317+
true,
318+
formattingInfo.getMinLength(),
319+
formattingInfo.getMaxLength());
310320
break;
311321

322+
312323
case '.':
313324
state = DOT_STATE;
314325

@@ -319,7 +330,9 @@ public static void parse(
319330
if ((c >= '0') && (c <= '9')) {
320331
formattingInfo =
321332
new FormattingInfo(
322-
formattingInfo.isLeftAligned(), c - '0',
333+
formattingInfo.isLeftAligned(),
334+
formattingInfo.isRightTruncated(),
335+
c - '0',
323336
formattingInfo.getMaxLength());
324337
state = MIN_STATE;
325338
} else {
@@ -343,6 +356,7 @@ public static void parse(
343356
formattingInfo =
344357
new FormattingInfo(
345358
formattingInfo.isLeftAligned(),
359+
formattingInfo.isRightTruncated(),
346360
(formattingInfo.getMinLength() * 10) + (c - '0'),
347361
formattingInfo.getMaxLength());
348362
} else if (c == '.') {
@@ -364,7 +378,9 @@ public static void parse(
364378
if ((c >= '0') && (c <= '9')) {
365379
formattingInfo =
366380
new FormattingInfo(
367-
formattingInfo.isLeftAligned(), formattingInfo.getMinLength(),
381+
formattingInfo.isLeftAligned(),
382+
formattingInfo.isRightTruncated(),
383+
formattingInfo.getMinLength(),
368384
c - '0');
369385
state = MAX_STATE;
370386
} else {
@@ -383,7 +399,9 @@ public static void parse(
383399
if ((c >= '0') && (c <= '9')) {
384400
formattingInfo =
385401
new FormattingInfo(
386-
formattingInfo.isLeftAligned(), formattingInfo.getMinLength(),
402+
formattingInfo.isLeftAligned(),
403+
formattingInfo.isRightTruncated(),
404+
formattingInfo.getMinLength(),
387405
(formattingInfo.getMaxLength() * 10) + (c - '0'));
388406
} else {
389407
i = finalizeConverter(

0 commit comments

Comments
 (0)