Python 用 Azure SDK のログレベル変更方法

## はじめに
Azure App Service のデータを CosmosDB で永続化しようと思い、pip で `azure-cosmos` ライブラリを追加、接続処理の実装を行いました。

```text:requirements.txt
azure-cosmos==4.0.0
```

実装後、デバッグをしていると Azure SDK のパッケージ `azure.core.pipeline.policies.http_logging_policy` に関するログが大量に出力されることに気づきました。

この記事では上記ロガーのログレベルを変更して、ログ出力を抑制する方法を紹介します。

## ログ出力例
私の環境では、以下のログが出力されました。


```bash
2020-06-24 19:11:55,493 DEBUG urllib3.connectionpool :https://xxx.documents.azure.com:443 "POST /dbs/xxx/colls/xxx/docs/ HTTP/1.1" 200 None
2020-06-24 19:11:55,495 INFO azure.core.pipeline.policies.http_logging_policy :Response status: 200
2020-06-24 19:11:55,495 INFO azure.core.pipeline.policies.http_logging_policy :Response headers:
2020-06-24 19:11:55,495 INFO azure.core.pipeline.policies.http_logging_policy :    'Cache-Control': 'no-store, no-cache'
2020-06-24 19:11:55,495 INFO azure.core.pipeline.policies.http_logging_policy :    'Pragma': 'no-cache'
2020-06-24 19:11:55,495 INFO azure.core.pipeline.policies.http_logging_policy :    'Transfer-Encoding': 'chunked'
2020-06-24 19:11:55,495 INFO azure.core.pipeline.policies.http_logging_policy :    'Content-Type': 'application/json'
2020-06-24 19:11:55,495 INFO azure.core.pipeline.policies.http_logging_policy :    'Server': 'Microsoft-HTTPAPI/2.0'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'Strict-Transport-Security': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-last-state-change-utc': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-resource-quota': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-resource-usage': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'lsn': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-item-count': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-schemaversion': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-alt-content-path': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-content-path': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-quorum-acked-lsn': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-current-write-quorum': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-current-replica-set-size': 'REDACTED'
2020-06-24 19:11:55,496 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-xp-role': 'REDACTED'
2020-06-24 19:11:55,497 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-cosmos-query-execution-info': 'REDACTED'
2020-06-24 19:11:55,497 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-global-Committed-lsn': 'REDACTED'
2020-06-24 19:11:55,497 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-number-of-read-regions': 'REDACTED'
2020-06-24 19:11:55,497 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-transport-request-id': 'REDACTED'
2020-06-24 19:11:55,497 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-cosmos-llsn': 'REDACTED'
2020-06-24 19:11:55,497 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-cosmos-quorum-acked-llsn': 'REDACTED'
2020-06-24 19:11:55,497 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-session-token': 'REDACTED'
2020-06-24 19:11:55,498 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-request-charge': 'REDACTED'
2020-06-24 19:11:55,498 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-serviceversion': 'REDACTED'
2020-06-24 19:11:55,498 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-activity-id': 'REDACTED'
2020-06-24 19:11:55,498 INFO azure.core.pipeline.policies.http_logging_policy :    'x-ms-gatewayversion': 'REDACTED'
2020-06-24 19:11:55,498 INFO azure.core.pipeline.policies.http_logging_policy :    'Date': 'Wed, 24 Jun 2020 10:11:55 GMT'
```

## 対処方法
`azure.core.pipeline.policies.http_logging_policy` ロガーにログレベルを設定することで不要なログを抑制できます。

```python:application.py
logging.getLogger('azure.core.pipeline.policies.http_logging_policy').setLevel('WARNING')
```

この例ではログレベル `WARNING` 以上のログが出力されます。その他のログレベルを設定したければ、[logging — Logging facility for Python — Python 3.8.3 documentation](https://docs.python.org/3.8/library/logging.html#levels) を参照してください。


## 参考
- [クイック スタート:Azure Cosmos DB SQL API アカウントを使用して Python アプリを構築する | Microsoft Docs](https://docs.microsoft.com/ja-jp/azure/cosmos-db/create-sql-api-python)
- [How to disable HTTP logging? · Issue #9422 · Azure/azure-sdk-for-python](https://github.com/Azure/azure-sdk-for-python/issues/9422)