This is an automated email from the ASF dual-hosted git repository.

dahn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/main by this push:
     new 01f1aae  Improve VMWare logs (#5353)
01f1aae is described below

commit 01f1aaee6037a16198b8f7e95eeeaea88f7717d7
Author: SadiJr <[email protected]>
AuthorDate: Wed Dec 22 09:56:00 2021 -0300

    Improve VMWare logs (#5353)
    
    * Improve VMWare logs
    
    * Address reviews
    
    * Address reviews and add more logs
    
    * Improve VMWare logs
    
    * Address reviews
    
    * Address reviews and add more logs
    
    * Address reviews
    
    * Address reviews
    
    * Try fix tests
    
    * Refactor some logs to try fix tests
    
    * Verifying if GSON is the cause of test exceptions
    
    * Fix checkstyle
    
    Co-authored-by: SadiJr <[email protected]>
    Co-authored-by: SadiJr <[email protected]>
---
 .../StorageSubsystemCommandHandlerBase.java        |  10 +
 .../vmware/manager/VmwareHostService.java          |   2 +
 .../vmware/manager/VmwareStorageManagerImpl.java   |  67 +--
 .../hypervisor/vmware/resource/VmwareResource.java | 494 ++++-----------------
 .../VmwareSecondaryStorageResourceHandler.java     |  21 +-
 .../storage/resource/VmwareStorageProcessor.java   | 176 ++------
 .../hypervisor/vmware/mo/VirtualMachineMO.java     |   5 +-
 7 files changed, 174 insertions(+), 601 deletions(-)

diff --git 
a/core/src/main/java/com/cloud/storage/resource/StorageSubsystemCommandHandlerBase.java
 
b/core/src/main/java/com/cloud/storage/resource/StorageSubsystemCommandHandlerBase.java
index 1a66034..7044490 100644
--- 
a/core/src/main/java/com/cloud/storage/resource/StorageSubsystemCommandHandlerBase.java
+++ 
b/core/src/main/java/com/cloud/storage/resource/StorageSubsystemCommandHandlerBase.java
@@ -44,6 +44,7 @@ import com.cloud.agent.api.to.DataTO;
 import com.cloud.agent.api.to.DiskTO;
 import com.cloud.storage.DataStoreRole;
 import com.cloud.storage.Volume;
+import com.google.gson.Gson;
 
 public class StorageSubsystemCommandHandlerBase implements 
StorageSubsystemCommandHandler {
     private static final Logger s_logger = 
Logger.getLogger(StorageSubsystemCommandHandlerBase.class);
@@ -55,6 +56,7 @@ public class StorageSubsystemCommandHandlerBase implements 
StorageSubsystemComma
 
     @Override
     public Answer handleStorageCommands(StorageSubSystemCommand command) {
+        logCommand(command);
         if (command instanceof CopyCommand) {
             return this.execute((CopyCommand)command);
         } else if (command instanceof CreateObjectCommand) {
@@ -170,4 +172,12 @@ public class StorageSubsystemCommandHandlerBase implements 
StorageSubsystemComma
             return processor.dettachVolume(cmd);
         }
     }
+
+    private void logCommand(Command cmd) {
+        try {
+            s_logger.debug(String.format("Executing command %s: [%s].", 
cmd.getClass().getSimpleName(), new Gson().toJson(cmd)));
+        } catch (Exception e) {
+            s_logger.debug(String.format("Executing command %s.", 
cmd.getClass().getSimpleName()));
+        }
+    }
 }
diff --git 
a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/manager/VmwareHostService.java
 
b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/manager/VmwareHostService.java
index ea97a6e..d9ee3c5 100644
--- 
a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/manager/VmwareHostService.java
+++ 
b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/manager/VmwareHostService.java
@@ -29,4 +29,6 @@ public interface VmwareHostService {
     VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd);
 
     String getWorkerName(VmwareContext context, Command cmd, int 
workerSequence, DatastoreMO dsMo) throws Exception;
+
+    String createLogMessageException(Throwable e, Command command);
 }
diff --git 
a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/manager/VmwareStorageManagerImpl.java
 
b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/manager/VmwareStorageManagerImpl.java
index 1451a60..cad4e26 100644
--- 
a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/manager/VmwareStorageManagerImpl.java
+++ 
b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/manager/VmwareStorageManagerImpl.java
@@ -21,7 +21,6 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.OutputStreamWriter;
-import java.rmi.RemoteException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -85,7 +84,6 @@ import com.cloud.storage.Volume;
 import com.cloud.storage.template.OVAProcessor;
 import com.cloud.utils.NumbersUtil;
 import com.cloud.utils.Pair;
-import com.cloud.utils.StringUtils;
 import com.cloud.utils.Ternary;
 import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.utils.script.Script;
@@ -96,6 +94,7 @@ public class VmwareStorageManagerImpl implements 
VmwareStorageManager {
 
     private String _nfsVersion;
 
+
     @Override
     public boolean execute(VmwareHostService hostService, 
CreateEntityDownloadURLCommand cmd) {
         DataTO data = cmd.getData();
@@ -296,13 +295,7 @@ public class VmwareStorageManagerImpl implements 
VmwareStorageManager {
 
             return new PrimaryStorageDownloadAnswer(templateUuidName, 0);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            String msg = "Unable to execute PrimaryStorageDownloadCommand due 
to exception";
-            s_logger.error(msg, e);
-            return new PrimaryStorageDownloadAnswer(msg);
+            return new 
PrimaryStorageDownloadAnswer(hostService.createLogMessageException(e, cmd));
         }
     }
 
@@ -380,22 +373,14 @@ public class VmwareStorageManagerImpl implements 
VmwareStorageManager {
 
                 try {
                     if (workerVm != null) {
-                        // detach volume and destroy worker vm
                         workerVm.detachAllDisksAndDestroy();
                     }
                 } catch (Throwable e) {
-                    s_logger.warn("Failed to destroy worker VM: " + 
workerVMName);
+                    s_logger.warn(String.format("Failed to destroy worker VM 
[%s] due to: [%s].", workerVMName, e.getMessage()), e);
                 }
             }
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            s_logger.error("Unexpecpted exception ", e);
-
-            details = "BackupSnapshotCommand exception: " + 
StringUtils.getExceptionStackInfo(e);
-            return new BackupSnapshotAnswer(cmd, false, details, 
snapshotBackupUuid, true);
+            return new BackupSnapshotAnswer(cmd, false, 
hostService.createLogMessageException(e, cmd), snapshotBackupUuid, true);
         }
 
         return new BackupSnapshotAnswer(cmd, success, details, 
snapshotBackupUuid, true);
@@ -433,14 +418,7 @@ public class VmwareStorageManagerImpl implements 
VmwareStorageManager {
             return new CreatePrivateTemplateAnswer(cmd, true, null, 
result.first(), result.third(), result.second(), cmd.getUniqueName(), 
ImageFormat.OVA);
 
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            s_logger.error("Unexpecpted exception ", e);
-
-            details = "CreatePrivateTemplateFromVolumeCommand exception: " + 
StringUtils.getExceptionStackInfo(e);
-            return new CreatePrivateTemplateAnswer(cmd, false, details);
+            return new CreatePrivateTemplateAnswer(cmd, false, 
hostService.createLogMessageException(e, cmd));
         }
     }
 
@@ -461,14 +439,7 @@ public class VmwareStorageManagerImpl implements 
VmwareStorageManager {
 
             return new CreatePrivateTemplateAnswer(cmd, true, null, 
result.first(), result.third(), result.second(), uniqeName, ImageFormat.OVA);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            s_logger.error("Unexpecpted exception ", e);
-
-            details = "CreatePrivateTemplateFromSnapshotCommand exception: " + 
StringUtils.getExceptionStackInfo(e);
-            return new CreatePrivateTemplateAnswer(cmd, false, details);
+            return new CreatePrivateTemplateAnswer(cmd, false, 
hostService.createLogMessageException(e, cmd));
         }
     }
 
@@ -504,13 +475,7 @@ public class VmwareStorageManagerImpl implements 
VmwareStorageManager {
             }
             return new CopyVolumeAnswer(cmd, true, null, result.first(), 
result.second());
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            String msg = "Unable to execute CopyVolumeCommand due to 
exception";
-            s_logger.error(msg, e);
-            return new CopyVolumeAnswer(cmd, false, "CopyVolumeCommand failed 
due to exception: " + StringUtils.getExceptionStackInfo(e), null, null);
+            return new CopyVolumeAnswer(cmd, false, 
hostService.createLogMessageException(e, cmd), null, null);
         }
     }
 
@@ -543,12 +508,7 @@ public class VmwareStorageManagerImpl implements 
VmwareStorageManager {
                 success = true;
             }
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            s_logger.error("Unexpecpted exception ", e);
-            details = "CreateVolumeFromSnapshotCommand exception: " + 
StringUtils.getExceptionStackInfo(e);
+            details = hostService.createLogMessageException(e, cmd);
         }
 
         return new CreateVolumeFromSnapshotAnswer(cmd, success, details, 
newVolumeName);
@@ -946,8 +906,12 @@ public class VmwareStorageManagerImpl implements 
VmwareStorageManager {
                 Script command = new Script(false, "mkdir", _timeout, 
s_logger);
                 command.add("-p");
                 command.add(exportPath);
-                if (command.execute() != null) {
-                    throw new Exception("unable to prepare snapshot backup 
directory");
+
+                String result = command.execute();
+                if (result != null) {
+                    String errorMessage = String.format("Unable to prepare 
snapshot backup directory: [%s] due to [%s].", exportPath, result);
+                    s_logger.error(errorMessage);
+                    throw new Exception(errorMessage);
                 }
             }
         }
@@ -968,7 +932,7 @@ public class VmwareStorageManagerImpl implements 
VmwareStorageManager {
                 vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, 
volumeDeviceInfo.second(), 
VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()), 
vmxFormattedVirtualHardwareVersion);
                 clonedVm = 
vmMo.getRunningHost().findVmOnHyperHost(workerVmName);
                 if (clonedVm == null) {
-                    String msg = "Unable to create dummy VM to export volume. 
volume path: " + volumePath;
+                    String msg = String.format("Unable to create dummy VM to 
export volume. volume path: [%s].", volumePath);
                     s_logger.error(msg);
                     throw new Exception(msg);
                 }
@@ -1033,7 +997,6 @@ public class VmwareStorageManagerImpl implements 
VmwareStorageManager {
                 vmMo.removeSnapshot(exportName, false);
             }
             if (workerVm != null) {
-                //detach volume and destroy worker vm
                 workerVm.detachAllDisksAndDestroy();
             }
         }
diff --git 
a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java
 
b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java
index 0dfb465..cce875e 100644
--- 
a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java
+++ 
b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java
@@ -16,7 +16,6 @@
 // under the License.
 package com.cloud.hypervisor.vmware.resource;
 
-import static com.cloud.utils.HumanReadableJson.getHumanReadableBytesJson;
 import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
 
 import java.io.File;
@@ -364,6 +363,7 @@ import 
com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec;
 public class VmwareResource implements StoragePoolResource, ServerResource, 
VmwareHostService, VirtualRouterDeployer {
     private static final Logger s_logger = 
Logger.getLogger(VmwareResource.class);
     public static final String VMDK_EXTENSION = ".vmdk";
+    private static final String EXECUTING_RESOURCE_COMMAND = "Executing 
resource command %s: [%s].";
 
     private static final Random RANDOM = new Random(System.nanoTime());
 
@@ -452,10 +452,7 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
     @Override
     public Answer executeRequest(Command cmd) {
-
-        if (s_logger.isTraceEnabled())
-            s_logger.trace("Begin executeRequest(), cmd: " + 
cmd.getClass().getSimpleName());
-
+        logCommand(cmd);
         Answer answer = null;
         NDC.push(getCommandLogTitle(cmd));
         try {
@@ -1006,10 +1003,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(CheckNetworkCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource CheckNetworkCommand " + 
_gson.toJson(cmd));
-        }
-
         // TODO setup portgroup for private network needs to be done here now
         return new CheckNetworkAnswer(cmd, true, "Network Setup check by names 
is done");
     }
@@ -1018,9 +1011,7 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
         if (cmd.isForVpc()) {
             return VPCNetworkUsage(cmd);
         }
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource NetworkUsageCommand " + 
_gson.toJson(cmd));
-        }
+
         if (cmd.getOption() != null && cmd.getOption().equals("create")) {
             String result = networkUsage(cmd.getPrivateIP(), "create", null);
             NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, result, 
0L, 0L);
@@ -1263,10 +1254,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     private PlugNicAnswer execute(PlugNicCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource PlugNicCommand " + 
_gson.toJson(cmd));
-        }
-
         getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
         VmwareContext context = getServiceContext();
         try {
@@ -1347,10 +1334,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     private ReplugNicAnswer execute(ReplugNicCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource ReplugNicCommand " + 
_gson.toJson(cmd));
-        }
-
         getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
         VmwareContext context = getServiceContext();
         try {
@@ -1427,10 +1410,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     private UnPlugNicAnswer execute(UnPlugNicCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource UnPlugNicCommand " + 
_gson.toJson(cmd));
-        }
-
         VmwareContext context = getServiceContext();
         try {
             VmwareHypervisorHost hyperHost = getHyperHost(context);
@@ -1671,14 +1650,15 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
             s_logger.debug("Ping command port, " + privateIp + ":" + cmdPort);
         }
 
+        String errorMessage = "Can not ping System VM [%s], due to: [%s].";
         try {
             String result = connect(cmd.getName(), privateIp, cmdPort);
             if (result != null) {
-                s_logger.error("Can not ping System vm " + vmName + "due to:" 
+ result);
-                return new CheckSshAnswer(cmd, "Can not ping System vm " + 
vmName + "due to:" + result);
+                s_logger.error(String.format(errorMessage, vmName, result));
+                return new CheckSshAnswer(cmd, String.format(errorMessage, 
vmName, result));
             }
         } catch (Exception e) {
-            s_logger.error("Can not ping System vm " + vmName + "due to 
exception");
+            s_logger.error(String.format(errorMessage, vmName, 
e.getMessage()), e);
             return new CheckSshAnswer(cmd, e);
         }
 
@@ -1731,7 +1711,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected ScaleVmAnswer execute(ScaleVmCommand cmd) {
-
         VmwareContext context = getServiceContext();
         VirtualMachineTO vmSpec = cmd.getVirtualMachine();
         try {
@@ -1771,8 +1750,8 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                 throw new Exception("Unable to execute ScaleVmCommand");
             }
         } catch (Exception e) {
-            s_logger.error("Unexpected exception: ", e);
-            return new ScaleVmAnswer(cmd, false, "Unable to execute 
ScaleVmCommand due to " + e.toString());
+            s_logger.error(String.format("ScaleVmCommand failed due to: 
[%s].", VmwareHelper.getExceptionMessage(e)), e);
+            return new ScaleVmAnswer(cmd, false, String.format("Unable to 
execute ScaleVmCommand due to: [%s].", e.toString()));
         }
         return new ScaleVmAnswer(cmd, true, null);
     }
@@ -1835,10 +1814,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected StartAnswer execute(StartCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource StartCommand: " + 
getHumanReadableBytesJson(_gson.toJson(cmd)));
-        }
-
         VirtualMachineTO vmSpec = cmd.getVirtualMachine();
         boolean vmAlreadyExistsInVcenter = false;
 
@@ -1984,7 +1959,8 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                             }
                         }
 
-                        boolean vmFolderExists = 
dsRootVolumeIsOn.folderExists(String.format("[%s]", 
dsRootVolumeIsOn.getName()), vmNameOnVcenter);                        String 
vmxFileFullPath = dsRootVolumeIsOn.searchFileInSubFolders(vmNameOnVcenter + 
".vmx", false, VmwareManager.s_vmwareSearchExcludeFolder.value());
+                        boolean vmFolderExists = 
dsRootVolumeIsOn.folderExists(String.format("[%s]", 
dsRootVolumeIsOn.getName()), vmNameOnVcenter);
+                        String vmxFileFullPath = 
dsRootVolumeIsOn.searchFileInSubFolders(vmNameOnVcenter + ".vmx", false, 
VmwareManager.s_vmwareSearchExcludeFolder.value());
                         if (vmFolderExists && vmxFileFullPath != null) { // VM 
can be registered only if .vmx is present.
                             registerVm(vmNameOnVcenter, dsRootVolumeIsOn);
                             vmMo = 
hyperHost.findVmOnHyperHost(vmInternalCSName);
@@ -2502,33 +2478,23 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
             return startAnswer;
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String msg = "StartCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.warn(msg, e);
-            StartAnswer startAnswer = new StartAnswer(cmd, msg);
+            StartAnswer startAnswer = new StartAnswer(cmd, 
createLogMessageException(e, cmd));
             if (vmAlreadyExistsInVcenter) {
                 startAnswer.setContextParam("stopRetry", "true");
             }
 
-            // Since VM start failed, if there was an existing VM in a 
different cluster that was unregistered, register it back.
             if (existingVmName != null && existingVmFileInfo != null) {
-                s_logger.debug("Since VM start failed, registering back an 
existing VM: " + existingVmName + " that was unregistered");
+                s_logger.debug(String.format("Since VM start failed, 
registering back an existing VM: [%s] that was unregistered.", existingVmName));
                 try {
                     DatastoreFile fileInDatastore = new 
DatastoreFile(existingVmFileInfo.getVmPathName());
                     DatastoreMO existingVmDsMo = new 
DatastoreMO(dcMo.getContext(), 
dcMo.findDatastore(fileInDatastore.getDatastoreName()));
                     registerVm(existingVmName, existingVmDsMo);
                 } catch (Exception ex) {
-                    String message = "Failed to register an existing VM: " + 
existingVmName + " due to " + VmwareHelper.getExceptionMessage(ex);
-                    s_logger.warn(message, ex);
+                    String message = String.format("Failed to register an 
existing VM: [%s] due to [%s].", existingVmName, 
VmwareHelper.getExceptionMessage(ex));
+                    s_logger.error(message, ex);
                 }
             }
-
             return startAnswer;
-        } finally {
         }
     }
 
@@ -3935,10 +3901,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(ReadyCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource ReadyCommand: " + 
_gson.toJson(cmd));
-        }
-
         try {
             VmwareContext context = getServiceContext();
             VmwareHypervisorHost hyperHost = getHyperHost(context);
@@ -3954,10 +3916,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(GetHostStatsCommand cmd) {
-        if (s_logger.isTraceEnabled()) {
-            s_logger.trace("Executing resource GetHostStatsCommand: " + 
_gson.toJson(cmd));
-        }
-
         VmwareContext context = getServiceContext();
         VmwareHypervisorHost hyperHost = getHyperHost(context);
 
@@ -3966,17 +3924,12 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
         try {
             HostStatsEntry entry = getHyperHostStats(hyperHost);
             if (entry != null) {
+                s_logger.debug(String.format("Host stats response from 
hypervisor is: [%s].", _gson.toJson(entry)));
                 entry.setHostId(cmd.getHostId());
                 answer = new GetHostStatsAnswer(cmd, entry);
             }
         } catch (Exception e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String msg = "Unable to execute GetHostStatsCommand due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg, e);
+            s_logger.error(createLogMessageException(e, cmd), e);
         }
 
         if (s_logger.isTraceEnabled()) {
@@ -3987,10 +3940,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(GetVmStatsCommand cmd) {
-        if (s_logger.isTraceEnabled()) {
-            s_logger.trace("Executing resource GetVmStatsCommand: " + 
_gson.toJson(cmd));
-        }
-
         HashMap<String, VmStatsEntry> vmStatsMap = null;
 
         try {
@@ -4008,24 +3957,15 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                 }
             }
 
-            if (vmNames != null) {
+            if (CollectionUtils.isNotEmpty(vmNames)) {
                 vmStatsMap = getVmStats(vmNames);
             }
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            s_logger.error("Unable to execute GetVmStatsCommand due to : " + 
VmwareHelper.getExceptionMessage(e), e);
+            createLogMessageException(e, cmd);
         }
 
-        Answer answer = new GetVmStatsAnswer(cmd, vmStatsMap);
-
-        if (s_logger.isTraceEnabled()) {
-            s_logger.trace("Report GetVmStatsAnswer: " + _gson.toJson(answer));
-        }
-        return answer;
+        s_logger.debug(String.format("VM Stats Map is: [%s].", 
_gson.toJson(vmStatsMap)));
+        return new GetVmStatsAnswer(cmd, vmStatsMap);
     }
 
     protected Answer execute(GetVmDiskStatsCommand cmd) {
@@ -4138,15 +4078,17 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                     }
                     diskStats.add(new VmDiskStatsEntry(vmName, 
VmwareHelper.getDiskDeviceFileName(disk), writeReq, readReq, writeBytes, 
readBytes));
                 }
-                if (diskStats.size() > 0) {
+                if (CollectionUtils.isNotEmpty(diskStats)) {
                     vmStatsMap.put(vmName, diskStats);
                 }
             }
-            if (vmStatsMap.size() > 0) {
+
+            s_logger.debug(String.format("VM Disks Maps is: [%s].", 
_gson.toJson(vmStatsMap)));
+            if (MapUtils.isNotEmpty(vmStatsMap)) {
                 return new GetVmDiskStatsAnswer(cmd, "", cmd.getHostName(), 
vmStatsMap);
             }
         } catch (Exception e) {
-            s_logger.error("Unable to execute GetVmDiskStatsCommand due to " + 
VmwareHelper.getExceptionMessage(e), e);
+            s_logger.error(String.format("Unable to execute 
GetVmDiskStatsCommand due to [%s].", VmwareHelper.getExceptionMessage(e)), e);
         }
         return new GetVmDiskStatsAnswer(cmd, null, null, null);
     }
@@ -4192,40 +4134,28 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                     }
                 }
             }
+            s_logger.debug(String.format("Volume Stats Entry is: [%s].", 
_gson.toJson(statEntry)));
             return new GetVolumeStatsAnswer(cmd, "", statEntry);
         } catch (Exception e) {
-            s_logger.info("VOLSTAT GetVolumeStatsCommand failed " + 
e.getMessage());
+            s_logger.error(String.format("VOLSTAT GetVolumeStatsCommand failed 
due to [%s].", VmwareHelper.getExceptionMessage(e)), e);
         }
 
         return new GetVolumeStatsAnswer(cmd, "", null);
     }
 
     protected Answer execute(CheckHealthCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource CheckHealthCommand: " + 
_gson.toJson(cmd));
-        }
-
         try {
             VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
             if (hyperHost.isHyperHostConnected()) {
                 return new CheckHealthAnswer(cmd, true);
             }
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            s_logger.error("Unable to execute CheckHealthCommand due to " + 
VmwareHelper.getExceptionMessage(e), e);
+            createLogMessageException(e, cmd);
         }
         return new CheckHealthAnswer(cmd, false);
     }
 
     protected Answer execute(StopCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource StopCommand: " + 
_gson.toJson(cmd));
-        }
-
         // In the stop command, we're passed in the name of the VM as seen by 
cloudstack,
         // i.e., i-x-y. This is the internal VM name.
         VmwareContext context = getServiceContext();
@@ -4270,28 +4200,16 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                 } finally {
                 }
             } else {
-
                 String msg = "VM " + cmd.getVmName() + " is no longer on the 
expected host in vSphere";
                 s_logger.info(msg);
                 return new StopAnswer(cmd, msg, true);
             }
         } catch (Exception e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String msg = "StopCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg);
-            return new StopAnswer(cmd, msg, false);
+            return new StopAnswer(cmd, createLogMessageException(e, cmd), 
false);
         }
     }
 
     protected Answer execute(RebootRouterCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource RebootRouterCommand: " + 
_gson.toJson(cmd));
-        }
-
         RebootAnswer answer = (RebootAnswer) execute((RebootCommand) cmd);
 
         if (answer.getResult()) {
@@ -4307,10 +4225,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(RebootCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource RebootCommand: " + 
getHumanReadableBytesJson(_gson.toJson(cmd)));
-        }
-
         boolean toolsInstallerMounted = false;
         VirtualMachineMO vmMo = null;
         VmwareContext context = getServiceContext();
@@ -4349,21 +4263,14 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                 return new RebootAnswer(cmd, msg, false);
             }
         } catch (Exception e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String msg = "RebootCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg);
-            return new RebootAnswer(cmd, msg, false);
+            return new RebootAnswer(cmd, createLogMessageException(e, cmd), 
false);
         } finally {
             if (toolsInstallerMounted) {
                 try {
                     vmMo.mountToolsInstaller();
-                    s_logger.debug("Successfully re-mounted vmware tools 
installer for :[" + cmd.getVmName() + "]");
+                    s_logger.debug(String.format("Successfully re-mounted 
vmware tools installer for :[%s].", cmd.getVmName()));
                 } catch (Exception e) {
-                    s_logger.warn("Unabled to re-mount vmware tools installer 
for :[" + cmd.getVmName() + "]");
+                    s_logger.error(String.format("Unabled to re-mount vmware 
tools installer for: [%s].", cmd.getVmName()), e);
                 }
             }
         }
@@ -4397,10 +4304,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(CheckVirtualMachineCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource CheckVirtualMachineCommand: " + 
_gson.toJson(cmd));
-        }
-
         final String vmName = cmd.getVmName();
         PowerState powerState = PowerState.PowerUnknown;
         Integer vncPort = null;
@@ -4419,26 +4322,13 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
             }
 
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-            s_logger.error("Unexpected exception: " + 
VmwareHelper.getExceptionMessage(e), e);
-
+            createLogMessageException(e, cmd);
             return new CheckVirtualMachineAnswer(cmd, powerState, vncPort);
         }
     }
 
     protected Answer execute(PrepareForMigrationCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource PrepareForMigrationCommand: " + 
getHumanReadableBytesJson(_gson.toJson(cmd)));
-        }
-
         VirtualMachineTO vm = cmd.getVirtualMachine();
-        if (s_logger.isDebugEnabled()) {
-            s_logger.debug("Preparing host for migrating " + vm);
-        }
-
         final String vmName = vm.getName();
         try {
             VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
@@ -4485,25 +4375,11 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
             }
             return new PrepareForMigrationAnswer(cmd);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String msg = "Unexpected exception " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg, e);
-            return new PrepareForMigrationAnswer(cmd, msg);
+            return new PrepareForMigrationAnswer(cmd, 
createLogMessageException(e, cmd));
         }
     }
 
     protected Answer execute(MigrateVmToPoolCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info(String.format("Executing MigrateVmToPoolCommand %s", 
cmd.getVmName()));
-            if (s_logger.isDebugEnabled()) {
-                s_logger.debug("MigrateVmToPoolCommand: " + _gson.toJson(cmd));
-            }
-        }
-
         final String vmName = cmd.getVmName();
 
         VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
@@ -4643,10 +4519,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(MigrateCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource MigrateCommand: " + 
getHumanReadableBytesJson(_gson.toJson(cmd)));
-        }
-
         final String vmName = cmd.getVmName();
         try {
             VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
@@ -4673,22 +4545,11 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
             return new MigrateAnswer(cmd, true, "migration succeeded", null);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String msg = "MigrationCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.warn(msg, e);
-            return new MigrateAnswer(cmd, false, msg, null);
+            return new MigrateAnswer(cmd, false, createLogMessageException(e, 
cmd), null);
         }
     }
 
     protected Answer execute(MigrateWithStorageCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource MigrateWithStorageCommand: " + 
getHumanReadableBytesJson(_gson.toJson(cmd)));
-        }
-
         final VirtualMachineTO vmTo = cmd.getVirtualMachine();
         final List<Pair<VolumeTO, StorageFilerTO>> volToFiler = 
cmd.getVolumeToFilerAsList();
         final String targetHost = cmd.getTargetHost();
@@ -4868,10 +4729,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
         StorageFilerTO poolTo = cmd.getPool();
         VolumeObjectTO volumeObjectTO = (VolumeObjectTO)cmd.getSrcData();
 
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource MigrateVolumeCommand: " + 
_gson.toJson(cmd));
-        }
-
         String vmName = cmd.getAttachedVmName();
 
         VirtualMachineMO vmMo = null;
@@ -5069,10 +4926,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(ModifyStoragePoolCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource ModifyStoragePoolCommand: " + 
_gson.toJson(cmd));
-        }
-
         try {
             VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
             StorageFilerTO pool = cmd.getPool();
@@ -5149,24 +5002,12 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
             return answer;
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-
-                invalidateServiceContext();
-            }
-
-            String msg = "ModifyStoragePoolCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-
-            s_logger.error(msg, e);
-
-            return new Answer(cmd, false, msg);
+            return new Answer(cmd, false, createLogMessageException(e, cmd));
         }
     }
 
     protected Answer execute(GetStoragePoolCapabilitiesCommand cmd) {
-
         try {
-
             VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
 
             HostMO host = (HostMO) hyperHost;
@@ -5193,18 +5034,9 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
             return answer;
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-
-                invalidateServiceContext();
-            }
-
-            String msg = "GetStoragePoolCapabilitiesCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-
-            s_logger.error(msg, e);
             GetStoragePoolCapabilitiesAnswer answer = new 
GetStoragePoolCapabilitiesAnswer(cmd);
             answer.setResult(false);
-            answer.setDetails(msg);
+            answer.setDetails(createLogMessageException(e, cmd));
             return answer;
         }
     }
@@ -5232,10 +5064,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(DeleteStoragePoolCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource DeleteStoragePoolCommand: " + 
_gson.toJson(cmd));
-        }
-
         try {
             if (cmd.getRemoveDatastore()) {
                 _storageProcessor.handleDatastoreAndVmdkDetach(cmd, 
cmd.getDetails().get(DeleteStoragePoolCommand.DATASTORE_NAME),
@@ -5260,7 +5088,8 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
             }
 
             StorageFilerTO pool = cmd.getPool();
-            String msg = "DeleteStoragePoolCommand (pool: " + pool.getHost() + 
", path: " + pool.getPath() + ") failed due to " + 
VmwareHelper.getExceptionMessage(e);
+            String msg = String.format("DeleteStoragePoolCommand (pool: [%s], 
path: [%s]) failed due to [%s].", pool.getHost(), pool.getPath(), 
VmwareHelper.getExceptionMessage(e));
+            s_logger.error(msg, e);
 
             return new Answer(cmd, false, msg);
         }
@@ -5275,10 +5104,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected AttachIsoAnswer execute(AttachIsoCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource AttachIsoCommand: " + 
_gson.toJson(cmd));
-        }
-
         try {
             VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
             VirtualMachineMO vmMo = 
hyperHost.findVmOnHyperHost(cmd.getVmName());
@@ -5346,15 +5171,9 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                 invalidateServiceContext();
             }
 
-            if (cmd.isAttach()) {
-                String msg = "AttachIsoCommand(attach) failed due to " + 
VmwareHelper.getExceptionMessage(e);
-                s_logger.error(msg, e);
-                return new AttachIsoAnswer(cmd, false, msg);
-            } else {
-                String msg = "AttachIsoCommand(detach) failed due to " + 
VmwareHelper.getExceptionMessage(e);
-                s_logger.warn(msg, e);
-                return new AttachIsoAnswer(cmd, false, msg);
-            }
+            String message = String.format("AttachIsoCommand(%s) failed due to 
[%s].", cmd.isAttach()? "attach" : "detach", 
VmwareHelper.getExceptionMessage(e));
+            s_logger.error(message, e);
+            return new AttachIsoAnswer(cmd, false, message);
         }
     }
 
@@ -5394,10 +5213,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(ValidateSnapshotCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource ValidateSnapshotCommand: " + 
_gson.toJson(cmd));
-        }
-
         // the command is no longer available
         String expectedSnapshotBackupUuid = null;
         String actualSnapshotBackupUuid = null;
@@ -5407,10 +5222,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(ManageSnapshotCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource ManageSnapshotCommand: " + 
_gson.toJson(cmd));
-        }
-
         long snapshotId = cmd.getSnapshotId();
 
         /*
@@ -5439,24 +5250,13 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(BackupSnapshotCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource BackupSnapshotCommand: " + 
_gson.toJson(cmd));
-        }
-
         try {
             VmwareContext context = getServiceContext();
             VmwareManager mgr = 
context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
 
             return mgr.getStorageManager().execute(this, cmd);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String details = "BackupSnapshotCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(details, e);
-            return new BackupSnapshotAnswer(cmd, false, details, null, true);
+            return new BackupSnapshotAnswer(cmd, false, 
createLogMessageException(e, cmd), null, true);
         }
     }
 
@@ -5467,7 +5267,7 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
             return mgr.getStorageManager().execute(this, cmd);
         } catch (Exception e) {
-            e.printStackTrace();
+            createLogMessageException(e, cmd);
             return new CreateVMSnapshotAnswer(cmd, false, "");
         }
     }
@@ -5479,7 +5279,7 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
             return mgr.getStorageManager().execute(this, cmd);
         } catch (Exception e) {
-            e.printStackTrace();
+            createLogMessageException(e, cmd);
             return new DeleteVMSnapshotAnswer(cmd, false, "");
         }
     }
@@ -5490,16 +5290,12 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
             VmwareManager mgr = 
context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
             return mgr.getStorageManager().execute(this, cmd);
         } catch (Exception e) {
-            e.printStackTrace();
+            createLogMessageException(e, cmd);
             return new RevertToVMSnapshotAnswer(cmd, false, "");
         }
     }
 
     protected Answer execute(CreateVolumeFromSnapshotCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource CreateVolumeFromSnapshotCommand: 
" + _gson.toJson(cmd));
-        }
-
         String details = null;
         boolean success = false;
         String newVolumeName = UUID.randomUUID().toString();
@@ -5509,23 +5305,13 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
             VmwareManager mgr = 
context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
             return mgr.getStorageManager().execute(this, cmd);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            details = "CreateVolumeFromSnapshotCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(details, e);
+            details = createLogMessageException(e, cmd);
         }
 
         return new CreateVolumeFromSnapshotAnswer(cmd, success, details, 
newVolumeName);
     }
 
     protected Answer execute(CreatePrivateTemplateFromVolumeCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource 
CreatePrivateTemplateFromVolumeCommand: " + _gson.toJson(cmd));
-        }
-
         try {
             VmwareContext context = getServiceContext();
             VmwareManager mgr = 
context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
@@ -5533,14 +5319,7 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
             return mgr.getStorageManager().execute(this, cmd);
 
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String details = "CreatePrivateTemplateFromVolumeCommand failed 
due to " + VmwareHelper.getExceptionMessage(e);
-            s_logger.error(details, e);
-            return new CreatePrivateTemplateAnswer(cmd, false, details);
+            return new CreatePrivateTemplateAnswer(cmd, false, 
createLogMessageException(e, cmd));
         }
     }
 
@@ -5549,31 +5328,16 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(CreatePrivateTemplateFromSnapshotCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource 
CreatePrivateTemplateFromSnapshotCommand: " + _gson.toJson(cmd));
-        }
-
         try {
             VmwareManager mgr = 
getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
             return mgr.getStorageManager().execute(this, cmd);
 
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String details = "CreatePrivateTemplateFromSnapshotCommand failed 
due to " + VmwareHelper.getExceptionMessage(e);
-            s_logger.error(details, e);
-            return new CreatePrivateTemplateAnswer(cmd, false, details);
+            return new CreatePrivateTemplateAnswer(cmd, false, 
createLogMessageException(e, cmd));
         }
     }
 
     protected Answer execute(GetStorageStatsCommand cmd) {
-        if (s_logger.isTraceEnabled()) {
-            s_logger.trace("Executing resource GetStorageStatsCommand: " + 
_gson.toJson(cmd));
-        }
-
         try {
             VmwareContext context = getServiceContext();
             VmwareHypervisorHost hyperHost = getHyperHost(context);
@@ -5596,10 +5360,8 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
                 long used = capacity - free;
 
-                if (s_logger.isDebugEnabled()) {
-                    s_logger.debug("Datastore summary info, storageId: " + 
cmd.getStorageId() + ", localPath: " + cmd.getLocalPath() + ", poolType: " + 
cmd.getPooltype()
-                            + ", capacity: " + toHumanReadableSize(capacity) + 
", free: " + toHumanReadableSize(free) + ", used: " + 
toHumanReadableSize(used));
-                }
+                s_logger.debug(String.format("Datastore summary info: 
[storageId: %s, ], localPath: %s, poolType: %s, capacity: %s, free: %s, used: 
%s].", cmd.getStorageId(),
+                        cmd.getLocalPath(), cmd.getPooltype(), 
toHumanReadableSize(capacity), toHumanReadableSize(free), 
toHumanReadableSize(used)));
 
                 if (capacity <= 0) {
                     s_logger.warn("Something is wrong with vSphere NFS 
datastore, rebooting ESX(ESXi) host should help");
@@ -5607,8 +5369,8 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
                 return new GetStorageStatsAnswer(cmd, capacity, used);
             } else {
-                String msg = "Could not find datastore for 
GetStorageStatsCommand storageId : " + cmd.getStorageId() + ", localPath: " + 
cmd.getLocalPath() + ", poolType: "
-                        + cmd.getPooltype();
+                String msg = String.format("Could not find datastore for 
GetStorageStatsCommand: [storageId: %s, localPath: %s, poolType: %s].",
+                        cmd.getStorageId(), cmd.getLocalPath(), 
cmd.getPooltype());
 
                 s_logger.error(msg);
                 return new GetStorageStatsAnswer(cmd, msg);
@@ -5619,18 +5381,13 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                 invalidateServiceContext();
             }
 
-            String msg = "Unable to execute GetStorageStatsCommand(storageId : 
" + cmd.getStorageId() + ", localPath: " + cmd.getLocalPath() + ", poolType: " 
+ cmd.getPooltype()
-                    + ") due to " + VmwareHelper.getExceptionMessage(e);
+            String msg = String.format("Unable to execute 
GetStorageStatsCommand(storageId : [%s], localPath: [%s], poolType: [%s]) due 
to [%s]", cmd.getStorageId(), cmd.getLocalPath(), cmd.getPooltype(), 
VmwareHelper.getExceptionMessage(e));
             s_logger.error(msg, e);
             return new GetStorageStatsAnswer(cmd, msg);
         }
     }
 
     protected Answer execute(GetVncPortCommand cmd) {
-        if (s_logger.isTraceEnabled()) {
-            s_logger.trace("Executing resource GetVncPortCommand: " + 
_gson.toJson(cmd));
-        }
-
         try {
             VmwareContext context = getServiceContext();
             VmwareHypervisorHost hyperHost = getHyperHost(context);
@@ -5657,38 +5414,19 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
             }
             return new GetVncPortAnswer(cmd, portInfo.first(), 
portInfo.second());
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String msg = "GetVncPortCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg, e);
-            return new GetVncPortAnswer(cmd, msg);
+            return new GetVncPortAnswer(cmd, createLogMessageException(e, 
cmd));
         }
     }
 
     protected Answer execute(SetupCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource SetupCommand: " + 
_gson.toJson(cmd));
-        }
-
         return new SetupAnswer(cmd, false);
     }
 
     protected Answer execute(MaintainCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource MaintainCommand: " + 
_gson.toJson(cmd));
-        }
-
         return new MaintainAnswer(cmd, "Put host in maintaince");
     }
 
     protected Answer execute(PingTestCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource PingTestCommand: " + 
_gson.toJson(cmd));
-        }
-
         String controlIp = cmd.getRouterIp();
         if (controlIp != null) {
             String args = " -c 1 -n -q " + cmd.getPrivateIp();
@@ -5729,27 +5467,15 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(CheckOnHostCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource CheckOnHostCommand: " + 
_gson.toJson(cmd));
-        }
-
         return new CheckOnHostAnswer(cmd, null, "Not Implmeneted");
     }
 
     protected Answer execute(ModifySshKeysCommand cmd) {
-        //do not log the command contents for this command. do NOT log the ssh 
keys
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource ModifySshKeysCommand.");
-        }
-
+        s_logger.debug(String.format("Executing resource command %s.", 
cmd.getClass().getSimpleName()));
         return new Answer(cmd);
     }
 
     protected Answer execute(GetVmIpAddressCommand cmd) {
-        if (s_logger.isTraceEnabled()) {
-            s_logger.trace("Executing resource command GetVmIpAddressCommand: 
" + _gson.toJson(cmd));
-        }
-
         String details = "Unable to find IP Address of VM. ";
         String vmName = cmd.getVmName();
         boolean result = false;
@@ -5783,11 +5509,8 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                 s_logger.info(details);
             }
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-            details += "Encountered exception : " + 
VmwareHelper.getExceptionMessage(e);
+            createLogMessageException(e, cmd);
+            details = String.format("%s. Encountered exception: [%s].", 
details,  VmwareHelper.getExceptionMessage(e));
             s_logger.error(details);
         }
 
@@ -5800,23 +5523,12 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
     @Override
     public PrimaryStorageDownloadAnswer execute(PrimaryStorageDownloadCommand 
cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource PrimaryStorageDownloadCommand: " 
+ _gson.toJson(cmd));
-        }
-
         try {
             VmwareContext context = getServiceContext();
             VmwareManager mgr = 
context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
             return (PrimaryStorageDownloadAnswer) 
mgr.getStorageManager().execute(this, cmd);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String msg = "PrimaryStorageDownloadCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg, e);
-            return new PrimaryStorageDownloadAnswer(msg);
+            return new 
PrimaryStorageDownloadAnswer(createLogMessageException(e, cmd));
         }
     }
 
@@ -5829,10 +5541,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     protected Answer execute(UnregisterVMCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource UnregisterVMCommand: " + 
_gson.toJson(cmd));
-        }
-
         VmwareContext context = getServiceContext();
         VmwareHypervisorHost hyperHost = getHyperHost(context);
         try {
@@ -5859,14 +5567,7 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                 return new Answer(cmd, true, msg);
             }
         } catch (Exception e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String msg = "UnregisterVMCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg);
-            return new Answer(cmd, false, msg);
+            return new Answer(cmd, false, createLogMessageException(e, cmd));
         }
     }
 
@@ -5880,8 +5581,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
      * @return
      */
     protected Answer execute(UnregisterNicCommand cmd) {
-        s_logger.info("Executing resource UnregisterNicCommand: " + 
_gson.toJson(cmd));
-
         if (_guestTrafficInfo == null) {
             return new Answer(cmd, false, "No Guest Traffic Info found, unable 
to determine where to clean up");
         }
@@ -5912,14 +5611,7 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
             }
             return new Answer(cmd, true, "Unregistered resources for NIC " + 
cmd.getNicUuid());
         } catch (Exception e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String msg = "UnregisterVMCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg);
-            return new Answer(cmd, false, msg);
+            return new Answer(cmd, false, createLogMessageException(e, cmd));
         }
     }
 
@@ -5975,23 +5667,12 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
     @Override
     public CopyVolumeAnswer execute(CopyVolumeCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource CopyVolumeCommand: " + 
_gson.toJson(cmd));
-        }
-
         try {
             VmwareContext context = getServiceContext();
             VmwareManager mgr = 
context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
             return (CopyVolumeAnswer) mgr.getStorageManager().execute(this, 
cmd);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext();
-            }
-
-            String msg = "CopyVolumeCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg, e);
-            return new CopyVolumeAnswer(cmd, false, msg, null, null);
+            return new CopyVolumeAnswer(cmd, false, 
createLogMessageException(e, cmd), null, null);
         }
     }
 
@@ -6681,7 +6362,7 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
                                     for (final Long value : values) {
                                         sum += value;
                                     }
-                                    double avg = sum / values.size();
+                                    double avg = sum / (values.size() * 1f);
                                     if (series.getId().getCounterId() == 
rxPerfCounterInfo.getKey()) {
                                         networkReadKBs = avg;
                                     }
@@ -7105,10 +6786,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
 
     @Override
     public Answer execute(DestroyCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource DestroyCommand to evict template 
from storage pool: " + getHumanReadableBytesJson(_gson.toJson(cmd)));
-        }
-
         try {
             VmwareContext context = getServiceContext(null);
             VmwareHypervisorHost hyperHost = getHyperHost(context, null);
@@ -7132,14 +6809,7 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
             return new Answer(cmd, true, "Success");
 
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                invalidateServiceContext(null);
-            }
-
-            String msg = "DestroyCommand failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg, e);
-            return new Answer(cmd, false, msg);
+            return new Answer(cmd, false, createLogMessageException(e, cmd));
         }
     }
 
@@ -7458,10 +7128,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     private Answer execute(GetUnmanagedInstancesCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource GetUnmanagedInstancesCommand " + 
_gson.toJson(cmd));
-        }
-
         VmwareContext context = getServiceContext();
         HashMap<String, UnmanagedInstanceTO> unmanagedInstances = new 
HashMap<>();
         try {
@@ -7498,18 +7164,18 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     private Answer execute(PrepareUnmanageVMInstanceCommand cmd) {
-        s_logger.debug("Verify VMware instance: " + cmd.getInstanceName() + " 
is available before unmanaging VM");
         VmwareContext context = getServiceContext();
         VmwareHypervisorHost hyperHost = getHyperHost(context);
         String instanceName = cmd.getInstanceName();
 
         try {
+            s_logger.debug(String.format("Verify if VMware instance: [%s] is 
available before unmanaging VM.", cmd.getInstanceName()));
+
             ManagedObjectReference  dcMor = hyperHost.getHyperHostDatacenter();
             DatacenterMO dataCenterMo = new DatacenterMO(getServiceContext(), 
dcMor);
             VirtualMachineMO vm = dataCenterMo.findVm(instanceName);
             if (vm == null) {
-                return new PrepareUnmanageVMInstanceAnswer(cmd, false, "Cannot 
find VM with name " + instanceName +
-                        " in datacenter " + dataCenterMo.getName());
+                return new PrepareUnmanageVMInstanceAnswer(cmd, false, 
String.format("Cannot find VM with name [%s] in datacenter [%s].", 
instanceName, dataCenterMo.getName()));
             }
         } catch (Exception e) {
             s_logger.error("Error trying to verify if VM to unmanage exists", 
e);
@@ -7795,9 +7461,6 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
     }
 
     private Answer execute(ValidateVcenterDetailsCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource ValidateVcenterDetailsCommand " 
+ _gson.toJson(cmd));
-        }
         String vCenterServerAddress = cmd.getvCenterServerAddress();
         VmwareContext context = getServiceContext();
 
@@ -7837,4 +7500,25 @@ public class VmwareResource implements 
StoragePoolResource, ServerResource, Vmwa
         }
         return windowInterval;
     }
+
+    @Override
+    public String createLogMessageException(Throwable e, Command command) {
+        if (e instanceof RemoteException) {
+            s_logger.warn("Encounter remote exception to vCenter, invalidate 
VMware session context.");
+            invalidateServiceContext();
+        }
+
+        String message = String.format("%s failed due to [%s].", 
command.getClass().getSimpleName(), VmwareHelper.getExceptionMessage(e));
+        s_logger.error(message, e);
+
+        return message;
+    }
+
+    private void logCommand(Command cmd) {
+        try {
+            s_logger.debug(String.format(EXECUTING_RESOURCE_COMMAND, 
cmd.getClass().getSimpleName(), _gson.toJson(cmd)));
+        } catch (Exception e) {
+            s_logger.error(String.format("Failed to log command %s due to: 
[%s].", cmd.getClass().getSimpleName(), e.getMessage()), e);
+        }
+    }
 }
\ No newline at end of file
diff --git 
a/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareSecondaryStorageResourceHandler.java
 
b/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareSecondaryStorageResourceHandler.java
index 936ca15..44bcbf3 100644
--- 
a/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareSecondaryStorageResourceHandler.java
+++ 
b/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareSecondaryStorageResourceHandler.java
@@ -47,6 +47,7 @@ import com.cloud.hypervisor.vmware.mo.VmwareHostType;
 import com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost;
 import com.cloud.hypervisor.vmware.mo.VmwareHypervisorHostNetworkSummary;
 import com.cloud.hypervisor.vmware.util.VmwareContext;
+import com.cloud.hypervisor.vmware.util.VmwareHelper;
 import com.cloud.serializer.GsonHelper;
 import com.cloud.utils.NumbersUtil;
 import com.cloud.utils.Pair;
@@ -96,9 +97,7 @@ public class VmwareSecondaryStorageResourceHandler implements 
SecondaryStorageRe
         try {
             Answer answer;
             NDC.push(getCommandLogTitle(cmd));
-
-            if (s_logger.isDebugEnabled())
-                s_logger.debug("Executing " + _gson.toJson(cmd));
+            logCommand(cmd);
 
             if (cmd instanceof PrimaryStorageDownloadCommand) {
                 answer = execute((PrimaryStorageDownloadCommand)cmd);
@@ -145,6 +144,14 @@ public class VmwareSecondaryStorageResourceHandler 
implements SecondaryStorageRe
         }
     }
 
+    private void logCommand(Command cmd) {
+        try {
+            s_logger.debug(String.format("Executing command: [%s].", 
_gson.toJson(cmd)));
+        } catch (Exception e) {
+            s_logger.debug(String.format("Executing command: [%s].", 
cmd.getClass().getSimpleName()));
+        }
+    }
+
     protected Answer execute(CreateEntityDownloadURLCommand cmd) {
         boolean success = _storageMgr.execute(this, cmd);
         if (success) {
@@ -311,4 +318,12 @@ public class VmwareSecondaryStorageResourceHandler 
implements SecondaryStorageRe
     public String getMountPoint(String storageUrl, String nfsVersion) {
         return _resource.getRootDir(storageUrl, nfsVersion);
     }
+
+    @Override
+    public String createLogMessageException(Throwable e, Command command) {
+        String message = String.format("%s failed due to [%s].", 
command.getClass().getSimpleName(), VmwareHelper.getExceptionMessage(e));
+        s_logger.error(message, e);
+
+        return message;
+    }
 }
diff --git 
a/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareStorageProcessor.java
 
b/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareStorageProcessor.java
index c0aaf0e..dab7a74 100644
--- 
a/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareStorageProcessor.java
+++ 
b/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareStorageProcessor.java
@@ -302,7 +302,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
             return answer;
         }
         catch (Exception ex) {
-            s_logger.debug(ex.getMessage());
+            s_logger.error(String.format("Command %s failed due to: [%s].", 
cmd.getClass().getSimpleName(), ex.getMessage()), ex);
 
             throw new CloudRuntimeException(ex.getMessage());
         }
@@ -313,8 +313,8 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
 
         if (extents != null) {
             for (HostUnresolvedVmfsExtent extent : extents) {
-                s_logger.debug("extent devicePath=" + extent.getDevicePath() + 
", ordinal=" + extent.getOrdinal()
-                        + ", reason=" + extent.getReason() + ", isHeadExtent=" 
+ extent.isIsHeadExtent());
+                s_logger.debug(String.format("HostUnresolvedVmfsExtent 
details: [devicePath: %s, ordinal: %s, reason: %s, isHeadExtent: %s].", 
extent.getDevicePath(),
+                        extent.getOrdinal(), extent.getReason(), 
extent.isIsHeadExtent()));
 
                 String extentDevicePath = extent.getDevicePath();
 
@@ -380,6 +380,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
      * Returns the (potentially new) name of the VMDK file.
      */
     private String cleanUpDatastore(Command cmd, HostDatastoreSystemMO 
hostDatastoreSystem, DatastoreMO dsMo, Map<String, String> details) throws 
Exception {
+        s_logger.debug(String.format("Executing clean up in DataStore: [%s].", 
dsMo.getName()));
         boolean expandDatastore = 
Boolean.parseBoolean(details.get(DiskTO.EXPAND_DATASTORE));
 
         // A volume on the storage system holding a template uses a minimum 
hypervisor snapshot reserve value.
@@ -488,11 +489,10 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
     private Pair<VirtualMachineMO, Long> 
copyTemplateFromSecondaryToPrimary(VmwareHypervisorHost hyperHost, DatastoreMO 
datastoreMo, String secondaryStorageUrl,
                                                                             
String templatePathAtSecondaryStorage, String templateName, String templateUuid,
                                                                             
boolean createSnapshot, String nfsVersion, String configuration) throws 
Exception {
-        s_logger.info("Executing copyTemplateFromSecondaryToPrimary. 
secondaryStorage: " + secondaryStorageUrl + ", templatePathAtSecondaryStorage: 
" +
-                templatePathAtSecondaryStorage + ", templateName: " + 
templateName + ", configuration: " + configuration);
-
         String secondaryMountPoint = 
mountService.getMountPoint(secondaryStorageUrl, nfsVersion);
-        s_logger.info("Secondary storage mount point: " + secondaryMountPoint);
+
+        s_logger.info(String.format("Init copy of template [name: %s, path in 
secondary storage: %s, configuration: %s] in secondary storage [url: %s, mount 
point: %s] to primary storage.",
+                templateName, templatePathAtSecondaryStorage, configuration, 
secondaryStorageUrl, secondaryMountPoint));
 
         String srcOVAFileName =
                 
VmwareStorageLayoutHelper.getTemplateOnSecStorageFilePath(secondaryMountPoint, 
templatePathAtSecondaryStorage, templateName,
@@ -525,9 +525,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
         }
 
         VmConfigInfo vAppConfig;
-        if (s_logger.isDebugEnabled()) {
-            s_logger.debug(String.format("Deploying OVF template %s with 
configuration %s", templateName, configuration));
-        }
+        s_logger.debug(String.format("Deploying OVF template %s with 
configuration %s.", templateName, configuration));
         hyperHost.importVmFromOVF(srcFileName, templateUuid, datastoreMo, 
"thin", configuration);
         VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(templateUuid);
         if (vmMo == null) {
@@ -720,15 +718,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
 
             return new CopyCmdAnswer(newTemplate);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            String msg = "Unable to copy template to primary storage due to 
exception:" + VmwareHelper.getExceptionMessage(e);
-
-            s_logger.error(msg, e);
-
-            return new CopyCmdAnswer(msg);
+            return new CopyCmdAnswer(hostService.createLogMessageException(e, 
cmd));
         }
         finally {
             if (dsMo != null && managedStoragePoolName != null) {
@@ -818,7 +808,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
                 VirtualMachineMO existingVm = dcMo.findVm(vmName);
                 if (volume.getDeviceId().equals(0L)) {
                     if (existingVm != null) {
-                        s_logger.info("Found existing VM " + vmName + " before 
cloning from template, destroying it");
+                        s_logger.info(String.format("Found existing VM wth 
name [%s] before cloning from template, destroying it", vmName));
                         existingVm.detachAllDisksAndDestroy();
                     }
                     s_logger.info("ROOT Volume from deploy-as-is template, 
cloning template");
@@ -890,14 +880,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
             }
             return new CopyCmdAnswer(newVol);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                hostService.invalidateServiceContext(null);
-            }
-
-            String msg = "clone volume from base image failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg, e);
-            return new CopyCmdAnswer(e.toString());
+            return new CopyCmdAnswer(hostService.createLogMessageException(e, 
cmd));
         }
     }
 
@@ -1074,13 +1057,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
             newVolume.setPath(result.second());
             return new CopyCmdAnswer(newVolume);
         } catch (Throwable t) {
-            if (t instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            String msg = "Unable to execute CopyVolumeCommand due to 
exception";
-            s_logger.error(msg, t);
-            return new CopyCmdAnswer("copy volume secondary to primary failed 
due to exception: " + VmwareHelper.getExceptionMessage(t));
+            return new CopyCmdAnswer(hostService.createLogMessageException(t, 
cmd));
         }
 
     }
@@ -1135,7 +1112,6 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
                 vmMo.removeSnapshot(exportName, false);
             }
             if (workerVm != null) {
-                //detach volume and destroy worker vm
                 workerVm.detachAllDisksAndDestroy();
             }
         }
@@ -1162,13 +1138,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
             newVolume.setPath(result.first() + File.separator + 
result.second());
             return new CopyCmdAnswer(newVolume);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            String msg = "Unable to execute CopyVolumeCommand due to 
exception";
-            s_logger.error(msg, e);
-            return new CopyCmdAnswer("copy volume from primary to secondary 
failed due to exception: " + VmwareHelper.getExceptionMessage(e));
+            return new CopyCmdAnswer(hostService.createLogMessageException(e, 
cmd));
         }
     }
 
@@ -1345,14 +1315,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
             return new CopyCmdAnswer(newTemplate);
 
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            s_logger.error("Unexpecpted exception ", e);
-
-            details = "create template from volume exception: " + 
VmwareHelper.getExceptionMessage(e);
-            return new CopyCmdAnswer(details);
+            return new CopyCmdAnswer(hostService.createLogMessageException(e, 
cmd));
         } finally {
             try {
                 if (volume.getVmName() == null && workerVmMo != null) {
@@ -1780,15 +1743,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
 
             return new CopyCmdAnswer(newTemplate);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            s_logger.error("Unexpected exception ", e);
-
-            details = "create template from snapshot exception: " + 
VmwareHelper.getExceptionMessage(e);
-
-            return new CopyCmdAnswer(details);
+            return new CopyCmdAnswer(hostService.createLogMessageException(e, 
cmd));
         }
     }
 
@@ -1804,8 +1759,11 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
                 Script command = new Script(false, "mkdir", _timeout, 
s_logger);
                 command.add("-p");
                 command.add(exportPath);
-                if (command.execute() != null) {
-                    throw new Exception("unable to prepare snapshot backup 
directory");
+                String result = command.execute();
+                if (result != null) {
+                    String errorMessage = String.format("Unable to prepare 
snapshot backup directory: [%s] due to [%s].", exportPath, result);
+                    s_logger.error(errorMessage);
+                    throw new Exception(errorMessage);
                 }
             }
         }
@@ -1835,8 +1793,8 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
                 }
                 vmMo = clonedVm;
             }
-            vmMo.exportVm(exportPath, exportName, false, false);
 
+            vmMo.exportVm(exportPath, exportName, false, false);
             return new Pair<>(diskDevice, disks);
         } finally {
             if (clonedVm != null) {
@@ -1921,6 +1879,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
                     hasOwnerVm = true;
                 }
 
+                s_logger.debug(String.format("Executing backup snapshot with 
UUID [%s] to secondary storage.", snapshotUuid));
                 backupResult =
                         backupSnapshotToSecondaryStorage(context, vmMo, 
hyperHost, destSnapshot.getPath(), srcSnapshot.getVolume().getPath(), 
snapshotUuid, secondaryStorageUrl,
                                 prevSnapshotUuid, prevBackupUuid, 
hostService.getWorkerName(context, cmd, 1, null), _nfsVersion);
@@ -2009,24 +1968,16 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
 
                 try {
                     if (workerVm != null) {
-                        // detach volume and destroy worker vm
                         workerVm.detachAllDisksAndDestroy();
                     }
                 } catch (Throwable e) {
-                    s_logger.warn("Failed to destroy worker VM: " + 
workerVMName);
+                    s_logger.warn(String.format("Failed to destroy worker VM 
[%s] due to: [%s]", workerVMName, e.getMessage()), e);
                 }
             }
 
             return answer;
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            s_logger.error("Unexpecpted exception ", e);
-
-            details = "backup snapshot exception: " + 
VmwareHelper.getExceptionMessage(e);
-            return new CopyCmdAnswer(details);
+            return new CopyCmdAnswer(hostService.createLogMessageException(e, 
cmd));
         }
     }
 
@@ -2226,23 +2177,9 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
 
             return answer;
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-
-                hostService.invalidateServiceContext(null);
-            }
-
-            String msg = "";
-
-            if (isAttach) {
-                msg += "Failed to attach volume: ";
-            }
-            else {
-                msg += "Failed to detach volume: ";
-            }
-
-            s_logger.error(msg + e.getMessage(), e);
-
+            String msg = String.format("Failed to %s volume!", isAttach? 
"attach" : "detach");
+            s_logger.error(msg, e);
+            hostService.createLogMessageException(e, cmd);
             // Sending empty error message - too many duplicate errors in UI
             return new AttachAnswer("");
         }
@@ -2468,17 +2405,9 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
                 hostService.invalidateServiceContext(null);
             }
 
-            if (isAttach) {
-                String msg = "AttachIsoCommand(attach) failed due to " + 
VmwareHelper.getExceptionMessage(e);
-                msg = msg + " Also check if your guest os is a supported 
version";
-                s_logger.error(msg, e);
-                return new AttachAnswer(msg);
-            } else {
-                String msg = "AttachIsoCommand(detach) failed due to " + 
VmwareHelper.getExceptionMessage(e);
-                msg = msg + " Also check if your guest os is a supported 
version";
-                s_logger.warn(msg, e);
-                return new AttachAnswer(msg);
-            }
+            String message = String.format("AttachIsoCommand(%s) failed due 
to: [%s]. Also check if your guest os is a supported version", isAttach? 
"attach" : "detach", VmwareHelper.getExceptionMessage(e));
+            s_logger.error(message, e);
+            return new AttachAnswer(message);
         }
     }
 
@@ -2558,14 +2487,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
             }
             return new CreateObjectAnswer(newVol);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                hostService.invalidateServiceContext(null);
-            }
-
-            String msg = "create volume failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg, e);
-            return new CreateObjectAnswer(e.toString());
+            return new 
CreateObjectAnswer(hostService.createLogMessageException(e, cmd));
         }
     }
 
@@ -2588,10 +2510,6 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
 
     @Override
     public Answer deleteVolume(DeleteCommand cmd) {
-        if (s_logger.isInfoEnabled()) {
-            s_logger.info("Executing resource DeleteCommand: " + 
_gson.toJson(cmd));
-        }
-
         try {
             VmwareContext context = hostService.getServiceContext(null);
             VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, 
null);
@@ -2735,14 +2653,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
 
             return new Answer(cmd, true, "Success");
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-                hostService.invalidateServiceContext(null);
-            }
-
-            String msg = "delete volume failed due to " + 
VmwareHelper.getExceptionMessage(e);
-            s_logger.error(msg, e);
-            return new Answer(cmd, false, msg);
+            return new Answer(cmd, false, 
hostService.createLogMessageException(e, cmd));
         }
     }
 
@@ -3816,12 +3727,8 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
             newVol.setPath(newVolumeName);
             return new CopyCmdAnswer(newVol);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-
-            s_logger.error("Unexpecpted exception ", e);
-            details = "create volume from snapshot exception: " + 
VmwareHelper.getExceptionMessage(e);
+            hostService.createLogMessageException(e, cmd);
+            details = String.format("Failed to create volume from snapshot due 
to exception: [%s]", VmwareHelper.getExceptionMessage(e));
         }
         return new CopyCmdAnswer(details);
     }
@@ -3907,11 +3814,8 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
             else
                 return new Answer(cmd, isDatastoreStoragePolicyComplaint, 
null);
         } catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                hostService.invalidateServiceContext(context);
-            }
-            String details = String.format("Exception while checking if 
datastore %s is storage policy %s complaince : %s", primaryStorageNameLabel, 
storagePolicyId,  VmwareHelper.getExceptionMessage(e));
-            s_logger.error(details, e);
+            hostService.createLogMessageException(e, cmd);
+            String details = String.format("Exception while checking if 
datastore [%s] is storage policy [%s] complaince due to: [%s]", 
primaryStorageNameLabel, storagePolicyId, VmwareHelper.getExceptionMessage(e));
             return new Answer(cmd, false, details);
         }
     }
@@ -4023,13 +3927,7 @@ public class VmwareStorageProcessor implements 
StorageProcessor {
 
             return answer;
         }  catch (Throwable e) {
-            if (e instanceof RemoteException) {
-                s_logger.warn("Encounter remote exception to vCenter, 
invalidate VMware session context");
-
-                hostService.invalidateServiceContext(null);
-            }
-
-            return new SyncVolumePathAnswer("Failed to process 
SyncVolumePathCommand due to " + e.getMessage());
+            return new 
SyncVolumePathAnswer(hostService.createLogMessageException(e, cmd));
         }
     }
 }
diff --git 
a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java
 
b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java
index 5a08aa7..be564cf 100644
--- 
a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java
+++ 
b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java
@@ -127,6 +127,7 @@ import com.vmware.vim25.VirtualSCSISharing;
 public class VirtualMachineMO extends BaseMO {
     private static final Logger s_logger = 
Logger.getLogger(VirtualMachineMO.class);
     private static final ExecutorService MonitorServiceExecutor = 
Executors.newCachedThreadPool(new NamedThreadFactory("VM-Question-Monitor"));
+    private static final Gson GSON = new Gson();
 
     public static final String ANSWER_YES = "0";
     public static final String ANSWER_NO = "1";
@@ -1402,7 +1403,7 @@ public class VirtualMachineMO extends BaseMO {
 
         if(s_logger.isTraceEnabled())
             s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + 
_mor.getValue() + ", vmdkDatastorePath: "
-                            + new Gson().toJson(vmdkDatastorePathChain) + ", 
datastore: " + morDs.getValue());
+                            + GSON.toJson(vmdkDatastorePathChain) + ", 
datastore: " + morDs.getValue());
         int controllerKey = 0;
         int unitNumber = 0;
 
@@ -1800,7 +1801,7 @@ public class VirtualMachineMO extends BaseMO {
         Pair<VmdkFileDescriptor, byte[]> result = new Pair<VmdkFileDescriptor, 
byte[]>(descriptor, content);
         if (s_logger.isTraceEnabled()) {
             s_logger.trace("vCenter API trace - getVmdkFileInfo() done");
-            s_logger.trace("VMDK file descriptor: " + new 
Gson().toJson(result.first()));
+            s_logger.trace("VMDK file descriptor: " + 
GSON.toJson(result.first()));
         }
         return result;
     }

Reply via email to