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