> For the complete documentation index, see [llms.txt](https://docs.payments.thalescloud.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.payments.thalescloud.io/nfc-wallet-sdk-android/ja/additional-features/collect-logs-with-secure-logger.md).

# LogService でログを収集する

## 概要

NFC Wallet SDK は実行時ログを収集し、アプリケーションのサンドボックスに保存します。

デジタルウォレットアプリケーションは、ログファイルを取得して、トラブルシューティングのために Thales と共有できます。

NFC Wallet SDK 6.14.0 以降では、デフォルト設定でログ記録が自動的に有効になります。

## SDK の統合

### ログサービスを取得する

{% hint style="warning" %}
Secure logger API は非推奨です。LogService を使用する必要があります。

参照 [secure logger api からの移行](#migrate-from-secure-logger-api) 詳細については、
{% endhint %}

取得してください `LogService` インスタンス:

{% tabs %}
{% tab title="Java" %}

```java
LogService logService = SDKInitializer.getLogService(context);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val logService = SDKInitializer.getLogService(context)
```

{% endtab %}
{% endtabs %}

SDK は、SDK の初期化中にデフォルト設定を自動的に適用します。

###

{% tabs %}
{% tab title="Java" %}

```java
// 6.14.0 より前
SecureLog secureLog = SDKInitializer.INSTANCE.configureSecureLog(
        new SecureLogConfig.Builder(context)
                .build()
);

secureLog.setLevel(SecureLogLevel.INFO);
List<File> files = secureLog.getFiles();
secureLog.deleteFiles();

// 6.14.0 以降
LogService logService = SDKInitializer.getLogService(context);

logService.setLevel(LogService.Level.INFO);
List<File> files = logService.getFiles();
logService.deleteFiles();
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
// 6.14.0 より前
val secureLog = SDKInitializer.INSTANCE.configureSecureLog(
    SecureLogConfig.Builder(context).build()
)

secureLog.setLevel(SecureLogLevel.INFO)
val files = secureLog.files
secureLog.deleteFiles()

// 6.14.0 以降
val logService = SDKInitializer.getLogService(context)

logService.setLevel(LogService.Level.INFO)
val files = logService.files
logService.deleteFiles()
```

{% endtab %}
{% endtabs %}

### ログレベルを変更する

使用 `setLevel(...)` を使ってログレベルを変更します。デフォルトのログレベルは `Level.WARN`.

{% tabs %}
{% tab title="Java" %}

```java
LogService logService = SDKInitializer.getLogService(context);
logService.setLevel(LogService.Level.INFO);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val logService = SDKInitializer.getLogService(context)
logService.setLevel(LogService.Level.INFO)
```

{% endtab %}
{% endtabs %}

レベルを `OFF` に設定すると、新しいログエントリの書き込みを停止します。

### ログレベル

ユースケースを満たす最も低いレベルを使用してください。

| レベル     | 数値 | 使用                                     |
| ------- | -- | -------------------------------------- |
| `ALL`   | 7  | すべてのログエントリを取得します。 `DEBUG`.             |
| `DEBUG` | 7  | トラブルシューティング用の詳細ログを取得します。短期間のみ使用してください。 |
| `INFO`  | 6  | 通常の運用ログを取得します。                         |
| `ERROR` | 3  | 調査が必要な実行時エラーを取得します。                    |
| `WARN`  | 4  | 警告を取得します。これはデフォルトのレベルです。               |
| `FATAL` | 2  | 回復不能な実行時エラーのみを取得します。                   |
| `OFF`   | 0  | ログ記録を無効にします。                           |

{% hint style="info" %}
使用 `DEBUG` 短時間のトラブルシューティングセッションでのみ

通常運用では、デフォルトレベルを維持してください `WARN`.
{% endhint %}

### ログ記録を無効にする

NFC Wallet SDK 6.14.0 以降では、ログ記録がデフォルトで有効になっています。

レベルを `OFF` 無効にするには:

{% tabs %}
{% tab title="Java" %}

```java
LogService logService = SDKInitializer.getLogService(context);
logService.setLevel(LogService.Level.OFF);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val logService = SDKInitializer.getLogService(context)
logService.setLevel(LogService.Level.OFF)
```

{% endtab %}
{% endtabs %}

### ログファイルを取得する

取得後、 `LogService` インスタンスを呼び出して `getFiles()`:

{% tabs %}
{% tab title="Java" %}

```java
LogService logService = SDKInitializer.getLogService(context);

// ファイルのリストを返す
List<File> files = logService.getFiles();
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val logService = SDKInitializer.getLogService(context)

// ファイルのリストを返す
val files = logService.files
```

{% endtab %}
{% endtabs %}

呼び出し `deleteFiles()` を使ってログファイルを削除します:

{% tabs %}
{% tab title="Java" %}

```java
LogService logService = SDKInitializer.getLogService(context);
logService.deleteFiles();
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val logService = SDKInitializer.getLogService(context)
logService.deleteFiles()
```

{% endtab %}
{% endtabs %}

### 完全な例

この例では、ログサービスのインスタンスを取得し、すべてのログファイルを zip 化して、Android の共有シートを使って共有します。

{% tabs %}
{% tab title="Java" %}
{% code title="LogServiceHelper.java" expandable="true" %}

```java
public class LogServiceHelper {

    private static LogService getLogService(Context context) {
        return SDKInitializer.getLogService(context);
    }

    public static void setDebugLogLevel(Context context) {
        LogService logService = getLogService(context);
        logService.setLevel(LogService.Level.DEBUG);
    }

    public static void shareLogs(Activity activity) {
        LogService logService = getLogService(activity.getApplicationContext());
        List<File> files = logService.getFiles();
        if (files == null || files.isEmpty()) {
            // 共有するログはありません
            return;
        }

        File directory = files.get(0).getParentFile();
        String outputZipPath = activity.getCacheDir().getAbsolutePath() + "/temp.zip";

        Thread zipTask = new Thread(new Runnable() {
            @Override
            public void run() {
                File file = new File(outputZipPath);
                if (file.exists()) {
                    file.delete();
                }

                zipFolder(directory.getAbsolutePath(), outputZipPath);

                if (!file.exists() || !file.canRead()) {
                    return;
                }

                // FileProvider を使用する場合は、アプリケーションのマニフェストに宣言してください。
                Uri uri = FileProvider.getUriForFile(activity, "your.fileprovider.authority", file);

                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("application/zip");
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, "ログファイル");
                shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                activity.startActivity(Intent.createChooser(shareIntent, "ログを共有"));
            }
        });

        zipTask.start();
    }

    private static void zipFolder(String inputFolderPath, String outZipPath) {
        try {
            FileOutputStream fos = new FileOutputStream(outZipPath);
            ZipOutputStream zos = new ZipOutputStream(fos);
            File srcFile = new File(inputFolderPath);
            File[] files = srcFile.listFiles();

            for (int i = 0; i < files.length; i++) {
                byte[] buffer = new byte[1024];
                FileInputStream fis = new FileInputStream(files[i]);
                zos.putNextEntry(new ZipEntry(files[i].getName()));
                int length;
                while ((length = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, length);
                }
                zos.closeEntry();
                fis.close();
            }
            zos.close();
        } catch (IOException ioe) {
            Log.e("", ioe.getMessage());
        }
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code title="LogServiceHelper.kt" expandable="true" %}

```kotlin
object LogServiceHelper {

    private fun getLogService(context: Context): LogService {
        return SDKInitializer.getLogService(context)
    }

    fun setDebugLogLevel(context: Context) {
        val logService = getLogService(context)
        logService.setLevel(LogService.Level.DEBUG)
    }

    fun shareLogs(activity: Activity) {
        val logService = getLogService(activity.applicationContext)
        val files = logService.files
        if (files.isNullOrEmpty()) {
            // 共有するログはありません
            return
        }

        val directory = files[0].parentFile
        val outputZipPath = activity.cacheDir.absolutePath + "/temp.zip"

        val zipTask = Thread {
            val file = File(outputZipPath)
            if (file.exists()) {
                file.delete()
            }

            zipFolder(directory.absolutePath, outputZipPath)

            if (!file.exists() || !file.canRead()) {
                return@Thread
            }

            // FileProvider を使用する場合は、アプリケーションのマニフェストに宣言してください。
            val uri = FileProvider.getUriForFile(activity, "your.fileprovider.authority", file)

            val shareIntent = Intent(Intent.ACTION_SEND).apply {
                type = "application/zip"
                putExtra(Intent.EXTRA_SUBJECT, "ログファイル")
                putExtra(Intent.EXTRA_STREAM, uri)
                addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            }

            activity.startActivity(Intent.createChooser(shareIntent, "ログを共有"))
        }

        zipTask.start()
    }

    private fun zipFolder(inputFolderPath: String, outZipPath: String) {
        try {
            val fos = FileOutputStream(outZipPath)
            val zos = ZipOutputStream(fos)
            val srcFile = File(inputFolderPath)
            val files = srcFile.listFiles() ?: return

            for (currentFile in files) {
                val buffer = ByteArray(1024)
                val fis = FileInputStream(currentFile)
                zos.putNextEntry(ZipEntry(currentFile.name))
                var length: Int
                while (fis.read(buffer).also { length = it } > 0) {
                    zos.write(buffer, 0, length)
                }
                zos.closeEntry()
                fis.close()
            }
            zos.close()
        } catch (ioe: IOException) {
            Log.e("", ioe.message ?: "")
        }
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

### secure logger API からの移行

非推奨の設定コードを削除します。

その後、必要なときに `LogService` インスタンスを取得します。

| 非推奨 API                                                       | 置き換え                                      |
| ------------------------------------------------------------- | ----------------------------------------- |
| `SDKInitializer.INSTANCE.configureSecureLog(SecureLogConfig)` | `SDKInitializer.getLogService(Context)`   |
| `SecureLogConfig.Builder(...)`                                | 置き換えはありません。SDK はデフォルト設定を使用します。            |
| `secureLog.setLevel(SecureLogLevel.X)`                        | `logService.setLevel(LogService.Level.X)` |
| `secureLog.getFiles()`                                        | `logService.getFiles()`                   |
| `secureLog.deleteFiles()`                                     | `logService.deleteFiles()`                |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.payments.thalescloud.io/nfc-wallet-sdk-android/ja/additional-features/collect-logs-with-secure-logger.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
