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

Implement support for json-only logs #193

Closed
wants to merge 2 commits into from
Closed

Conversation

lundibundi
Copy link
Member

@lundibundi lundibundi commented Mar 19, 2022

  • tests and linter show no problems (npm t)
  • tests are added/updated for bug fixes and new features
  • code is properly formatted (npm run fmt)
  • description of changes is added in CHANGELOG.md
  • update .d.ts typings

Sample output
image

@tshemsedinov
Copy link
Member

It's not a level of logging, it's rather type in metalog, so it is not hierarchical or nested, it works just like a label or tag. @lundibundi

class Console {
constructor(write) {
this._write = write;
this._groupIndent = '';
this._groupIndent = 0;
Copy link
Member

Choose a reason for hiding this comment

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

_groupIndent is precalculated string, if you need a number we may add _groupIndentCount

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 considerend this to be a very "narrow" use-case to optimize for. I'd expect for it to be used very infrequently and in examples mostly.
IMO I'm not sure if we need to support this at all for 'logger'.

metalog.js Outdated
@@ -89,7 +92,7 @@ class Console {
try {
console.assert(assertion, ...args);
} catch (err) {
this._write('error', `${this._groupIndent}${err.stack}`);
this._write('error', this._groupIndent, err.stack);
Copy link
Member

Choose a reason for hiding this comment

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

Can't catch an idea why do you pass this._groupIndent as an argument to _write

Copy link
Member Author

Choose a reason for hiding this comment

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

Because the formatter has to decide what to do with this indent. (i.e. json - ignore, pretty - indent everything).
This also allows to properly indent multiline messages if we need it later.

Copy link
Member

Choose a reason for hiding this comment

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

Dropping indentation will be ok, but as a separate PR 😄

Copy link
Member

Choose a reason for hiding this comment

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

Do we need to pass it as a parameter? Maybe we can read it from property?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually yes, we can add it in this._write.

Copy link
Member Author

@lundibundi lundibundi Mar 19, 2022

Choose a reason for hiding this comment

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

Though we didn't have "indent" for count and table methods and this change will add them there.
image

Comment on lines -179 to -185
trace(...args) {
const msg = util.format(...args);
const err = new Error(msg);
this._write('debug', `${this._groupIndent}Trace${err.stack}`);
}

warn(...args) {
Copy link
Member

Choose a reason for hiding this comment

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

Why do you drop trace and warn methods?

Copy link
Member Author

Choose a reason for hiding this comment

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

They are just moved to have them near other log (info, debug, error etc) methods

return `${dateTime} [${type}] ${msg}`;
}

formatJson(type, indent, ...args) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it's better to have a collection of formatters not multiple methods in Logger class

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but it is hard to "connect" that to current stdout-vs-file separation.

@tshemsedinov
Copy link
Member

@lundibundi I am going to land this

tshemsedinov pushed a commit that referenced this pull request Mar 20, 2022
@tshemsedinov
Copy link
Member

Landed in 33695c4

@tshemsedinov tshemsedinov deleted the impl-json-logging branch March 20, 2022 19:54
@tshemsedinov
Copy link
Member

@lundibundi Can you please remove timestamp printed at the beginning of each line as a separate PR because we have a json field so it duplicates?

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.

2 participants