2

This seems to be a simple question but I could get it. In one of the interviews it was asked to be "how will you make sure that a person writing java program is putting proper logging messages"

I said that we have to check that during code review. To this he said that there is a way through which you can mandate the logging.

I didnt had the answer. Is there any way to do that?

Any help will be great

1
  • 3
    Impossible to see if the log messages are proper wihout reading them. Stupid question (if they really expected an answer). Commented Sep 11, 2013 at 11:33

4 Answers 4

3

You could write an abstract class containing a logging method. The abstract implementation throws an error. During unit testing, simply check whether the method throws an error and fail the test if it does.

However, this is quite prone to error as your programmer could simply have made a dummy implementation. As of now, it is hard to assess the quality of log messages automatically. For example, consider the two logging messages below:

1) En error occurred while executing methods XY, parameter Z null.
2) Error in crabs take over method XY and annihilate Z with a laser.

If you were to only check for key terms, both would fit the test. So an oracle is hard to come by. The method above ensures that your programmer does some work but it is up to him to add a quality log. You can certainly add other parameters for quality such as length or relevance but all are meaningless if they don't refer to the problem. The same dilemma has plagued the industry with proper error handling for quite some while.

I believe the only concrete and effective tool is code review and employee evaluations.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use jMock to write a mock of your logging class. Then write a unit test of your program, which checks the expectation that it calls the proper logging functions.

Comments

1

I guess you can use PMD toolo to mandate logging

Comments

0

There are two things here.

You can check the Code Coverage to check whether source code of a program is tested or not

Second there are other tools to analyze code if there are any programmatic flaws like PMD

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.