user6941415
user6941415

Reputation:

When to use logger during Exceptionhandling?

I am newbie at Java and I don't know when to use logger. For example is it logical when I use logger in following example. How to say "Okay I should use logger here". To be clear, I am not asking which logger should I use, but should I use logger at all?

 public boolean isValid(Object bean, ConstraintValidatorContext ctx) {
        try {
            if (Assert.isNull(bean)) {
                throw new NullPointerException(EXC_MSG_BEAN_NULL);
            }

            String dependentFieldActualValue;
            dependentFieldActualValue = BeanUtils.getProperty(bean, dependentField);
            boolean isActualEqual = stringEquals(dependentFieldValue, dependentFieldActualValue);

            if (isActualEqual == ifInEqualThenValidate) {
                return true; // The condition is not met => Do not validate at all.
            }
            return isTargetValid(bean, ctx); // Perform the actual validation on the target field
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException(e);
        }
    }

Upvotes: 0

Views: 60

Answers (1)

Shubham Sharma
Shubham Sharma

Reputation: 133

Loggers are good practice to use. They are used to find out what went wrong and where without using debugger, or in a production environment. For example, when you have a doubt regarding the value that is coming as a parameter in a function and you want to check that the correct value is being passed, you can log that value at debug level so that you can be sure that the correct value is being passed and if not, then you can rectify that.

Upvotes: 0

Related Questions