-
Notifications
You must be signed in to change notification settings - Fork 2k
[bug] Enable log options for Morgan that were missed in the 0.4.0 merge #999
Conversation
I'm aware of the coverage decrease. However, I just wanted to get this out there for review & work on writing tests in the meantime. I shall do that now :) |
I wrote two tests initially for this feature. However, I'm unsure if I went about this correctly. They just seem too simple. Although, the testability of a feature like this does seem limited.
This brings up an interesting idea I was pondering a while ago.. Would be worthwhile to add a feature to reinitialize the global configuration? In instances where the NODE_ENV is manually changed (most likely for test coverage only), it would be nice to test features against the updated configuration settings. Any thought on this? |
I reworked the tests, and cleaned up the default settings for the log options & format. I think this last commit is better, but still would love some feedback. |
bae1ad4
to
1f4cc3b
Compare
@mleanos I did a similar "trick" with changing the NODE_ENV from testing to production through out the tests to make sure that the config works properly. Otherwise LGTM. |
@lirantal I liked your strategy of changing the NODE_ENV manually. However, that won't work in this case. Once the logger is required, the config is initialized. Changing the node env will have no impact aftward. What if I added a |
Don't think so because that's usually a bad practice to |
SGTM. Thank you. Any opinion on if we should add the log settings to the Test env config? This way we can test the Stream option as well. |
options.stream = fs.createWriteStream(process.cwd() + '/' + config.log.options.stream, { flags: 'a' }); | ||
} catch (err) { | ||
// remove the stream option | ||
delete options.stream; |
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.
Do we want to log an error to the console instead of just silently catching and handeling the error?
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.
That's a good point. I think logging to the console would be appropriate here.
1f4cc3b
to
b3d5af4
Compare
I just committed quite a bit of changes. I added a bit more error handling, and modified some logic of how I was returning the log settings. For instance, before I was returning references to the env config settings. I think that was wrong. Now I'm making copies. I also added a couple more tests, and added the log settings to the Test env config. Further review would be greatly appreciated. |
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny' | ||
format: process.env.LOG_FORMAT || 'combined', | ||
// Stream defaults to process.stdout | ||
// Uncomment to enable logging to a log on the file system |
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.
@mleanos what is this comment for? doesn't the option below log to the file system?
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.
It was just a left over comment from when I copied the settings from the development config.
I can remove it, but it also does make sense to leave it?
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.
It seems confusing. Let's move the comments in line 18 to https://github.com/mleanos/mean/blob/morgan-logger-config/config/env/test.js#L21 inside the options object and above the stream option.
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.
@jloveland @lirantal I moved the comment, and re-worded it. Let me know if you'd like additional changes to it. Thanks.
b3d5af4
to
5b83f3f
Compare
@codydaig I have rebased, and squashed. If there's no other feedback, or requested changes, then I think this is ready. |
|
||
// create the WriteStream to use for the logs | ||
if (options.stream.length) { | ||
options.stream = fs.createWriteStream(process.cwd() + '/' + config.log.options.stream, { flags: 'a' }); |
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.
@mleanos, great job on this!
I think it would be more robust to provide an ability to specify the full path to the directory which we would like logs to be stored. For example, I like my logs to go to /var/log/meanjs/access.log
. Your code assumes the root directory is the cwd.
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.
@jloveland That's a good idea. I'll work on adding this.
I think moving the definition of the log location to be in the env config would be the way to go. This way the the logger
config won't need to calculate the location of the logs.
I think it would be nice add log rotation configs to the // create a rotating write stream
var accessLogStream = FileStreamRotator.getStream({
filename: logDirectory + '/access-%DATE%.log',
frequency: 'daily',
verbose: false
})
// setup the logger
app.use(morgan('combined', {stream: accessLogStream})) |
@jloveland sounds like a good diea |
@jloveland @lirantal The log rotating is a good idea. I'll have to reconfigure the stream options, but I think it can be done to easily allow switching between both log rotating, and single file log (or even possibly allow for both options at the same time). |
@lirantal @jloveland I started looking at how I could change the Stream options in the env configs. Here's my initial idea... Any thoughts on this approach? stream: {
single: {
active: true,
directoryPath: process.cwd(),
fileName: 'access.log'
},
rotating: {
active: true,
directoryPath: 'c:/windows/logs/meanjs',
fileName: 'access-%DATE%.log',
frequency: 'daily',
verbose: false
}
} We could use these settings to build the options in the |
I guess the above won't work, since I can't pass both streams into Morgan at the same time :) I'll keep at it. |
5b83f3f
to
cf81e22
Compare
@jloveland @lirantal I have implemented the Rotating Logs functionality. This still needs a little bit of work, and I need to write tests for the Rotating logs. But I wanted to get this out for feedback. I had to re-work the Stream options to account for the Rotating Logs. How is this new implementation looking? I've also refactored the tests to be more maintainable. There's one test in particular that I'm having an issue with. I have disabled this test for now, but would love to know if anyone has any insight into this. I'm getting a ENOTEMPTY error from the |
@mleanos I think you have to empty the directory before you can remove the directory. |
@codydaig I'm using |
@mleanos I would assume so as well. Sorry, I didn't get a chance to look at the code before I made that comment, just going off the error message. |
Quite a lot of logic to add for the log rotation. Is there no package for that on npm that we can rely on and complicate the logic less? |
@lirantal What logic are you referring to? Don't we want to offer the option of using a single file log, or a rotating log? |
Yes that's the point. I was wondering if there's some package to handle that so we don't need to start messing around with deleting files, creating directories, etc. |
@@ -34,6 +34,7 @@ | |||
"crypto": "0.0.3", | |||
"express": "^4.13.1", | |||
"express-session": "^1.11.3", | |||
"file-stream-rotator": "0.0.6", |
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.
~
@lirantal I agree. I'm looking into possible packages to use. One problem is the unit testing though. If we want to write Unit Tests against the logging, then we will need to remove the test logs after the tests are complete. I'm not seeing a solution to that particular issue, unless a package offers this out of the box. |
I tested these features and they LGTM! |
@lirantal I ended up implementing Thank you for testing with me @jloveland It's much appreciated :) We still have to make sure the provided log directory is created, if it doesn't exist. There's no way around it, but I don't think it will be an issue. @codydaig @ilanbiala @lirantal @jloveland Let me know what you think of these changes, and if we're satisfied I can squash. |
LGTM |
@@ -112,6 +113,7 @@ | |||
"karma-ng-html2js-preprocessor": "^0.1.2", | |||
"karma-phantomjs-launcher": "~0.2.0", | |||
"load-grunt-tasks": "^3.2.0", | |||
"mock-fs": "^3.4.0", |
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.
~, we are moving away from ^.
@mleanos pretty much LGTM, just looking at tests and I'm wondering if we can add a bit more coverage for those edge cases? Ideally we hit every line, though I'm not sure how to test https://coveralls.io/builds/3890174/source?filename=..%2Fconfig%2Flib%2Flogger.js |
@ilanbiala @mleanos Something along these lines should work. I hope I remember the exact syntax correctly. var spy = sinon.spy(console, 'log');
spy.firstCall.calledWith();
spy.secondCall.calledWith(chalk.yellow('Warning: An invalid format was provided. The logger will use the default format of "' + format + '"'));
spy.ThirdCall.calledWith();
console.log.restore(); |
@codydaig Thank you for the suggestion. I'll try to implement this. We have a few other areas of the app where this would be useful. |
Adds the log options, and format to the Morgan middleware in the Express configuration. These options are defined in the environment configurations. The implementation derived from meanjs#254 by @lirantal, which somehow got overlooked when merging 0.4.0 into master. Added tests for the Logger configuration. Added the log settings to the Test env config. Added environment variables for the log settings in the Test & Production env configs. Moved the Morgan Express middleware outside of the NODE_ENV === 'development' check. Morgan should be used in all environments, and use the settings set in each env config. Changed the wording of the Stream option comments in the env configs. Added Rotating Logs functionality, and refactored the log Stream options. Added a new npm package, FileStreamRotator, for use with Morgan's rotating logs functionality. Also, refactored the log configuration tests to be more maintainable. Added more tests, and refactored test suite to use mock-fs.
cd95f35
to
8cd2291
Compare
@codydaig @ilanbiala I looked into using Sinon, and it was quite handy. It turns out that using Sinon's sandbox feature, it's possible to mute all console logs for the duration of a test/test-suite. I think this will really be useful in a few different areas of our application. I'd like to look at incorporating the muting of the console logs into our test coverage, but more on a global test coverage scope. Let's not worry about adding the extra coverage for the lines that log to console in this PR. I think it would be better to address it separately. Part of my reasoning to wait on using Sinon, is because currently we're checking if @ilanbiala I addressed the I have rebased & squashed since this should be ready now. |
Did another once over, LGTM. |
LGTM |
[bug] Enable log options for Morgan that were missed in the 0.4.0 merge
Adds the log options, and format to the Morgan middleware in the Express configuration.
These options are defined in the environment configurations.
The implementation derived from #254 by @lirantal, which somehow got overlooked when merging 0.4.0 into master.
Related to the discussion in #975, and taking over for @codydaig #982