5

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;

2 Answers 2

6

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
  • 2
    I'd add that it's good to be judicious with logging. In addition to setting the correct logging level, it's important to think about why you're doing the logging, and whether you'll glean (or regret missing) some crucial piece of information at runtime.
    – Jonathan
    Feb 5, 2013 at 13:11
4

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
  • can I use ? log.log(java.util.logging.Level.SEVERE,e.getMessage(),e); Feb 5, 2013 at 11:26
  • yes, that's for stuff like exceptions that shouldn't happen, i.e. your database connection is down or some file that's supposed to be there isn't.
    – linkerro
    Feb 5, 2013 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.