0

I have a project which should print the result on the screen in the console and also to the file. I use spring boot with logback. I can print the result as a table on screen but not as a good format in the log file with logback. Any ideas and suggestions are welcome.

see the output result below.

+------------------+---------------+---------------+--------------+---------------+---------------+--------------+--------------+  
| %-16s | %-13s | %-13s | %-12s | %-13s | %-13s | %-12s | %-12s |%n  
+------------------+---------------+---------------+--------------+---------------+---------------+--------------+--------------+  
| %-16s | %-13s | %-13s | %-12s | %-13s | %-13s | %-12s | %-12s |%n  
| %-16s | %-13s | %-13s | %-12s | %-13s | %-13s | %-12s | %-12s |%n  
| %-16s | %-13s | %-13s | %-12s | %-13s | %-13s | %-12s | %-12s |%n  
| %-16s | %-13s | %-13s | %-12s | %-13s | %-13s | %-12s | %-12s |%n  
| %-16s | %-13s | %-13s | %-12s | %-13s | %-13s | %-12s | %-12s |%n  
| %-16s | %-13s | %-13s | %-12s | %-13s | %-13s | %-12s | %-12s |%n  
+------------------+---------------+---------------+--------------+---------------+---------------+--------------+--------------+ 

Method for printing:

public static void printListAsTable(List<ResultControl> resultcontrolList) {
        String format = "| %-16s | %-13s | %-13s | %-12s | %-13s | %-13s | %-12s | %-12s |%n";
        log("+------------------+---------------+---------------+--------------+---------------+---------------+--------------+--------------+");
        log(format,"Id", "Net", "Mat", "Dat", "Apo", "Nato", "Nano", "Pico");
        log("+------------------+---------------+---------------+--------------+---------------+---------------+--------------+--------------+");
        for (ResultControl result :resultcontrolList ) {
            log(format, result.getId(), result.getNet , result.getMat , result.getDat , result.getApo , result.getNato , result.getNano , result.getPico  );
        }
        log("+------------------+---------------+---------------+--------------+---------------+---------------+--------------+--------------+");
    }



    public static void log(String format, Object... args) {
        System.out.printf(format, args);
        logger.info( format, args);
    }
itro
  • 7,006
  • 27
  • 78
  • 121

1 Answers1

0

I did changed the log method to the following and it works as I expected to be.

    public static void log(String format, Object... args) {
        String formatted = String.format(format, args);
        log( formatted);
    }


    public static void log(String message) {
        logger.info(message);
    }
itro
  • 7,006
  • 27
  • 78
  • 121