|
| 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 | +``` |
0 commit comments