Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Ensure LoggerProxy is not scoped #11379

Merged
merged 1 commit into from
Oct 24, 2024

Conversation

ivov
Copy link
Contributor

@ivov ivov commented Oct 24, 2024

Currently we initialize a Logger singleton shared by most services. More recently, we allowed some services to use Logger.scoped to instantiate a scoped logger to auto-inject scopes like 'waiting-executions' into the scoped logger's log metadata.

In Logger.constructor we also initialize a LoggerProxy for use by workflow and core packages. Since Logger.scoped calls Logger.constructor we are unintentionally re-initializing LoggerProxy on every Logger.scoped call, which causes LoggerProxy to end up with the scopes of the last Logger.scoped call, e.g. LoggerProxy in core in regular mode is currently auto-injecting the waiting-executions scope.

This PR ensures LoggerProxy is only initialized by the root Logger.constructor so that LoggerProxy does not receive scopes from further constructor calls.

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Oct 24, 2024
@@ -61,7 +62,7 @@ export class Logger {
/** Create a logger that injects the given scopes into its log metadata. */
scoped(scopes: LogScope | LogScope[]) {
scopes = Array.isArray(scopes) ? scopes : [scopes];
const scopedLogger = new Logger(this.globalConfig, this.instanceSettings);
const scopedLogger = new Logger(this.globalConfig, this.instanceSettings, { isRoot: false });
Copy link
Member

Choose a reason for hiding this comment

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

do you think it might make sense to move the minimal amount of code needed for scoped loggers to a separate class, instead of reusing this class for two separate purposes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting, let me try this and report back

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried this quickly here. The scoped logger relies heavily on the existing logger, so

  • if the scoped logger doesn't extend from the existing logger, we'd have to extract quite a bit of deeply integrated logic or duplicate quite a bit, also using a separate scoped logger like this introduces a circular dependency ReferenceError: Circular dependency: TargetServer. Index: 0 that I haven't spent time debugging yet
  • if the scoped logger does extend from the existing logger to reuse logic and avoid extracting most of it, then we're back at wanting a way to avoid initializing the proxy on every constructor call, which is what this PR is addressing

Let me know if worth investing more time into this

Copy link
Member

Choose a reason for hiding this comment

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

does the scoped logger also need to use winston to write logs to the file?

also, if you prefer, we can merge this as it is, and I can have a look at splitting this in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

does the scoped logger also need to use winston to write logs to the file?

Yes, the scoped logger does everything the existing logger does, with the only addition that it auto-injects the scope string into the metadata of every log it emits, no matter the transport, etc.

also, if you prefer, we can merge this as it is, and I can have a look at splitting this in a separate PR.

Let's do this and revisit. I'd like to fix this minor bug quickly.

Copy link

cypress bot commented Oct 24, 2024

n8n    Run #7544

Run Properties:  status check passed Passed #7544  •  git commit 9f83313506: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Project n8n
Branch Review ensure-logger-proxy-is-not-scoped
Run status status check passed Passed #7544
Run duration 04m 12s
Commit git commit 9f83313506: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Committer Iván Ovejero
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 458
View all changes introduced in this branch ↗︎

Copy link
Contributor

✅ All Cypress E2E specs passed

@ivov ivov merged commit f4ea943 into master Oct 24, 2024
78 checks passed
@ivov ivov deleted the ensure-logger-proxy-is-not-scoped branch October 24, 2024 10:53
This was referenced Oct 28, 2024
@janober
Copy link
Member

janober commented Oct 31, 2024

Got released with n8n@1.66.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team Released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants