Logging
The client logs through @apify/log, the logger the Apify SDK and Crawlee use too. Each ApifyClient 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. - 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
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() logs through the logger option of its own constructor. For details, see HTTP clients.
Redirecting Actor run logs
In Node.js, ActorClient.call() redirects the log of the run it starts to your process while it waits 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
Loginstance of@apify/logreceives the redirected lines. nullturns 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(), RunClient.getStreamedLog() returns a StreamedLog 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();
For more on configuring the logger, see the @apify/log README.