以表格格式在日志文件中打印哈希图中的数据

Roh*_*han 2 java string collections logging

你好,我想以表格格式打印 hashmap 的数据。下面是我在 hashmap 中插入数据的代码。

 String foldername = item.getParent().substring(item.getParent().lastIndexOf("/") + 1);
                        statusname = foldername + "\t" + _DeployResult.ServerIP + "\t" + _DeployResult.ServerType;
                        if (!Prdeploy.statusmap.containsKey(statusname)) {
                            Prdeploy.statusmap.put(statusname, 0);
                        }
Run Code Online (Sandbox Code Playgroud)

下面是为打印日志文件中的数据而编写的代码:

 logger.info("****************Summary Report****************");
                   logger.info("Folder\tServer\tType\tFailed");

        for (String name: statusmap.keySet()){

            String key =name.toString();
            String value = statusmap.get(name).toString();  

            System.out.println(key + "  " + value);  
            logger.info(key + "\t" + value);
} 
        logger.info("**********************************************");
Run Code Online (Sandbox Code Playgroud)

在这里我得到上面的输出如下:

****************Summary Report****************
Folder  Server  Type    Failed
180_Perl_Scripts            10.5.50.195 SS  0
050_Images              10.5.50.195 SS  0
020_XSL             10.5.50.195 SS  0
srf             10.5.50.195 SS  0
030_XSLT            10.5.50.195 SS  0
bscript 10.5.50.195 SS  0
bscript 10.5.50.195 WS  1
010_StyleSheets             10.5.50.195 SS  0
040_WebTemplates            10.5.50.195 SS  0
050_Images              10.5.50.195 WS  0
060_js_Files            10.5.50.195 SS  0
010_StyleSheets             10.5.50.195 WS  0
**********************************************
Run Code Online (Sandbox Code Playgroud)

但我希望我的输出如下:

    ****************Summary Report****************
Folder                  Server          Type    Failed
180_Perl_Scripts            10.5.50.195 SS  0
050_Images              10.5.50.195 SS  0
020_XSL                     10.5.50.195 SS  0
srf                     10.5.50.195 SS  0
030_XSLT                    10.5.50.195 SS  0
bscript                 10.5.50.195 SS  0
bscript                 10.5.50.195 WS  1
010_StyleSheets             10.5.50.195 SS  0
040_WebTemplates            10.5.50.195 SS  0
050_Images              10.5.50.195 WS  0
060_js_Files            10.5.50.195 SS  0
010_StyleSheets             10.5.50.195 WS  0
**********************************************
Run Code Online (Sandbox Code Playgroud)

在这里我无法输入确切的模式。但我只是想将其转换为表格格式。结构良好。有办法做到这一点吗?请在这里帮助我!

Evg*_*eev 6

尝试这个

String str = String.format("%-10s%-10s%-10s%s", statusMap.get("Folder"), statusMap.get("Server"), statusMap.get("Type"), statusMap.get("Failed"));
logger.info(str);
Run Code Online (Sandbox Code Playgroud)