I’m delighted to follow up on my previous article on Optimising Log Retrieval in IDN , which garnered positive feedback. In this installment, we’re taking our approach to the next level.
In the context of our internal cloud system, log lines may not arrive in proper order. Consequently, when these log lines are retrieved and presented, users often face the challenge of manually rearranging them. Moreover, if a logging line is executed multiple times (such as in a loop), managing multiple entries can be cumbersome, making it difficult to discern the chronological sequence.
Let’s revisit one of the examples from the prior article to illustrate how log lines are currently written:
1 2 3 4 5 6 7 8 9 10 |
String identityName = identity.getName(); String logPrefix = "Generate sAMAccountName - [" + identityName + "] "; String firstName = StringUtils.trimToNull(identity.getAttribute("firstname")); String lastName = StringUtils.trimToNull(identity.getAttribute("lastname")); log.error(logPrefix + "firstName: " + firstName); log.error(logPrefix + "lastName: " + lastName); ... ... |
By adopting a slight modification to this method, we can write multiple log lines with a standardized prefix to easily identify the associated identity.
Taking it a step further, we introduce a logNumber
and encapsulate the entire logging structure into a method that is repeatedly executed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
int logNumber = 0; String identityName = identity.getName(); String logPrefix = "Generate sAMAccountName - [" + identityName + "] "; public void logMessage(String message) { log.error(logPrefix + “[” + logNumber++ + “] ” + message); } String firstName = StringUtils.trimToNull(identity.getAttribute("firstname")); String lastName = StringUtils.trimToNull(identity.getAttribute("lastname")); logMessage("firstName: " + firstName); logMessage("lastName: " + lastName); ... ... |
Key differences from the previous code include the introduction of logNumber
as a newly initiated counter and the implementation of the logMessage
method, which is executed on every line, printing the logNumber and incrementing it accordingly.
The result of this modified code is a more streamlined output, exemplified as follows:
1 2 |
Generate sAMAccountName - [EMP001] [0] firstName: John Generate sAMAccountName - [EMP001] [1] lastName: Doe |
0 Comments