使用適用于 JAVA 的 Azure 儲存體用戶端程式庫進行用戶端記錄

如需如何在 JAVA 專案中安裝 Azure 儲存體用戶端程式庫二進位檔的指示,請參閱 GitHub 上專案的讀我檔案: https://github.com/Azure/azure-storage-java 。 此檔案記載您必須安裝的其他相依項目。

如果您計劃使用用戶端記錄,則必須安裝選用的 SLF4J 相依項目。 SLF4J 是一種記錄外觀,可讓您輕鬆地從用戶端應用程式使用許多常見的 JAVA 記錄架構:如需 SLF4J 的詳細資訊,請參閱 SLF4J 使用者手冊。 如需如何搭配儲存體 SDK 使用 SLF4J 的簡單測試,請將 slf4j-apislf4j-simple JAR 檔案放在儲存體用戶端專案的建置路徑中。 所有儲存體記錄訊息隨後都會導向至主控台。

下列範例 Java 程式碼顯示如何藉由呼叫靜態方法 setLoggingEnabledByDefault 來預設關閉儲存體用戶端記錄,然後使用 OperationContext 物件來啟用特定要求的記錄:

// Set logging off by default.  
OperationContext.setLoggingEnabledByDefault(false);  
OperationContext ctx = new OperationContext();  
ctx.setLoggingEnabled(true);  
  
// Create an operation to add a new customer to the people table.  
TableOperation insertCustomer1 = TableOperation.insertOrReplace(customer1);  
  
// Submit the operation to the table service.  
table.execute(insertCustomer1, null, ctx);  
  

下列範例顯示 slf4j 簡單寫入主控台的記錄訊息:

[main] INFO ROOT - {ceba5ec6...}: {Starting operation.}  
[main] INFO ROOT - {ceba5ec6...}: {Starting operation with location 'PRIMARY' per location mode 'PRIMARY_ONLY'.}  
[main] INFO ROOT - {ceba5ec6...}: {Starting request to 'http://storageaccountname2.table.core.windows.net/people(PartitionKey='Harp',RowKey='Walter')' at 'Tue, 08 Jul 2014 15:07:43 GMT'.}  
[main] INFO ROOT - {ceba5ec6...}: {Writing request data.}  
[main] INFO ROOT - {ceba5ec6...}: {Request data was written successfully.}  
[main] INFO ROOT - {ceba5ec6...}: {Waiting for response.}  
[main] INFO ROOT - {ceba5ec6...}: {Response received. Status code = '204', Request ID = '8f6ce566-3760-4733-a8da-a090e642286a', Content-MD5 = 'null', ETag = 'W/"datetime'2014-07-08T15%3A07%3A41.1177234Z'"'.}  
[main] INFO ROOT - {ceba5ec6...}: {Processing response headers.}  
[main] INFO ROOT - {ceba5ec6...}: {Response headers were processed successfully.}  
[main] INFO ROOT - {ceba5ec6...}: {Processing response body.}  
[main] INFO ROOT - {ceba5ec6...}: {Response body was parsed successfully.}  
[main] INFO ROOT - {ceba5ec6...}: {Operation completed.}  
  

GUID (ceba5ec6...範例) 是用戶端儲存程式庫指派給儲存體作業的用戶端要求識別碼。