forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Azure#202 from Azure/master
Standalone sample showing how to enable client-side logging
- Loading branch information
Showing
2 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
microsoft-azure-storage-samples/src/com/microsoft/azure/storage/logging/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.microsoft.azure</groupId> | ||
<artifactId>azure-storage-samples-logging</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<name>Microsoft Azure Storage Client Samples</name> | ||
<description>Samples for creating blobs, queues and tables</description> | ||
<url>https://github.com/Azure/azure-storage-java</url> | ||
|
||
<build> | ||
<sourceDirectory>src</sourceDirectory> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.0</version> | ||
<configuration> | ||
<source>1.6</source> | ||
<target>1.6</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.microsoft.azure</groupId> | ||
<artifactId>azure-storage</artifactId> | ||
<version>5.4.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.microsoft.azure</groupId> | ||
<artifactId>azure-keyvault-extensions</artifactId> | ||
<version>0.8.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>1.7.12</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<version>1.7.5</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
132 changes: 132 additions & 0 deletions
132
...rage/logging/src/com/microsoft/azure/storage/ClientLoggingBasics/ClientLoggingBasics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* Copyright Microsoft Corporation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.microsoft.azure.storage.ClientLoggingBasics; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.net.URISyntaxException; | ||
import java.security.InvalidKeyException; | ||
import java.util.UUID; | ||
|
||
import com.microsoft.azure.storage.CloudStorageAccount; | ||
import com.microsoft.azure.storage.OperationContext; | ||
import com.microsoft.azure.storage.StorageException; | ||
import com.microsoft.azure.storage.blob.CloudBlobClient; | ||
import com.microsoft.azure.storage.blob.CloudBlobContainer; | ||
import com.microsoft.azure.storage.blob.CloudBlockBlob; | ||
|
||
/** | ||
* This sample illustrates basic usage of enabling client-side logging in the Java library. | ||
* This is a stand along sample with its own connection string. | ||
*/ | ||
public class ClientLoggingBasics { | ||
|
||
/** | ||
* MODIFY THIS! | ||
* | ||
* Stores the storage connection string. | ||
*/ | ||
public static final String storageConnectionString = "DefaultEndpointsProtocol=https;" | ||
+ "AccountName=[MY_ACCOUNT_NAME];" | ||
+ "AccountKey=[MY_ACCOUNT_KEY]"; | ||
|
||
/** | ||
* Executes the sample. | ||
* | ||
* @param args | ||
* No input args are expected from users. | ||
* @throws URISyntaxException | ||
* @throws InvalidKeyException | ||
*/ | ||
public static void main(String[] args) throws InvalidKeyException, URISyntaxException, StorageException { | ||
// Logs will be written to standard out by default and can be redirected to a file | ||
System.out.println("The Azure storage client sample to enable logging has started."); | ||
|
||
// Setup the cloud storage account. | ||
CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString); | ||
|
||
// Create a blob service client | ||
CloudBlobClient blobClient = account.createCloudBlobClient(); | ||
|
||
// Get a reference to a container | ||
// This operation will not be logged since it does not make a service request. | ||
// Append a random UUID to the end of the container name so that | ||
// this sample can be run more than once in quick succession. | ||
CloudBlobContainer container = blobClient.getContainerReference("basicloggingcontainer" | ||
+ UUID.randomUUID().toString().replace("-", "")); | ||
|
||
try { | ||
EnableGlobalLogging(container); | ||
|
||
EnablePerRequestLogging(container); | ||
} | ||
catch (Throwable t) { | ||
printException(t); | ||
container.deleteIfExists(); | ||
} | ||
|
||
System.out.println("The Azure storage client sample to enable logging has completed."); | ||
} | ||
|
||
public static void EnableGlobalLogging(CloudBlobContainer container) throws StorageException { | ||
System.out.println("Enable logging for all requests"); | ||
|
||
// Enable logging for cases where an OperationContext instance is not used in an API call. | ||
OperationContext.setLoggingEnabledByDefault(true); | ||
|
||
// Create the container if it does not exists. | ||
// This operation contacts the Azure Storage Service and will be logged. | ||
// Creating a container is just an example of a request to log. | ||
container.createIfNotExists(); | ||
} | ||
|
||
public static void EnablePerRequestLogging(CloudBlobContainer container) throws StorageException, URISyntaxException, IOException { | ||
System.out.println("Enable logging per selected requests"); | ||
|
||
// Set logging to false by default and pass in an operation context to log only specific APIs | ||
OperationContext.setLoggingEnabledByDefault(false); | ||
|
||
// Upload a blob passing in the operation context | ||
// Get a reference to a blob in the container | ||
// This operation will not be logged since it does not make a service request. | ||
CloudBlockBlob blob = container.getBlockBlobReference("blob"); | ||
|
||
OperationContext operationContext = new OperationContext(); | ||
operationContext.setLoggingEnabled(true); | ||
|
||
// Upload text to the blob passing in the operation context so the request is logged. | ||
// This operation contacts the Azure Storage Service and will be logged | ||
// since it is passing in an operation context that enables logging. | ||
blob.uploadText("Hello, World", null, null, null, operationContext); | ||
|
||
// Delete the container if it exists. | ||
// This operation will not be logged since logging has been disabled | ||
// by default and no operation context is being passed in. | ||
container.deleteIfExists(); | ||
} | ||
|
||
/** | ||
* Prints out the exception information. | ||
*/ | ||
public static void printException(Throwable t) { | ||
StringWriter stringWriter = new StringWriter(); | ||
PrintWriter printWriter = new PrintWriter(stringWriter); | ||
t.printStackTrace(printWriter); | ||
System.out.println(String.format( | ||
"Got an exception while running the sample. Exception details:\n%s\n", | ||
stringWriter.toString())); | ||
} | ||
} |