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

refactor(core): Move Logger to core (no-changelog) #12310

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

netroy
Copy link
Member

@netroy netroy commented Dec 19, 2024

Summary

This PR moved the Logger to n8n-core, so that we can stop using console.* and Loggerproxy in more places.

Review / Merge checklist

  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
  • PR Labeled with release/backport (if the PR is an urgent fix that needs to be backported)

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Dec 19, 2024
@netroy netroy requested a review from ivov December 19, 2024 14:56
Copy link
Contributor

@ivov ivov left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on! Code looks good, will give it a spin soon!

PS: I think we can omit (no-changelog) for refactor PRs

packages/cli/src/config/index.ts Outdated Show resolved Hide resolved
packages/core/src/InstanceSettingsConfig.ts Outdated Show resolved Hide resolved
if (!this.isScopingEnabled) return info;

const { scopes } = info.metadata;
const { scopes } = (info as unknown as { metadata: LogMetadata }).metadata;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this better than the original?

Copy link
Member Author

Choose a reason for hiding this comment

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

not sure why, but I'm getting this error
image

Copy link
Contributor

Choose a reason for hiding this comment

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

So confused, this should've been erroring out in cli then? Also the TransformableInfo type is left open [key: string | symbol]: any; so worst case we shouldn't need the unknown intermediary or? info.metadata as LogMetadata

@@ -0,0 +1,3 @@
export function isObjectLiteral(item: unknown): item is { [key: string]: unknown } {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit - better naming

Suggested change
export function isObjectLiteral(item: unknown): item is { [key: string]: unknown } {
export function isObjectLiteral(candidate: unknown): candidate is { [key: string]: unknown } {

Copy link
Member Author

Choose a reason for hiding this comment

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

we have an exact copy of this in cli. if we rename this, we should rename it in the other copy as well, so that if/when we deduplicate this code, it's not confusing for the person doing the refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're here let's keep only one copy in core and import it from elsewhere?

Comment on lines +2394 to +2404
export type LogMetadata = {
[key: string]: unknown;
scopes?: LogScope[];
file?: string;
function?: string;
};
export type Logger = Record<
Exclude<LogLevel, 'silent'>,
(message: string, metadata?: LogMetadata) => void
>;
export type LogLocationMetadata = Pick<LogMetadata, 'file' | 'function'>;
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Instead of picking from LogMetadata I wonder if it'd be more readable to define LogLocationMetadata as those two properties and intersect this type to create LogMetadata?
  2. I'm not clear just from this what Logger is here as opposed to the Logger service. The proxy? Maybe we can docline this?

Copy link
Member Author

Choose a reason for hiding this comment

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

I just copied this code over. I think we can refactor this bit in the next step when we get rid of more of the LoggerProxy code.

@netroy netroy requested a review from ivov December 19, 2024 16:29
if (!this.isScopingEnabled) return info;

const { scopes } = info.metadata;
const { scopes } = (info as unknown as { metadata: LogMetadata }).metadata;
Copy link
Contributor

Choose a reason for hiding this comment

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

So confused, this should've been erroring out in cli then? Also the TransformableInfo type is left open [key: string | symbol]: any; so worst case we shouldn't need the unknown intermediary or? info.metadata as LogMetadata

Copy link
Contributor

Choose a reason for hiding this comment

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

Why remove the .service. infix?

Copy link
Member Author

Choose a reason for hiding this comment

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

I prefer the file name and the DI class names to align. I can add the .service back, but then I prefer to rename the class to LoggerService. Would that be acceptable?

Copy link
Contributor

@ivov ivov Dec 20, 2024

Choose a reason for hiding this comment

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

Turning all the logger references to loggerService makes the code everywhere more verbose for little gain. Do we need to be so consistent? I always thought of the -Service suffix as an easy way to name classes that otherwise would be hard to name like WorklowService but that doesn't really apply for the logger.

If the alternative is making all code more verbose, I'd rather omit the .service. infix here. But I also notice that this is also inconsistent in that we're having some DI services infixed and some not.

Copy link
Member Author

Choose a reason for hiding this comment

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

we don't need to rename all variables. I just meant the class. So the only change would be change the class name in this file to LoggerService, and renaming the file back to logger.service.ts.
however I personally the Service suffix in this particular case not very useful.

Copy link
Contributor

Choose a reason for hiding this comment

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

however I personally the Service suffix in this particular case not very useful.

Agree! In any case if this renaming won't cause lots of changes everywhere, I'd be happy with what you decide.

@@ -0,0 +1,3 @@
export function isObjectLiteral(item: unknown): item is { [key: string]: unknown } {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're here let's keep only one copy in core and import it from elsewhere?

@netroy netroy requested a review from a team as a code owner December 20, 2024 15:11
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants