Customising logging
# crawlee-js
j
Is there a recommended way to customise logging? I want to be able to log which specific crawler and which handler a log is coming from. I have tried to override the logger in the crawler using
Copy code
import defaultLog, { Log } from '@apify/log';
...
const crawler = new BasicCrawler({
  requestHandler: router,
  log: defaultLog.child({ prefix: 'MyCrawler' })
})
but then I get the following type error:
Copy code
Type 'import("scrapers/node_modules/@apify/log/esm/index", { with: { "resolution-mode": "import" } }).Log' is not assignable to type 'import("scrapers/node_modules/@apify/log/cjs/index").Log'.
  Types have separate declarations of a private property 'options'.ts(2322)
Thanks!
h
Someone will reply to you shortly. In the meantime, this might help: -# This post was marked as solved by Matous. [View answer]().
l
I think it should work if you get the log from
'apify'
instead of
'@apify/log'
.
Copy code
import { log } from 'apify';
import { BasicCrawler } from 'crawlee';

const crawler = new BasicCrawler({
    log: log.child({ prefix: 'MyCrawler' }),
    requestHandler: ({ log: myLog, request }) => {
        myLog.info(`Check this out! ${request.url}`);
    },
});
This works fine for me. I think there is something broken with the import path. I'll pass this to the engineering team but let me know if this works for you first.
j
@luigi.ruocco This works for me! Had to of course install the apify package separately though which isn't ideal as I only need crawlee/logging but did the trick
n
@je no need for the apify package:
Copy code
javascript
import { log, LogLevel } from 'crawlee'
log.setOptions({
    logger: customLogger,
    level: LogLevel.DEBUG,
})