你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用适用于 Java 的 Azure 存储客户端库进行客户端日志记录

有关如何在 Java 项目中安装 Azure 存储客户端库的二进制文件的说明,请参阅 GitHub 上项目的自述文件: https://github.com/Azure/azure-storage-java。 此文件记录你必须安装的所有其他依赖项。

如果你计划使用客户端日志记录,则必须安装可选的 SLF4J 依赖项。 SLF4J 是一种日志记录外观,使你能够轻松地从客户端应用程序使用许多常见的 Java 日志记录框架:有关 SLF4J 的详细信息,请参阅 SLF4J 用户手册。 有关如何将 SLF4J 与存储 SDK 配合使用的简单测试,请将 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-simple 写入控制台的日志消息:

[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...示例中) 是由客户端存储库分配给存储操作的客户端请求 ID。