New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LOG4J2-2785: Added support for pattern Layout to abbreviate logger or class names except n rightmost words #542
Conversation
String[] words = original.split("\\."); | ||
for (int i = 0; i < words.length - rightWordCount; i++) { | ||
words[i] = words[i].substring(0, 1); | ||
} | ||
destination.append(String.join(".", words)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks very expensive. String.split uses regex and allocates a new string for each segment. Substring allocates another string per abbreviated segment, and the final String.join creates a string representation of the data that's added to the StringBuilder. I think allocation can be entirely avoided here by tacking indexes, and writing directly to the Destination buffer.
It could be interesting to run the benchmarks comparing 1.1*
to the existing 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your feedback.
Afaik String#split
does not create a Pattern
object for certain one-character patterns such as here.
I've rewritten the method for improved efficiency.
if (original != null && destination != null) { | ||
String[] words = original.split("\\."); | ||
for (int i = 0; i < words.length - rightWordCount; i++) { | ||
words[i] = words[i].substring(0, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably worthwhile to add test coverage for an input string along the lines of org..example.Name
with two periods in a row, I think this substring will throw an IndexOutOfBoundsException in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, you're right. I've fixed this and improved test coverage.
When I have some time to tinker, I think this feature could be added to the existing abbreviator. All existing patterns effectively use Something along the lines of int limit = input.length();
for (int i = 0; I < rightWordCount; I++) {
limit = input.lastIndexOf('.', limit);
if (limit < 0) {
return input;
}
}
// call into standard abbreviator. |
@carterkozak |
@carterkozak Here's another small update to my pull request. |
b7d26d0
to
0a86ecb
Compare
"we love pull requests" they say |
@sman-81 we are all volunteers and have limited time to devote to review. Your most recent comment comes across as entitled and rude. Please work on that. In your position, I might have replied on the PR with "Is there anything I can do to get this into a mergable state?"
I asked you twice to avoid |
I'm a volunteer just like you.
And you are wrong. |
The point is that |
Hi there @carterkozak now |
…onents except the 2 rightmost
This patch was manually extracted and merged since the backing repo has been deleted. |
Hard to believe my PR actually got merged after a little over 1,5 years :) |
Hi, I've added a specialized abbreviator named
DynamicWordAbbreviator
to address the specific requirement of this feature request. Javadoc added and JUnit test updated to cover the new feature.