Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Enable log options for Morgan
Browse files Browse the repository at this point in the history
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
mleanos committed Oct 21, 2015
1 parent c2a86e0 commit 8cd2291
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 12 deletions.
16 changes: 13 additions & 3 deletions config/env/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ module.exports = {
debug: process.env.MONGODB_DEBUG || false
},
log: {
// logging with Morgan - https:/expressjs/morgan
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
format: 'dev',
// Stream defaults to process.stdout
// Uncomment to enable logging to a log on the file system
options: {
//stream: 'access.log'
// Stream defaults to process.stdout
// Uncomment/comment to toggle the logging to a log on the file system
stream: {
directoryPath: process.cwd(),
fileName: 'access.log',
rotatingLogs: { // for more info on rotating logs - https:/holidayextras/file-stream-rotator#usage
active: false, // activate to use rotating logs
fileName: 'access-%DATE%.log', // if rotating logs are active, this fileName setting will be used
frequency: 'daily',
verbose: false
}
}
}
},
app: {
Expand Down
18 changes: 14 additions & 4 deletions config/env/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ module.exports = {
debug: process.env.MONGODB_DEBUG || false
},
log: {
// logging with Morgan - https:/expressjs/morgan
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
format: 'combined',
// Stream defaults to process.stdout
// Uncomment to enable logging to a log on the file system
format: process.env.LOG_FORMAT || 'combined',
options: {
stream: 'access.log'
// Stream defaults to process.stdout
// Uncomment/comment to toggle the logging to a log on the file system
stream: {
directoryPath: process.env.LOG_DIR_PATH || process.cwd(),
fileName: process.env.LOG_FILE || 'access.log',
rotatingLogs: { // for more info on rotating logs - https:/holidayextras/file-stream-rotator#usage
active: process.env.LOG_ROTATING_ACTIVE === 'true' ? true : false, // activate to use rotating logs
fileName: process.env.LOG_ROTATING_FILE || 'access-%DATE%.log', // if rotating logs are active, this fileName setting will be used
frequency: process.env.LOG_ROTATING_FREQUENCY || 'daily',
verbose: process.env.LOG_ROTATING_VERBOSE === 'true' ? true : false
}
}
}
},
facebook: {
Expand Down
19 changes: 19 additions & 0 deletions config/env/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ module.exports = {
// Enable mongoose debug mode
debug: process.env.MONGODB_DEBUG || false
},
log: {
// logging with Morgan - https:/expressjs/morgan
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
format: process.env.LOG_FORMAT || 'combined',
options: {
// Stream defaults to process.stdout
// Uncomment/comment to toggle the logging to a log on the file system
stream: {
directoryPath: process.cwd(),
fileName: 'access.log',
rotatingLogs: { // for more info on rotating logs - https:/holidayextras/file-stream-rotator#usage
active: false, // activate to use rotating logs
fileName: 'access-%DATE%.log', // if rotating logs are active, this fileName setting will be used
frequency: 'daily',
verbose: false
}
}
}
},
port: process.env.PORT || 3001,
app: {
title: defaultEnvConfig.app.title + ' - Test Environment'
Expand Down
7 changes: 4 additions & 3 deletions config/lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
var config = require('../config'),
express = require('express'),
morgan = require('morgan'),
logger = require('./logger'),
bodyParser = require('body-parser'),
session = require('express-session'),
MongoStore = require('connect-mongo')(session),
Expand Down Expand Up @@ -67,11 +68,11 @@ module.exports.initMiddleware = function (app) {
// Initialize favicon middleware
app.use(favicon('./modules/core/client/img/brand/favicon.ico'));

// Enable logger (morgan)
app.use(morgan(logger.getFormat(), logger.getOptions()));

// Environment dependent middleware
if (process.env.NODE_ENV === 'development') {
// Enable logger (morgan)
app.use(morgan('dev'));

// Disable views cache
app.set('view cache', false);
} else if (process.env.NODE_ENV === 'production') {
Expand Down
109 changes: 109 additions & 0 deletions config/lib/logger.js
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;
}
Loading

0 comments on commit 8cd2291

Please sign in to comment.