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

Commit

Permalink
Merge pull request #999 from mleanos/morgan-logger-config
Browse files Browse the repository at this point in the history
[bug] Enable log options for Morgan that were missed in the 0.4.0 merge
  • Loading branch information
codydaig committed Oct 23, 2015
2 parents c2a86e0 + 8cd2291 commit a6b3f14
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://github.com/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://github.com/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://github.com/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://github.com/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://github.com/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://github.com/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 a6b3f14

Please sign in to comment.