> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# File logging

> Use the experimental Laravel Copilot SDK ServerRpc file logging API to write redacted logs through the runtime logger to a host-approved destination.

## File logging

Scope: ServerRpc (server-wide operations tied to the Client)

For an overview of RPC and the list of all RPC pages, see [RPC](https://kawax.biz/en/packages/laravel-copilot-sdk/rpc.md).

`logging()->file()` writes logs through the runtime logger to a destination that the native host allows. You can't specify a file path in the request. This API is experimental.

## Open a logger and write to it

`open()` returns an opaque `logId`. The runtime redacts the values you register in `secretValues` from each message. Let your writes complete in order, then call `flush()` or `close()`.

```php theme={null}
use Revolution\Copilot\Enums\FileLogLevel;
use Revolution\Copilot\Enums\FileLogOperation;
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\Rpc\LoggingFileOpenRequest;
use Revolution\Copilot\Types\Rpc\LoggingFileWriteRequest;

$fileLogger = Copilot::client()->rpc()->logging()->file();

$opened = $fileLogger->open(new LoggingFileOpenRequest(
    level: FileLogLevel::Info,
    debugEnvironmentVariables: ['APP_DEBUG'],
    secretValues: [$secret],
));

$fileLogger->write(new LoggingFileWriteRequest(
    logId: $opened->logId,
    operation: FileLogOperation::Info,
    message: 'Background task started',
));

$fileLogger->write(new LoggingFileWriteRequest(
    logId: $opened->logId,
    operation: FileLogOperation::StartGroup,
    message: 'Processing items',
    groupLevel: FileLogLevel::Debug,
));
```

`FileLogOperation` accepts `log`, `debug`, `info`, `notice`, `warning`, `error`, `startGroup`, and `endGroup`. For `endGroup`, pass an empty string as `message`.

## Flush and close

```php theme={null}
use Revolution\Copilot\Types\Rpc\LoggingFileCloseRequest;
use Revolution\Copilot\Types\Rpc\LoggingFileFlushRequest;

$fileLogger->flush(new LoggingFileFlushRequest($opened->logId));
$fileLogger->close(new LoggingFileCloseRequest($opened->logId));
```

`flush()` waits until all earlier writes reach the file and reports any failure. `close()` also flushes, then closes the logger. After closing, you can't use the same `logId` again.


## Related topics

- [ServerRpc overview](https://kawax.biz/en/packages/laravel-copilot-sdk/rpc/server-overview.md)
- [RPC (Remote Procedure Call)](https://kawax.biz/en/packages/laravel-copilot-sdk/rpc.md)
- [Logging](https://kawax.biz/en/logging.md)
- [File Storage](https://kawax.biz/en/filesystem.md)
- [Extending and re-resolving log channels in packages](https://kawax.biz/en/advanced/package-logging.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.