0

I am writing a command line java application for a task of converting the content of a file to a json file. In here I want to get the response whether the operation is successful or not printed in the command line. For this I have two options,

  1. To use System.out.println (sout)
  2. To use a logger (eg:- java.util.logging.Logger)

I find the response shown in command line when using sout is more clear since it does not show texts related to logging (as an example INFO: My response message).

On the other hand I got to know that using sout is not a good practice (See Sonar rule java:s106).

I want to know which way is better and why?

Here is my code in the main method with the logger approach.

public static void main(String[] args) {

        try {
            Executor executor = ExecutorFactory.getExecutor(args);
            executor.execute();
        } catch (ExecutorException e) {
            logger.log(Level.INFO, e.getMessage());
        }
    }

Let's say e.getMessage() gives "No matching file found" as the result.

This is what I get as the response in the command line.

INFO: No matching file found

I would like it to be more clean. This is what I expect.

No matching file found

Should I use sout for this purpose or is there another way?

chameerar
  • 322
  • 1
  • 2
  • 8

1 Answers1

0

If you just want to output some information in the commond line ,you can simply use sout. But if you want to display prompt information at different levels, logger is better. In enterprise development, we only use log.info() log.err() Using too much sout will make code redundant. But you said it's just a command line java application, so I think both is ok.