From 3ac8995f5aecf8f4294d5117767bf0a0a2491c56 Mon Sep 17 00:00:00 2001 From: gitseti Date: Thu, 18 Feb 2021 10:13:51 +0100 Subject: [PATCH] Improve logging --- .../azure/AzureDiscoveryExtensionIT.java | 1 - .../AzureClusterDiscoveryCallback.java | 26 +++++++++++++------ .../azure/client/AzureStorageClient.java | 18 +++++++------ .../discovery/azure/config/ConfigReader.java | 12 ++++----- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/integrationTest/java/com/hivemq/extensions/discovery/azure/AzureDiscoveryExtensionIT.java b/src/integrationTest/java/com/hivemq/extensions/discovery/azure/AzureDiscoveryExtensionIT.java index bf3679e..87a7446 100644 --- a/src/integrationTest/java/com/hivemq/extensions/discovery/azure/AzureDiscoveryExtensionIT.java +++ b/src/integrationTest/java/com/hivemq/extensions/discovery/azure/AzureDiscoveryExtensionIT.java @@ -263,7 +263,6 @@ private HiveMQTestContainerExtension createHiveMQNode(final @NotNull String conn .withExtension(extensionDir.toFile()) .withHiveMQConfig(new File("src/integrationTest/resources/config.xml")) .withNetwork(network) - .withLogLevel(Level.DEBUG) .waitingFor(Wait.forLogMessage(".*Started HiveMQ in.*\\n", 1)); } diff --git a/src/main/java/com/hivemq/extensions/discovery/azure/callback/AzureClusterDiscoveryCallback.java b/src/main/java/com/hivemq/extensions/discovery/azure/callback/AzureClusterDiscoveryCallback.java index 343dbf8..14f7762 100644 --- a/src/main/java/com/hivemq/extensions/discovery/azure/callback/AzureClusterDiscoveryCallback.java +++ b/src/main/java/com/hivemq/extensions/discovery/azure/callback/AzureClusterDiscoveryCallback.java @@ -63,7 +63,7 @@ public void init( try { azureStorageClient.createOrUpdate(); } catch (final IllegalStateException | IllegalArgumentException ex) { - log.warn("Initialization of the Azure Cluster Discovery Callback failed. {}", ex.getMessage()); + log.warn("Initialization of the Azure Cluster Discovery Callback failed. {}", getRootCause(ex).getMessage()); return; } @@ -81,7 +81,7 @@ public void init( saveOwnFile(clusterDiscoveryInput.getOwnClusterId(), clusterDiscoveryInput.getOwnAddress()); clusterDiscoveryOutput.provideCurrentNodes(getNodeAddresses()); } catch (final Exception ex) { - log.warn("Initialization of the Azure Cluster Discovery Callback failed. {}", ex.getMessage()); + log.warn("Initialization of the Azure Cluster Discovery Callback failed. {}", getRootCause(ex).getMessage()); } } @@ -94,7 +94,7 @@ public void reload( try { azureStorageClient.createOrUpdate(); } catch (final IllegalStateException | IllegalArgumentException ex) { - log.warn("Reload of the Azure Cluster Discovery Callback failed. {}", ex.getMessage()); + log.warn("Reload of the Azure Cluster Discovery Callback failed. {}", getRootCause(ex).getMessage()); return; } @@ -116,7 +116,7 @@ public void reload( clusterDiscoveryOutput.provideCurrentNodes(getNodeAddresses()); } catch (final Exception ex) { - log.warn("Reload of the Azure Cluster Discovery Callback failed. {}", ex.getMessage()); + log.warn("Reload of the Azure Cluster Discovery Callback failed. {}", getRootCause(ex).getMessage()); } } @@ -127,7 +127,7 @@ public void destroy(final @NotNull ClusterDiscoveryInput clusterDiscoveryInput) deleteOwnFile(clusterDiscoveryInput.getOwnClusterId()); } } catch (final RuntimeException ex) { - log.warn("Destroy of the Azure Cluster Discovery Callback failed. {}", ex.getMessage()); + log.warn("Destroy of the Azure Cluster Discovery Callback failed. {}", getRootCause(ex).getMessage()); } } @@ -177,7 +177,7 @@ private void deleteOwnFile(final @NotNull String ownClusterId) throws RuntimeExc try { azureStorageClient.deleteBlob(blobKey); } catch (final Exception ex) { - log.warn("Could not delete expired Azure Blob file '{}'. {}", blobKey, ex.getMessage()); + log.warn("Could not delete expired Azure Blob file '{}'. {}", blobKey, getRootCause(ex)); } } else { nodeAddresses.add(nodeFile.getClusterNodeAddress()); @@ -203,7 +203,7 @@ private void deleteOwnFile(final @NotNull String ownClusterId) throws RuntimeExc } }); } catch (final Exception ex) { - log.warn("Could not get Azure Blobs. {}", ex.getMessage()); + log.warn("Could not get Azure Blobs. {}", getRootCause(ex).getMessage()); } return clusterNodeFiles; @@ -215,7 +215,7 @@ private void deleteOwnFile(final @NotNull String ownClusterId) throws RuntimeExc try { fileContent = azureStorageClient.getBlobContent(blob.getName()); } catch (RuntimeException e) { - log.warn("An error occurred while downloading the Azure Blob. {}", e.getMessage()); + log.warn("An error occurred while downloading the Azure Blob. {}", getRootCause(e).getMessage()); return null; } @@ -235,4 +235,14 @@ private void deleteOwnFile(final @NotNull String ownClusterId) throws RuntimeExc void setAzureStorageClient(final @NotNull AzureStorageClient azureStorageClient) { this.azureStorageClient = azureStorageClient; } + + private @NotNull Throwable getRootCause(final @NotNull Throwable e) { + Throwable cause; + Throwable result = e; + + while(null != (cause = result.getCause()) && (result != cause) ) { + result = cause; + } + return result; + } } diff --git a/src/main/java/com/hivemq/extensions/discovery/azure/client/AzureStorageClient.java b/src/main/java/com/hivemq/extensions/discovery/azure/client/AzureStorageClient.java index b7ff9a5..6002ece 100644 --- a/src/main/java/com/hivemq/extensions/discovery/azure/client/AzureStorageClient.java +++ b/src/main/java/com/hivemq/extensions/discovery/azure/client/AzureStorageClient.java @@ -75,7 +75,7 @@ public boolean existsContainer() throws RuntimeException { return containerClient.exists(); } catch (final BlobStorageException blobStorageException) { throw new RuntimeException("Azure Storage Container existence check failed with status code " + - blobStorageException.getStatusCode() + " and error code " + blobStorageException.getErrorCode()); + blobStorageException.getStatusCode() + " and error code " + blobStorageException.getErrorCode() + "."); } } @@ -93,7 +93,7 @@ public void createContainer() throws RuntimeException { } else { throw new RuntimeException( "Azure Storage Container creation failed with status code " + error.getStatusCode() + - " and error code " + error.getErrorCode()); + " and error code " + error.getErrorCode() + "."); } } } @@ -107,7 +107,7 @@ public void saveBlob(final @NotNull String blobName, final @NotNull String conte } catch (final BlobStorageException blobStorageException) { throw new RuntimeException( "Azure Storage Blob upload failed with status code " + blobStorageException.getStatusCode() + - " and error code " + blobStorageException.getErrorCode()); + " and error code " + blobStorageException.getErrorCode() + "."); } } @@ -116,10 +116,11 @@ public void deleteBlob(final @NotNull String blobName) throws RuntimeException { try { blob.delete(); - } catch (final BlobStorageException blobStorageException) { + } + catch (final BlobStorageException blobStorageException) { throw new RuntimeException( "Azure Storage Blob delete failed with status code " + blobStorageException.getStatusCode() + - " and error code " + blobStorageException.getErrorCode()); + " and error code " + blobStorageException.getErrorCode() + "."); } } @@ -130,10 +131,11 @@ public String getBlobContent(final @NotNull String blobName) throws RuntimeExcep try { blobClient.download(outputStream); - } catch (final BlobStorageException blobStorageException) { + } + catch (final BlobStorageException blobStorageException) { throw new RuntimeException( "Azure Storage Blob download failed with status code " + blobStorageException.getStatusCode() + - " and error code " + blobStorageException.getErrorCode()); + " and error code " + blobStorageException.getErrorCode() + "."); } return outputStream.toString(); @@ -147,7 +149,7 @@ public Iterator getBlobs(final @NotNull String filePrefix) throws Runt } catch (final BlobStorageException blobStorageException) { throw new RuntimeException( "Azure Storage Blobs retrieval failed with status code " + blobStorageException.getStatusCode() + - " and error code " + blobStorageException.getErrorCode()); + " and error code " + blobStorageException.getErrorCode() + "."); } } diff --git a/src/main/java/com/hivemq/extensions/discovery/azure/config/ConfigReader.java b/src/main/java/com/hivemq/extensions/discovery/azure/config/ConfigReader.java index be23780..dee0baf 100644 --- a/src/main/java/com/hivemq/extensions/discovery/azure/config/ConfigReader.java +++ b/src/main/java/com/hivemq/extensions/discovery/azure/config/ConfigReader.java @@ -88,13 +88,13 @@ private static boolean isValid(final @NotNull AzureDiscoveryConfig azureDiscover final String connectionString = azureDiscoveryConfig.getConnectionString(); if (isNullOrBlank(connectionString)) { - logger.warn("The Connection String of the configuration file was empty."); + logger.warn("The Connection String in the configuration file was empty."); return false; } final String containerName = azureDiscoveryConfig.getContainerName(); if (isNullOrBlank(containerName)) { - logger.warn("The Container Name of the configuration file was empty."); + logger.warn("The Container Name in the configuration file was empty."); return false; } @@ -103,12 +103,12 @@ private static boolean isValid(final @NotNull AzureDiscoveryConfig azureDiscover fileExpirationInSeconds = azureDiscoveryConfig.getFileExpirationInSeconds(); } catch (final UnsupportedOperationException e) { logger.warn( - "The File Expiration Interval of the configuration file was empty."); + "The File Expiration Interval in the configuration file was not valid. {}.", e.getMessage()); return false; } if (fileExpirationInSeconds < 0) { logger.warn( - "The File Expiration Interval of the configuration file was negative."); + "The File Expiration Interval in the configuration file was negative."); return false; } @@ -117,12 +117,12 @@ private static boolean isValid(final @NotNull AzureDiscoveryConfig azureDiscover fileUpdateIntervalInSeconds = azureDiscoveryConfig.getFileUpdateIntervalInSeconds(); } catch (final UnsupportedOperationException e) { logger.warn( - "The File Update Interval of the configuration file was empty."); + "The File Update Interval in the configuration file was not valid. {}.", e.getMessage()); return false; } if (fileUpdateIntervalInSeconds < 0) { logger.warn( - "The File Update Interval of the configuration file was negative."); + "The File Update Interval in the configuration file was negative."); return false; }