Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit d5ab7b7

Browse files
author
jofriedm-msft
authored
Merge pull request #202 from Azure/master
Standalone sample showing how to enable client-side logging
2 parents 96bd5ac + 56758d3 commit d5ab7b7

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.microsoft.azure</groupId>
5+
<artifactId>azure-storage-samples-logging</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<name>Microsoft Azure Storage Client Samples</name>
9+
<description>Samples for creating blobs, queues and tables</description>
10+
<url>https://github.com/Azure/azure-storage-java</url>
11+
12+
<build>
13+
<sourceDirectory>src</sourceDirectory>
14+
<plugins>
15+
<plugin>
16+
<artifactId>maven-compiler-plugin</artifactId>
17+
<version>3.0</version>
18+
<configuration>
19+
<source>1.6</source>
20+
<target>1.6</target>
21+
</configuration>
22+
</plugin>
23+
</plugins>
24+
</build>
25+
<dependencies>
26+
<dependency>
27+
<groupId>com.microsoft.azure</groupId>
28+
<artifactId>azure-storage</artifactId>
29+
<version>5.4.0</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.microsoft.azure</groupId>
33+
<artifactId>azure-keyvault-extensions</artifactId>
34+
<version>0.8.0</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.slf4j</groupId>
38+
<artifactId>slf4j-api</artifactId>
39+
<version>1.7.12</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.slf4j</groupId>
43+
<artifactId>slf4j-simple</artifactId>
44+
<version>1.7.5</version>
45+
</dependency>
46+
</dependencies>
47+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Copyright Microsoft Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package com.microsoft.azure.storage.ClientLoggingBasics;
16+
17+
import java.io.IOException;
18+
import java.io.PrintWriter;
19+
import java.io.StringWriter;
20+
import java.net.URISyntaxException;
21+
import java.security.InvalidKeyException;
22+
import java.util.UUID;
23+
24+
import com.microsoft.azure.storage.CloudStorageAccount;
25+
import com.microsoft.azure.storage.OperationContext;
26+
import com.microsoft.azure.storage.StorageException;
27+
import com.microsoft.azure.storage.blob.CloudBlobClient;
28+
import com.microsoft.azure.storage.blob.CloudBlobContainer;
29+
import com.microsoft.azure.storage.blob.CloudBlockBlob;
30+
31+
/**
32+
* This sample illustrates basic usage of enabling client-side logging in the Java library.
33+
* This is a stand along sample with its own connection string.
34+
*/
35+
public class ClientLoggingBasics {
36+
37+
/**
38+
* MODIFY THIS!
39+
*
40+
* Stores the storage connection string.
41+
*/
42+
public static final String storageConnectionString = "DefaultEndpointsProtocol=https;"
43+
+ "AccountName=[MY_ACCOUNT_NAME];"
44+
+ "AccountKey=[MY_ACCOUNT_KEY]";
45+
46+
/**
47+
* Executes the sample.
48+
*
49+
* @param args
50+
* No input args are expected from users.
51+
* @throws URISyntaxException
52+
* @throws InvalidKeyException
53+
*/
54+
public static void main(String[] args) throws InvalidKeyException, URISyntaxException, StorageException {
55+
// Logs will be written to standard out by default and can be redirected to a file
56+
System.out.println("The Azure storage client sample to enable logging has started.");
57+
58+
// Setup the cloud storage account.
59+
CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
60+
61+
// Create a blob service client
62+
CloudBlobClient blobClient = account.createCloudBlobClient();
63+
64+
// Get a reference to a container
65+
// This operation will not be logged since it does not make a service request.
66+
// Append a random UUID to the end of the container name so that
67+
// this sample can be run more than once in quick succession.
68+
CloudBlobContainer container = blobClient.getContainerReference("basicloggingcontainer"
69+
+ UUID.randomUUID().toString().replace("-", ""));
70+
71+
try {
72+
EnableGlobalLogging(container);
73+
74+
EnablePerRequestLogging(container);
75+
}
76+
catch (Throwable t) {
77+
printException(t);
78+
container.deleteIfExists();
79+
}
80+
81+
System.out.println("The Azure storage client sample to enable logging has completed.");
82+
}
83+
84+
public static void EnableGlobalLogging(CloudBlobContainer container) throws StorageException {
85+
System.out.println("Enable logging for all requests");
86+
87+
// Enable logging for cases where an OperationContext instance is not used in an API call.
88+
OperationContext.setLoggingEnabledByDefault(true);
89+
90+
// Create the container if it does not exists.
91+
// This operation contacts the Azure Storage Service and will be logged.
92+
// Creating a container is just an example of a request to log.
93+
container.createIfNotExists();
94+
}
95+
96+
public static void EnablePerRequestLogging(CloudBlobContainer container) throws StorageException, URISyntaxException, IOException {
97+
System.out.println("Enable logging per selected requests");
98+
99+
// Set logging to false by default and pass in an operation context to log only specific APIs
100+
OperationContext.setLoggingEnabledByDefault(false);
101+
102+
// Upload a blob passing in the operation context
103+
// Get a reference to a blob in the container
104+
// This operation will not be logged since it does not make a service request.
105+
CloudBlockBlob blob = container.getBlockBlobReference("blob");
106+
107+
OperationContext operationContext = new OperationContext();
108+
operationContext.setLoggingEnabled(true);
109+
110+
// Upload text to the blob passing in the operation context so the request is logged.
111+
// This operation contacts the Azure Storage Service and will be logged
112+
// since it is passing in an operation context that enables logging.
113+
blob.uploadText("Hello, World", null, null, null, operationContext);
114+
115+
// Delete the container if it exists.
116+
// This operation will not be logged since logging has been disabled
117+
// by default and no operation context is being passed in.
118+
container.deleteIfExists();
119+
}
120+
121+
/**
122+
* Prints out the exception information.
123+
*/
124+
public static void printException(Throwable t) {
125+
StringWriter stringWriter = new StringWriter();
126+
PrintWriter printWriter = new PrintWriter(stringWriter);
127+
t.printStackTrace(printWriter);
128+
System.out.println(String.format(
129+
"Got an exception while running the sample. Exception details:\n%s\n",
130+
stringWriter.toString()));
131+
}
132+
}

0 commit comments

Comments
 (0)