# Logging

Copy for LLM

The client logs through [`@apify/log`](https://www.npmjs.com/package/@apify/log), the logger the Apify SDK and Crawlee use too. Each [`ApifyClient`](https://docs.apify.com/api/client/js/reference/class/ApifyClient.md) creates a child of the package's default logger with the `ApifyClient` prefix and exposes it as `client.logger`. The client logs only warnings and errors, for example:

* A request that has failed half of its allowed attempts, with the cause of the last failure.
* A request timeout above `timeoutMaxSecs`, which the client caps. For details, see [Timeouts](https://docs.apify.com/api/client/js/docs/concepts/timeouts.md).
* A request with a stream body, which the client can't retry because the failed attempt has already consumed part of the stream.

## Configuring the logger[](#configuring-the-logger)

To configure the logger, install `@apify/log` alongside the client:

```
npm i @apify/log
```

The `client.logger` is the same logger the built-in HTTP client writes to, so a change to it applies to every request the client sends. To change the log level, call `setLevel()`:

```
import { LogLevel } from '@apify/log';

import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });



// Print errors only. `LogLevel.OFF` silences the client entirely.

client.logger.setLevel(LogLevel.ERROR);
```

The `APIFY_LOG_LEVEL` environment variable sets the level without any code change, for example `APIFY_LOG_LEVEL=ERROR`. The variable applies when the logger is created, so `setLevel()` overrides it.

By default, the logger prints human-readable lines. To print JSON instead, set the logger implementation to `LoggerJson`:

```
import { LoggerJson } from '@apify/log';

import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });



// Print every log line as a JSON object, for log collectors that parse structured output.

client.logger.setOptions({ logger: new LoggerJson() });
```

An HTTP client you plug in with [`ApifyClient.withCustomHttpClient()`](https://docs.apify.com/api/client/js/reference/class/ApifyClient.md#withCustomHttpClient) logs through the `logger` option of its own constructor. For details, see [HTTP clients](https://docs.apify.com/api/client/js/docs/concepts/custom-http-clients.md).

## Redirecting Actor run logs[](#redirecting-actor-run-logs)

In Node.js, [`ActorClient.call()`](https://docs.apify.com/api/client/js/reference/class/ActorClient.md#call) and [`TaskClient.call()`](https://docs.apify.com/api/client/js/reference/class/TaskClient.md#call) redirect the log of the run they start to your process while they wait for the run to finish. The redirected lines carry a prefix with the Actor name and the run ID. The `log` option of `call()` changes where they go:

* A `Log` instance of `@apify/log` receives the redirected lines.
* `null` turns the redirection off.
* `'default'` or no value keeps the built-in logger.

```
import { Log } from '@apify/log';

import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });

const actorClient = client.actor('username/actor-name');



// Redirect the run log to a logger with your own prefix.

await actorClient.call(undefined, { log: new Log({ prefix: 'My Actor' }) });



// Turn the redirection off.

await actorClient.call(undefined, { log: null });
```

For a run you started with [`start()`](https://docs.apify.com/api/client/js/reference/class/ActorClient.md#start), [`RunClient.getStreamedLog()`](https://docs.apify.com/api/client/js/reference/class/RunClient.md#getStreamedLog) returns a [`StreamedLog`](https://docs.apify.com/api/client/js/reference/class/StreamedLog.md) that you start and stop yourself. It takes the same choice of logger through its `toLog` option. Log redirection needs Node.js, so outside it `getStreamedLog()` resolves to `undefined` and `call()` doesn't redirect anything.

```
import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });



const run = await client.actor('username/actor-name').start();

const runClient = client.run(run.id);



// Resolves to `undefined` outside Node.js, where the log can't be streamed.

const streamedLog = await runClient.getStreamedLog();



streamedLog?.start();

await runClient.waitForFinish();

await streamedLog?.stop();
```

By default, the whole log of the run is redirected. To redirect only part of it, pass a `Date` as `since`, and only the lines logged at that time or later are redirected.

If the log stream is interrupted while the run is still going, the client reopens it and continues after the last redirected line.

[`StreamedLog.lastRedirectedAt`](https://docs.apify.com/api/client/js/reference/class/StreamedLog.md#lastRedirectedAt) is the timestamp of the last redirected line. To continue the redirection in another process, for example after the Actor that redirects the log reboots, save it and pass a later time as `since`.

```
import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });

const RECORD_KEY = 'LOG_REDIRECTED_UNTIL';



const runClient = client.run('MY-RUN-ID');

const storeClient = client.keyValueStore('MY-STORE-ID');



// After a restart, continue after the last line redirected before it.

const record = await storeClient.getRecord(RECORD_KEY);

const since = record ? new Date(new Date(String(record.value)).getTime() + 1) : undefined;



const streamedLog = await runClient.getStreamedLog({ since });

streamedLog?.start();

await runClient.waitForFinish();

await streamedLog?.stop();



// Before a restart, save how far the redirection got. Here it is saved once the

// run finishes; save it on your own restart or migration signal instead.

if (streamedLog?.lastRedirectedAt) {

    await storeClient.setRecord({ key: RECORD_KEY, value: streamedLog.lastRedirectedAt.toISOString() });

}
```

For more on configuring the logger, see the [`@apify/log` README](https://www.npmjs.com/package/@apify/log).
