-
Notifications
You must be signed in to change notification settings - Fork 490
test(@embark/core): add tests for logger module #2184
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
Conversation
DeepCode's analysis on #4328fd found:
💬 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. |
Added another commit that refactors the module (hopefully for the better) |
There was a problem hiding this 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.
There was a problem hiding this 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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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);
4328fd1
to
816b1cf
Compare
DeepCode's analysis on #2a5abe found:
💬 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. |
816b1cf
to
58d6881
Compare
There was a problem hiding this 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.
58d6881
to
2118a62
Compare
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. |
There was a problem hiding this 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
2118a62
to
cf7dbd8
Compare
Rebased and added a commit with several changes, please re-review. See: 2a5abe7. |
528ced0
to
554d865
Compare
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.
554d865
to
2a5abe7
Compare
…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.
…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.
…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.
No description provided.