Skip to content

docs: add custom logger guide#3473

Merged
l2ysho merged 7 commits intov4from
3460-docs-add-custom-logger-example
Mar 17, 2026
Merged

docs: add custom logger guide#3473
l2ysho merged 7 commits intov4from
3460-docs-add-custom-logger-example

Conversation

@l2ysho
Copy link
Contributor

@l2ysho l2ysho commented Mar 10, 2026

closes #3460

@l2ysho l2ysho force-pushed the 3460-docs-add-custom-logger-example branch from bcc8f4e to ca03e8b Compare March 10, 2026 09:54
add pino and winston
@l2ysho l2ysho force-pushed the 3460-docs-add-custom-logger-example branch from ca03e8b to 82f2c92 Compare March 10, 2026 09:57
@l2ysho l2ysho marked this pull request as ready for review March 13, 2026 07:47
@l2ysho l2ysho self-assigned this Mar 13, 2026
@l2ysho l2ysho requested review from barjin and janbuchar and removed request for janbuchar March 13, 2026 07:47
@l2ysho l2ysho added this to the v4 milestone Mar 13, 2026
Copy link
Member

@barjin barjin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @l2ysho , it looks mostly fine to me 👍

I left some nits below:

All Crawlee logging goes through the <ApiLink to="core/interface/CrawleeLogger">`CrawleeLogger`</ApiLink> interface. To plug in your own logger, extend the <ApiLink to="core/class/BaseCrawleeLogger">`BaseCrawleeLogger`</ApiLink> abstract class and implement two methods:

- **`logWithLevel(level, message, data)`** — dispatches a log message to your logging library. The `level` parameter uses <ApiLink to="core/enum/LogLevel">`LogLevel`</ApiLink> constants (`ERROR = 1`, `SOFT_FAIL = 2`, `WARNING = 3`, `INFO = 4`, `DEBUG = 5`, `PERF = 6`). Map these to your logger's native levels.
- **`createChild(options)`** — creates a child logger instance. Crawlee creates child loggers with prefixes (e.g. `CheerioCrawler`, `AutoscaledPool`, `SessionPool`) so each internal component is easily identifiable in the output.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity - why does the implementer need to implement this method?

Describing the fields on the options type (or adding a link to the API docs) might clarify this a little.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this deserves more clarity. main reason for createChild is we use child loggers on multiple places.


logWithLevel(level: number, message: string, data?: Record<string, unknown>): void {
const pinoLevel = CRAWLEE_TO_PINO[level] ?? 'info';
const prefix = this.getOptions().prefix;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the prefix option requires some special handling.

Can we / is it worth mentioning this somewhere in the guide? Are there any other options fields that require special care?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is good catch, it is not needed. In crawlee we never use prefix directly with log function, we use prefix only through createChild (and then it is handled automatically by child instance).


All Crawlee logging goes through the <ApiLink to="core/interface/CrawleeLogger">`CrawleeLogger`</ApiLink> interface. To plug in your own logger, extend the <ApiLink to="core/class/BaseCrawleeLogger">`BaseCrawleeLogger`</ApiLink> abstract class and implement two methods:

- **`logWithLevel(level, message, data)`** — dispatches a log message to your logging library. The `level` parameter uses <ApiLink to="core/enum/LogLevel">`LogLevel`</ApiLink> constants (`ERROR = 1`, `SOFT_FAIL = 2`, `WARNING = 3`, `INFO = 4`, `DEBUG = 5`, `PERF = 6`). Map these to your logger's native levels.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, can we have a description of what message and data types are (and how do these differ)?

Copy link
Contributor

@janbuchar janbuchar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take care of Jindra's comments. LGTM otherwise.

@l2ysho l2ysho requested a review from barjin March 16, 2026 15:46
Copy link
Member

@barjin barjin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thank you @l2ysho !

@l2ysho l2ysho merged commit 697dcf2 into v4 Mar 17, 2026
6 checks passed
@l2ysho l2ysho deleted the 3460-docs-add-custom-logger-example branch March 17, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants