Skip to content
This repository was archived by the owner on Apr 21, 2022. It is now read-only.

Adds callback parameter to the log and other functions.#73

Merged
vilyapilya merged 8 commits intomasterfrom
addCallbackToLog
Oct 24, 2019
Merged

Adds callback parameter to the log and other functions.#73
vilyapilya merged 8 commits intomasterfrom
addCallbackToLog

Conversation

@vilyapilya
Copy link
Copy Markdown
Contributor

Adds an option to pass a callback when the log (or any other logging function) is called. It will pass the object with the response code and the response or an error to the callback after sending the request to the server.

@smusali smusali self-assigned this Oct 15, 2019
Copy link
Copy Markdown

@dchai76 dchai76 left a comment

Choose a reason for hiding this comment

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

This is going to take me some time to review, I'm going to start with just one comment

Comment thread lib/logger.js

if (opts) {
if (typeof opts === 'string') {
if (typeof opts === 'function') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We seem to do this a lot and it seems to me like a terrible idea - it's confusing to me that we can let people send in a callback function as the opts param. Wouldn't it be preferable to require them to send in an empty/null opts and be explicit about what callback is than this?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Didn't answer my question, but I really dislike this - let's keep each parameter as it's meant to be instead of pseudo-overloading.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sorry I did not see your comments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@dchai76 I think it's arguable. On one hand the implementation of the code library becomes harder to read and maintain. On the other hand, usage of our code libraries becomes easier (you don't have to write your code likes this logger.log("my lolg", {}, (err) => {if err stdout)}); every time when you need to pass the cb to the log function. I think it's better to take the burden of readability on us than pass it on users.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

and besides, we can also pass a level to that. And it can grow into log("logline", "", {}, cb). Which I think is not a really good practice.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

To be clear, I think this is bad for callers, not for our own maintainability - magic overloading can mask errors on their part. Did they mean to forget to include opts? Or was it intentional? The type of magic where we say one parameter can be treated in different ways is surprising, and not in a good way.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I personally don't like this pattern, but I do see this used in lots of places :(
I guess the best way to escape is the make it an async function that returns a promise, so up to the caller whether to chain it or not

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@weizou19 pointed out to me that Node itself does this (magic overloading of provided callbacks) (https://github.com/nodejs/node/blob/master/lib/fs.js#L405-L414) so as much as I dislike it I'm going to say I'm wrong on this.

Comment thread lib/logger.js

if (opts) {
if (typeof opts === 'string') {
if (typeof opts === 'function') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Didn't answer my question, but I really dislike this - let's keep each parameter as it's meant to be instead of pseudo-overloading.

Comment thread lib/logger.js
Comment thread lib/logger.js Outdated
callback = opts;
opts = {};
}
if (!opts) opts = {};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I prefer opts = opts || {}; format. If you do have it this way, let's bracket the clause in the if - 1 line if statements like this can lead to errors when refactoring.

Comment thread test/logger.js Outdated
let sentLevels = [];

let callbackResult;
const testCallback = (er, res) => { callbackResult = res;};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Correct me if I'm wrong, but it appears the only place the callback is actually called (and not just passed along) is in Logger._flush(), and that seems to call callback without any params, so how does this work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@dchai76 it attaches the callback to the instance line 340 and in the async function send_request it calls the callback that was passed specifically for that instance.

Comment thread test/logger.js Outdated
@dchai76 dchai76 requested a review from weizou19 October 23, 2019 00:29
Comment thread lib/logger.js

if (opts) {
if (typeof opts === 'string') {
if (typeof opts === 'function') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@weizou19 pointed out to me that Node itself does this (magic overloading of provided callbacks) (https://github.com/nodejs/node/blob/master/lib/fs.js#L405-L414) so as much as I dislike it I'm going to say I'm wrong on this.

Comment thread lib/logger.js Outdated
configs.LOG_LEVELS.forEach(function(level) {
var l = level.toLowerCase();
Logger.prototype[l] = function(statement, opts) {
var levelFuncationName = level.toLowerCase();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Make this a const and fix the spelling

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should set "no-var": "error" to enforce it: https://github.com/logdna/nodejs/blob/master/.eslintrc#L219

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Next PR for it

Comment thread test/logger.js Outdated
done();
}, configs.FLUSH_INTERVAL + 200);
});
it('Debug Functio, when called with callback, should call the cb', function(done) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

While it's up to you, I would add a nested describe for these (describe('Debug Function'...) so that the actual test names only say the expected behavior (it('Executes callback when provided'...)`)

Copy link
Copy Markdown
Contributor

@weizou19 weizou19 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Contributor

@smusali smusali left a comment

Choose a reason for hiding this comment

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

LGTM too since I can see more people may need to use this - @dchai76 @mikehu do you have any extra comment on this Pull Request?

Comment thread lib/logger.js Outdated
}

if (!callback || typeof callback !== 'function') {
callback = (err) => {debug(err);};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Would prefer spaces between the brackets

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Another linting rule to add
https://github.com/logdna/nodejs/blob/master/.eslintrc#L226
"space-in-brackets": "always"

Comment thread test/logger.js Outdated
let sentLevels = [];

let callbackResult;
const testCallback = (er, res) => { callbackResult = res;};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Would prefer a space before the closing bracket

@vilyapilya vilyapilya merged commit c1b4fae into master Oct 24, 2019
@smusali smusali deleted the addCallbackToLog branch October 24, 2019 22:23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants