This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. 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.
- Loading branch information
Showing
7 changed files
with
347 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'), | ||
config = require('../config'), | ||
chalk = require('chalk'), | ||
fileStreamRotator = require('file-stream-rotator'), | ||
fs = require('fs'); | ||
|
||
// list of valid formats for the logging | ||
var validFormats = ['combined', 'common', 'dev', 'short', 'tiny']; | ||
|
||
// build logger service | ||
var logger = { | ||
getFormat: getLogFormat, // log format to use | ||
getOptions: getLogOptions // log options to use | ||
}; | ||
|
||
// export the logger service | ||
module.exports = logger; | ||
|
||
/** | ||
* The format to use with the logger | ||
* | ||
* Returns the log.format option set in the current environment configuration | ||
*/ | ||
function getLogFormat () { | ||
var format = config.log && config.log.format ? config.log.format.toString() : 'combined'; | ||
|
||
// make sure we have a valid format | ||
if (!_.includes(validFormats, format)) { | ||
format = 'combined'; | ||
|
||
if (process.env.NODE_ENV !== 'test') { | ||
console.log(); | ||
console.log(chalk.yellow('Warning: An invalid format was provided. The logger will use the default format of "' + format + '"')); | ||
console.log(); | ||
} | ||
} | ||
|
||
return format; | ||
} | ||
|
||
/** | ||
* The options to use with the logger | ||
* | ||
* Returns the log.options object set in the current environment configuration. | ||
* NOTE: Any options, requiring special handling (e.g. 'stream'), that encounter an error will be removed from the options. | ||
*/ | ||
function getLogOptions () { | ||
var options = config.log && config.log.options ? _.clone(config.log.options, true) : {}; | ||
|
||
// check if the current environment config has the log stream option set | ||
if (_.has(options, 'stream')) { | ||
|
||
try { | ||
|
||
// check if we need to use rotating logs | ||
if (_.has(options, 'stream.rotatingLogs') && options.stream.rotatingLogs.active) { | ||
|
||
if (options.stream.rotatingLogs.fileName.length && options.stream.directoryPath.length) { | ||
|
||
// ensure the log directory exists | ||
if (!fs.existsSync(options.stream.directoryPath)) { | ||
fs.mkdirSync(options.stream.directoryPath); | ||
} | ||
|
||
options.stream = fileStreamRotator.getStream({ | ||
filename: options.stream.directoryPath + '/' + options.stream.rotatingLogs.fileName, | ||
frequency: options.stream.rotatingLogs.frequency, | ||
verbose: options.stream.rotatingLogs.verbose | ||
}); | ||
|
||
} else { | ||
// throw a new error so we can catch and handle it gracefully | ||
throw new Error('An invalid fileName or directoryPath was provided for the rotating logs option.'); | ||
} | ||
|
||
} else { | ||
|
||
// create the WriteStream to use for the logs | ||
if (options.stream.fileName.length && options.stream.directoryPath.length) { | ||
|
||
// ensure the log directory exists | ||
if (!fs.existsSync(options.stream.directoryPath)) { | ||
fs.mkdirSync(options.stream.directoryPath); | ||
} | ||
|
||
options.stream = fs.createWriteStream(options.stream.directoryPath + '/' + config.log.options.stream.fileName, { flags: 'a' }); | ||
} else { | ||
// throw a new error so we can catch and handle it gracefully | ||
throw new Error('An invalid fileName or directoryPath was provided for stream option.'); | ||
} | ||
} | ||
} catch (err) { | ||
|
||
// remove the stream option | ||
delete options.stream; | ||
|
||
if (process.env.NODE_ENV !== 'test') { | ||
console.log(); | ||
console.log(chalk.red('An error has occured during the creation of the WriteStream. The stream option has been omitted.')); | ||
console.log(chalk.red(err)); | ||
console.log(); | ||
} | ||
} | ||
} | ||
|
||
return options; | ||
} |
Oops, something went wrong.