I have been doubting for a long time whether or not to have a logger in methods (usually helper methods) like the one below.
Whether we can print it in the method or print the response where the method is called.
public String getAddNewUserUrl()
{
String url = null;
String mode = null;
try
{
log.info("Inside getAddNewUserUrl "); // Can u use logs like this.
mode = new ModeUtil().getMode();
if("local".equalsIgnoreCase(mode) || "staging".equalsIgnoreCase(mode))
{
url = ResourceBundle.getBundle("ApplicationResources").getString("staging.url");
}
else
{
url = ResourceBundle.getBundle("ApplicationResources").getString("staging.url");
}
log.info("The url is : " + url); // Can u use logs like this
}
catch(Exception e)
{
e.printStackTrace();
log.info("Exception in the method getMode ::"+e.getMessage());
}
return url;
Logs are supposed to be put in every part of your application to provide meaningful information to developer/tester/debuggers or any other stake holder.
Just be careful to put appropriate logging level in your helper classes/utilities especially if you going to distribute it as jar.
1
It’s ok to have logging in every part of your application, even in the helper methods, if you can set different logging levels.
log.info("The url is : " + url, LogLevel.Debug);
This ensures that you can turn it on or off depending on how much info you want to gather.
Having it always on, that’s not so smart.
2