Skip to content

Conversation

0x-r4bbit
Copy link
Contributor

No description provided.

@ghost
Copy link

ghost commented Jan 15, 2020

DeepCode's analysis on #4328fd found:

  • 0 critical issues. ⚠️ 0 warnings and 2 minor issues. ✔️ 2 issues were fixed.

💬 This comment has been generated by the DeepCode bot, installed by the owner of the repository. The DeepCode bot protects your repository by detecting and commenting on security vulnerabilities or other critical issues.


☺️ If you want to provide feedback on our bot, here is how to contact us.

@0x-r4bbit
Copy link
Contributor Author

Added another commit that refactors the module (hopefully for the better)

Copy link
Contributor

@michaelsbradleyjr michaelsbradleyjr left a comment

Choose a reason for hiding this comment

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

Nice work! See review comments below re: some things I think need attention.

Copy link
Collaborator

@emizzle emizzle left a comment

Choose a reason for hiding this comment

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

Great job!!

Just a couple of small suggestions on the tests.


test('it should should use custom log function for logging', () => {
logger.info('Hello world');
assert(testLogFn.calledOnce);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we could also inspect the args coming through to the fake logFunction as well?

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 is already covered in the follow test

Copy link
Collaborator

Choose a reason for hiding this comment

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

I could definitely be wrong, but aren't there more args than just the single string parameter that is tested for in the next test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The following test checks if the logFunction, which is testLogFn gets called with the expected parameters. What we can't see is that logger actually performs a mutation on the input parameter of methods like info() and error().

It puts color codes around the given message, that's why the test checks against {color_code}input{color_code}.

Does that make sense? Or am I misunderstanding what you're asking for


test('it should write logs to log file', () => {
logger.info('Some test log');
const logs = fs.readFileSync(logFile.name, 'utf8')
Copy link
Collaborator

Choose a reason for hiding this comment

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

I really think we should not be reading the file system here and instead faking appendFileSync (or whatever async equivalent we decide to go with). This will require fs to be configurable on Logger (obviously why we should be using DI, but I digress), so maybe the easiest option is to tack on an fs property in the constructor:

class Logger {
  constructor(options) {
    // ...
    this.fs = options.fs || fs;
    // ...
  }
}

And then in the test setup:

const appendFileSync = sinon.fake(); // could be async version depending on if you implement michael's suggestion
const fs = { appendFileSync };
logger = new Logger({
  events: embark.events,
  logFunction: testLogFn,
  logFile: "test"
  fs
});

and then in the test:

logger.info('Some test log');
sinon.assert.calledWith(appendFileSync, "test", ... rest of params);

@ghost
Copy link

ghost commented Jan 22, 2020

DeepCode's analysis on #2a5abe found:

  • 0 critical issues. ⚠️ 0 warnings and 2 minor issues. ✔️ 0 issues were fixed.

💬 This comment has been generated by the DeepCode bot, installed by the owner of the repository. The DeepCode bot protects your repository by detecting and commenting on security vulnerabilities or other critical issues.


☺️ If you want to provide feedback on our bot, here is how to contact us.

Copy link
Contributor

@michaelsbradleyjr michaelsbradleyjr left a comment

Choose a reason for hiding this comment

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

Good work so far. I have some questions/comments below.

Also, this is the first time in awhile I've really looked hard at the logger and one thing that jumped out at me is that by pinning _logFunction to console.log it means that our error, warn, etc. output are all going to stdout rather than stderr.

In a terminal:

$ node -e 'console.log("hello")' 2>/dev/null
hello
$ node -e 'console.error("hello")' 2>/dev/null
$ node -e 'console.warn("hello")' 2>/dev/null
$ node -e 'console.debug("hello")' 2>/dev/null
hello
$ node -e 'console.trace("hello")' 2>/dev/null

Since it's standard behavior that some loglevels go to stderr instead of stdout it seems like we should probably have our logger work the same way when it comes to printing to the screen (it's fine that all of the levels write into one log file). Instead of only having _logFunction we could also have _warnFunction, _debugFunction, etc. all by default being set to their console.[level] counterparts. The functions we define in logger/src/index.js for info, warn, etc. could pass the desired _[level]Function as an argument to logFunction.

Maybe the behavior change re: stdout/stderr belongs in another PR instead of this one.

@0x-r4bbit
Copy link
Contributor Author

I've updated the log APIs to take an optional callback parameter so we can be notified inside tests when a log method is done (instead of relying on e.g. fs.statSync with a timeout loop to determine when a logger has written to a log file yet). /cc @emizzle

Copy link
Contributor

@michaelsbradleyjr michaelsbradleyjr left a comment

Choose a reason for hiding this comment

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

I wanted to use the improved logger (of this PR) and ran into some problems. I've got a branch in progress that I'll submit as a PR to this PR branch, but it's still WIP at the moment, will wrap it up tomorrow; definitely simpler than trying to explain what's wrong and how to fix it.

Short version: the origin calculation doesn't work if it's inside the async.cargo; also, each distinct message needs to be on its own line with timestamp, etc. otherwise it's very confusing to read.

See: test/core/logger...test/core/logger-mb

See: 2a5abe7

@michaelsbradleyjr
Copy link
Contributor

michaelsbradleyjr commented Jan 31, 2020

Rebased and added a commit with several changes, please re-review. See: 2a5abe7.

@michaelsbradleyjr michaelsbradleyjr force-pushed the test/core/logger branch 2 times, most recently from 528ced0 to 554d865 Compare January 31, 2020 01:19
@michaelsbradleyjr
Copy link
Contributor

michaelsbradleyjr commented Feb 3, 2020

@emizzle I believe your change request is satisfied by my commit on this branch. See 2a5abe7. If so, can you clear your change request? I think this PR is otherwise good to go.

0x-r4bbit and others added 4 commits February 3, 2020 14:50
This also removes `parseLogFile()` as it's not used anywhere inside Embark.
Also make a few more revisions:

Revise the "write logs" testing strategy such that it's not necessary for the
logger functions to take an optional callback.

Drop unused `tmp` package from `packages/core/logger` since it's not used in
the tests.

Strip colors before writing to the log file, use a two-space delimiter between
sections of each logged line in the log file, and collapse whitespace in the
message section of each line. These changes make the log file more amenable to
being processed with cli tools such as awk, cut, etc. It's also possible in a
text editor to replace `'  '` with `\t` and then load the file in a spreadsheet,
with each line-section in its own column.

Rearrange the sections of each logged line so that it's easier to read, and
only include the origin if the loglevel is debug or trace.
@michaelsbradleyjr michaelsbradleyjr merged commit 5b988ea into master Feb 4, 2020
@michaelsbradleyjr michaelsbradleyjr deleted the test/core/logger branch February 4, 2020 02:47
michaelsbradleyjr added a commit that referenced this pull request Feb 4, 2020
…ogger's `logFunction`

`packages/embark/src/cmd/dashboard/dashboard.js` overwrites the logger
instance's `logFunction` method with a method named `logEntry` defined on the
class exported from `packages/embark/src/cmd/dashboard/monitor.js`. Update
`logEntry` to process arguments in the same way as `logFunction` was revised
in #2184.
michaelsbradleyjr added a commit that referenced this pull request Feb 4, 2020
…ogger's `logFunction`

`packages/embark/src/cmd/dashboard/dashboard.js` overwrites the logger
instance's `logFunction` method with a method named `logEntry` defined on the
class exported from `packages/embark/src/cmd/dashboard/monitor.js`. Update
`logEntry` in the same way as `logFunction` was revised in #2184.
michaelsbradleyjr added a commit that referenced this pull request Feb 4, 2020
…ogger's `logFunction`

`packages/embark/src/cmd/dashboard/dashboard.js` overwrites the logger
instance's `logFunction` method with a method named `logEntry` defined on the
class exported from `packages/embark/src/cmd/dashboard/monitor.js`. Update
`logEntry` in the same way as `logFunction` was revised in #2184.
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.

4 participants