This is the thing: I want to improve the Logging system in Our Apps.
We have multiple services Running on Cloud (irrelevant).
Those services each one Log everything in their Logs, for example:
public class class1 {
protected Logger logger =
Logger.getLogger(Service1 .class.getName());
public method1 (){
...
logger.info("....."); //It creates a new String
...
logger.debug("..."); //It creates a new String
.
.
.
}
public method2 (){
...
logger.info("....."); //It creates a new String
...
logger.debug("..."); //It creates a new String
.
.
.
}
}
I want to improove this code and standardize the Logging System, but I'm not a Java Expert and I need help to know what's the best approach for this?
I got an Idea, here I go:
I'm thinking to create a Utility Class that manage all Logging Messaging (I think this approach would be with StringBuffer for synchronize) or create an StringBuilder on each method and reuse it, something like this:
public class class1 {
protected Logger logger =
Logger.getLogger(Service1 .class.getName());
public method1 (){
StringBuilder msg = new StringBuilder();
msg.append(" ...").append(" xx ").append(" yy "); //new message
logger.info(msg);
...
msg.setLength(0); //Reset
msg.append(" ...").append(" xx ").append(" yy "); //new message
logger.debug("...");
.
.
.
}
public method2 (){
StringBuilder msg = new StringBuilder();
msg.append(" ...").append(" xx ").append(" yy "); //new message
reusing builder
logger.info(msg);
...
msg.setLength(0); //Reset
msg.append(" ...").append(" xx ").append(" yy "); //new message
reusing builder
logger.debug(msg);
.
.
.
}
}
What do you think?
PD: We are using Log4j and my english is not perfect. This approach is to avoid Sonar fails for doing this:
logger.debug("..." + " " + someVariable + " " + otherVariable);
I hope you understand to me =)