Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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;
share|improve this question

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
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 '13 at 13:11

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.

share|improve this answer
    
can I use ? log.log(java.util.logging.Level.SEVERE,e.getMessage(),e); –  Sridhar Feb 5 '13 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 '13 at 11:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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