-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #552 from syjer/jdklogging-formatting-opt
optimize the log formatter
- Loading branch information
Showing
5 changed files
with
161 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/LogMessageIdFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.openhtmltopdf.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
class LogMessageIdFormat { | ||
|
||
private final Object PLACEHOLDER = new Object(); | ||
|
||
|
||
private final List<Object> tokens; | ||
|
||
LogMessageIdFormat(String message) { | ||
this.tokens = prepareFormatter(message); | ||
} | ||
|
||
private List<Object> prepareFormatter(String messageFormat) { | ||
List<Object> v = new ArrayList<>(); | ||
int idx = 0; | ||
while(true) { | ||
int newIdx = messageFormat.indexOf("{}", idx); | ||
String messageSegment = newIdx == -1 ? messageFormat.substring(idx) : messageFormat.substring(idx, newIdx); | ||
if (!messageSegment.isEmpty()) { | ||
v.add(messageSegment); | ||
} | ||
if (newIdx == -1) { | ||
break; | ||
} | ||
idx = newIdx + 2; | ||
v.add(PLACEHOLDER); | ||
} | ||
return v; | ||
} | ||
|
||
String formatMessage(Object[] args) { | ||
StringBuilder sb = new StringBuilder(); | ||
int size = tokens.size(); | ||
int argsUse = 0; | ||
for (int i = 0; i < size; i++) { | ||
Object f = tokens.get(i); | ||
if (f == PLACEHOLDER) { | ||
Object argument = args[argsUse]; | ||
sb.append(argument); | ||
argsUse++; | ||
} else { | ||
sb.append(f); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
} |
Oops, something went wrong.