简体   繁体   中英

print data in hashmap in a tabular format in log file

Hi here i want to print the data of hashmap in the tabular format.Below is my code to insert the data in 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);
                        }

and below is code which is written to print the data in the log file:

 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("**********************************************");

here i am getting the output of above as below:

****************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
**********************************************

but i want my output as below:

    ****************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
**********************************************

Here i am not able to put the exact pattern.But just i want it into tabular format.Well structired. Is there way to do this? Please help me here!

try this

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

You can format your strings using String.format methods.

`logger.info(String.format("%-20s %-20s%-10s%-10s" , "Folder", "Server", "Type", "Failed" ));`

Where %s is a placeholder for you string. The '-' makes the result left-justified. 20 and 10 is the width of the strings

If you only need this for display purposes, that is you don't really need it to be tab characters then it would be easy to create a padRight() or padLeft() methods which would pad the string with spaces

Something like:

String padRight(String inputStr, int length)
{
   while(inputStr.length() <= length)
       inputStr = inputStr + " ";
   return inputStr;
}

This could easily be modified to accept any character you wish to pad with also.

With tab it won't be easy. What you have to understand with tab is that it comes in groups of spaces. I think the default value of a tab is 4 spaces. This works like this.

Start__ middle _end

The group of underscores is one tab. Every underscore is one space. You have Start which is of length 5. So it overlapses by 1. So when you press tab it will go in the position 8 (like two tabs of 4) which is 3 spaces. Same with middle.

What you need to use is the formatter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM