Skip to content

Commit 244c277

Browse files
Pijukatelbarjin
andauthored
feat: Enable log redirection and update docs (#522)
- Bump the `apify-client` dependency to enable log redirection. - Add docs. --------- Co-authored-by: Jindřich Bär <[email protected]>
1 parent 84b1ce8 commit 244c277

File tree

4 files changed

+93
-40
lines changed

4 files changed

+93
-40
lines changed

docs/guides/log_redirection.mdx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
id: log-redirection
3+
title: Log redirection
4+
description: Redirect logs from another Actor run
5+
---
6+
7+
import ApiLink from '@site/src/components/ApiLink';
8+
9+
In some situations, one Actor is going to start one or more other Actors and wait for them to finish and produce some results. In such cases, you might want to redirect the logs of the started Actors' runs back to the parent Actor run, so that you can see the progress of the started Actors' runs in the parent Actor's logs. This guide will show possibilities on how to do it.
10+
11+
### Redirecting logs from `Actor.call`
12+
13+
Typical use case for log redirection is to call another Actor using the [`Actor.call()`](/reference/class/Actor#call) method. This method has an optional logger argument, which is by default set to the `default` literal. This means that the logs of the called Actor will be automatically redirected to the parent Actor's logs with default formatting and filtering. If you set the logger argument to `null`, then no log redirection happens. The third option is to pass your own `Log` instance with the possibility to define your specific logging. Below you can see those three possible ways of log redirection when starting another Actor run through Actor.call.
14+
15+
```javascript
16+
import { Actor } from 'apify';
17+
import { LoggerActorRedirect } from 'apify-client';
18+
import { LEVELS, Log } from '@apify/log';
19+
20+
const input = {}; // Some Actor input
21+
const actorId = 'someActorId'; // ID of actor you want to call
22+
23+
await Actor.init();
24+
25+
// Default log redirection - implicitly
26+
await Actor.call(actorId, input);
27+
28+
// Default log redirection - explicitly
29+
await Actor.call(actorId, input, { log: 'default' });
30+
31+
// No log redirection
32+
await Actor.call(actorId, input, { log: null });
33+
34+
// Custom log redirection
35+
await Actor.call(actorId, input, {
36+
log: new Log({
37+
level: LEVELS.DEBUG,
38+
prefix: 'customPrefix',
39+
logger: new LoggerActorRedirect(),
40+
}),
41+
});
42+
43+
await Actor.exit();
44+
```
45+
46+
Each default redirect log entry has a specific format. First part is a cyan-colored text with the redirect information (the other Actor's name and the run ID), the rest of the log message is printed in the same manner as the parent Actor's log is configured.
47+
48+
The log redirection can be deep, meaning that if the other Actor also starts another actor and is redirecting logs from it, then in the top-level Actor, you can see it as well.
49+
50+
### Redirecting logs from already running Actor run
51+
52+
In some cases, you might want to connect to an already running Actor run and redirect its logs to your current Actor run. This can be done using the <ApiLink to="class/ApifyClient">`ApifyClient`</ApiLink> and getting the streamed log from a specific Actor run. You can then control the log redirection manually by explicitly calling `start` and `stop` methods.
53+
54+
You can further decide whether you want to redirect just new logs of the ongoing Actor run, or if you also want to redirect historical logs from that Actor's run, so all logs it has produced since it was started. This can be controlled by the `fromStart` option of the `getStreamedLog()` method. If you set it to `true`, all historical logs will be redirected first, and then new logs will be redirected as they are produced. If you set it to `false`, only new logs will be redirected.
55+
56+
```javascript
57+
import { Actor } from 'apify';
58+
59+
const actorRunId = 'someRunId'; // ID of the run you want to connect to
60+
61+
await Actor.init();
62+
63+
// Redirect only new logs from the other Actor run, due to `fromStart: false`
64+
const streamedLog = await Actor.newClient()
65+
.run(actorRunId)
66+
.getStreamedLog({ fromStart: false });
67+
68+
streamedLog.start();
69+
// Do some stuff while also redirecting logs from another Actor
70+
await streamedLog.stop();
71+
72+
await Actor.exit();
73+
```

package-lock.json

Lines changed: 18 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"@types/semver": "^7.5.8",
7878
"@types/tough-cookie": "^4.0.5",
7979
"@types/ws": "^8.5.12",
80-
"apify-client": "^2.17.0",
80+
"apify-client": "^2.20.0",
8181
"commitlint": "^20.0.0",
8282
"crawlee": "^3.13.5",
8383
"eslint": "^9.23.0",

website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = {
2424
'guides/pay-per-event',
2525
'guides/type-script-actor',
2626
'guides/docker-images',
27+
'guides/log-redirection',
2728
],
2829
},
2930
{

0 commit comments

Comments
 (0)