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

feat(core): add customizable LoggerContext with labeling support #4233

Merged
merged 10 commits into from Apr 25, 2023

Conversation

SpencerKaiser
Copy link
Contributor

Fixes #4230

@SpencerKaiser SpencerKaiser changed the base branch from master to v6 April 17, 2023 22:52
@codecov
Copy link

codecov bot commented Apr 17, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.01 ⚠️

Comparison is base (9c3e4c4) 99.48% compared to head (01134bf) 99.47%.

❗ Current head 01134bf differs from pull request most recent head bd24480. Consider uploading reports for the commit bd24480 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##               v6    #4233      +/-   ##
==========================================
- Coverage   99.48%   99.47%   -0.01%     
==========================================
  Files         219      219              
  Lines       14361    14367       +6     
  Branches     3277     3281       +4     
==========================================
+ Hits        14287    14292       +5     
- Misses         73       74       +1     
  Partials        1        1              
Impacted Files Coverage Δ
packages/core/src/drivers/IDatabaseDriver.ts 100.00% <ø> (ø)
packages/core/src/logging/DefaultLogger.ts 100.00% <100.00%> (ø)
packages/core/src/logging/SimpleLogger.ts 100.00% <100.00%> (ø)
packages/knex/src/AbstractSqlConnection.ts 100.00% <100.00%> (ø)
packages/knex/src/AbstractSqlDriver.ts 100.00% <100.00%> (ø)
packages/knex/src/query/QueryBuilder.ts 99.54% <100.00%> (+<0.01%) ⬆️
packages/mongodb/src/MongoConnection.ts 100.00% <100.00%> (ø)

... and 3 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@SpencerKaiser
Copy link
Contributor Author

@B4nan I’m having a really difficult time tracking how data passes through the different abstract classes and where the context actually gets passed to logger.logQuery.

The changes to AbstractSqlDriver and QueryBuilder are the changes I think I need to get that data to logQuery, but not sure. I tried to test locally by bringing those changes into a real project but yarn link was seemingly breaking everything due to some strange types issue… I built the project, ran the link command from inside core, made sure all my other dependencies were on 58, and successfully got it linked into my other, but I get weird type errors around Loaded entities:
Types have separate declarations of a private property 'entity'

Any idea what I’m doing wrong there? Any suggestions on the approach?

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

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

i am usually going the old school way, compile the package and just copy paste the dist folder inside node_modules, maybe that will work? the error sounds like you have some private property with different type, maybe you need to link more packages than just core? as you did changes in knex package too

tests/Logger.test.ts Outdated Show resolved Hide resolved
@SpencerKaiser SpencerKaiser marked this pull request as ready for review April 19, 2023 01:15
@SpencerKaiser SpencerKaiser force-pushed the logger-context branch 7 times, most recently from e10eba6 to 620c5d8 Compare April 19, 2023 01:45
@@ -50,3 +51,5 @@ export interface LoggerOptions {
highlighter?: Highlighter;
usesReplicas?: boolean;
}

export type LoggerContext = Pick<LogContext, 'label'>;
Copy link
Member

Choose a reason for hiding this comment

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

maybe better this way so its universal way to pass params down to the logger?

Suggested change
export type LoggerContext = Pick<LogContext, 'label'>;
export type LoggerContext = { label?: string } & Dictionary;

and add a note about that to the section about custom loggers

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 change was also needed for LogContext... we might consider refactoring these at some point... one acts as a sort of public type where the other is more focused on internals unless someone uses LoggerFactory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also I personally prefer picking the label just to keep as much overlap as possible. If you still want that to be unique on it's own, let me know and I can fix it

@@ -608,7 +610,7 @@ export class QueryBuilder<T extends object = AnyEntity> {
}

const type = this.connectionType || (method === 'run' ? 'write' : 'read');
const res = await this.driver.getConnection(type).execute(query.sql, query.bindings as any[], method, this.context);
Copy link
Member

Choose a reason for hiding this comment

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

the same needs to be supported in mongo driver

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See my comment below 😬

tests/Logger.test.ts Outdated Show resolved Hide resolved
packages/knex/src/AbstractSqlDriver.ts Show resolved Hide resolved
@B4nan B4nan changed the title v6: Adding LoggerContext to allow for custom labels in log output feat(core): add customizable LoggerContext with labeling support Apr 22, 2023
@B4nan
Copy link
Member

B4nan commented Apr 22, 2023

Will you have time to do the mongo support? We can merge without it too, I'll be happy to do it myself.

Planning to ship v5.7 tomorrow probably, and want to rebase v6 again afterward, so would be good to merge this before instead of battling the rebases again :]

@SpencerKaiser
Copy link
Contributor Author

@B4nan sorry for the delay, I went on a mini vacation for the weekend + today; I'll work on it here shortly and/or tomorrow. I might have a question or two about it, but if I do I'll follow up in Slack for expediency!

Re: rebasing; feel free to rebase main, just don't pull it into this branch... when that happens, I'll see the "Update branch" button and know to reset/stash/delete/recreate 😬

@SpencerKaiser
Copy link
Contributor Author

@B4nan just added what I think are the only changes needed for Mongo, but I'm not able to test tonight (don't have a quick repo to test against and I'm calling it for the night).

Also I made some pretty substantial changes to the logging docs because I felt they were a little all over the place after adding more so I just reorganized. Hope you like it; if not, let me know and I can revert back to the old structure but with the new content.

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

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

few nits before i have time to review this fully

thanks for the docs improvements, that's amazing!

packages/core/src/logging/Logger.ts Show resolved Hide resolved
docs/docs/logging.md Show resolved Hide resolved
docs/docs/logging.md Outdated Show resolved Hide resolved
SpencerKaiser and others added 2 commits April 25, 2023 08:25
Co-authored-by: Martin Adámek <banan23@gmail.com>
@SpencerKaiser
Copy link
Contributor Author

Also re: tests, looks like they're all failing due to this error:
[Error: No driver specified, please fill in the `driver` option or use `defineConfig` helper (to define your ORM config) or `MikroORM` class (to call the `init` method) exported from the driver package (e.g. `import { defineConfig } from '@mikro-orm/mysql'; export defineConfig({ ... })`).]

But I didn't change anything related to the CLI 🤔 thoughts?

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

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

the tests here are failing because you have out of sync lock file, probably because of the version change i mentioned

packages/core/package.json Outdated Show resolved Hide resolved
@SpencerKaiser
Copy link
Contributor Author

Test are passing locally again! Should be ready for that through review once you have time 🙂 no rush though!

docs/docs/logging.md Outdated Show resolved Hide resolved
docs/docs/logging.md Outdated Show resolved Hide resolved
docs/docs/logging.md Outdated Show resolved Hide resolved
docs/docs/logging.md Outdated Show resolved Hide resolved
docs/docs/logging.md Outdated Show resolved Hide resolved
docs/docs/logging.md Outdated Show resolved Hide resolved
SpencerKaiser and others added 3 commits April 25, 2023 16:14
Co-authored-by: Martin Adámek <banan23@gmail.com>
Co-authored-by: Martin Adámek <banan23@gmail.com>
@B4nan B4nan merged commit c56f90f into mikro-orm:v6 Apr 25, 2023
6 checks passed
@SpencerKaiser SpencerKaiser deleted the logger-context branch April 25, 2023 21:51
jsprw pushed a commit to jsprw/mikro-orm-full-text-operators that referenced this pull request May 7, 2023
@B4nan B4nan mentioned this pull request Sep 30, 2023
22 tasks
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.

Add context to config logger property
2 participants