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

Commit a6b3f14

Browse files
committed
Merge pull request #999 from mleanos/morgan-logger-config
[bug] Enable log options for Morgan that were missed in the 0.4.0 merge
2 parents c2a86e0 + 8cd2291 commit a6b3f14

File tree

7 files changed

+347
-12
lines changed

7 files changed

+347
-12
lines changed

config/env/development.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,22 @@ module.exports = {
1313
debug: process.env.MONGODB_DEBUG || false
1414
},
1515
log: {
16+
// logging with Morgan - https://github.com/expressjs/morgan
1617
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
1718
format: 'dev',
18-
// Stream defaults to process.stdout
19-
// Uncomment to enable logging to a log on the file system
2019
options: {
21-
//stream: 'access.log'
20+
// Stream defaults to process.stdout
21+
// Uncomment/comment to toggle the logging to a log on the file system
22+
stream: {
23+
directoryPath: process.cwd(),
24+
fileName: 'access.log',
25+
rotatingLogs: { // for more info on rotating logs - https://github.com/holidayextras/file-stream-rotator#usage
26+
active: false, // activate to use rotating logs
27+
fileName: 'access-%DATE%.log', // if rotating logs are active, this fileName setting will be used
28+
frequency: 'daily',
29+
verbose: false
30+
}
31+
}
2232
}
2333
},
2434
app: {

config/env/production.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,22 @@ module.exports = {
1717
debug: process.env.MONGODB_DEBUG || false
1818
},
1919
log: {
20+
// logging with Morgan - https://github.com/expressjs/morgan
2021
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
21-
format: 'combined',
22-
// Stream defaults to process.stdout
23-
// Uncomment to enable logging to a log on the file system
22+
format: process.env.LOG_FORMAT || 'combined',
2423
options: {
25-
stream: 'access.log'
24+
// Stream defaults to process.stdout
25+
// Uncomment/comment to toggle the logging to a log on the file system
26+
stream: {
27+
directoryPath: process.env.LOG_DIR_PATH || process.cwd(),
28+
fileName: process.env.LOG_FILE || 'access.log',
29+
rotatingLogs: { // for more info on rotating logs - https://github.com/holidayextras/file-stream-rotator#usage
30+
active: process.env.LOG_ROTATING_ACTIVE === 'true' ? true : false, // activate to use rotating logs
31+
fileName: process.env.LOG_ROTATING_FILE || 'access-%DATE%.log', // if rotating logs are active, this fileName setting will be used
32+
frequency: process.env.LOG_ROTATING_FREQUENCY || 'daily',
33+
verbose: process.env.LOG_ROTATING_VERBOSE === 'true' ? true : false
34+
}
35+
}
2636
}
2737
},
2838
facebook: {

config/env/test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ module.exports = {
1212
// Enable mongoose debug mode
1313
debug: process.env.MONGODB_DEBUG || false
1414
},
15+
log: {
16+
// logging with Morgan - https://github.com/expressjs/morgan
17+
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
18+
format: process.env.LOG_FORMAT || 'combined',
19+
options: {
20+
// Stream defaults to process.stdout
21+
// Uncomment/comment to toggle the logging to a log on the file system
22+
stream: {
23+
directoryPath: process.cwd(),
24+
fileName: 'access.log',
25+
rotatingLogs: { // for more info on rotating logs - https://github.com/holidayextras/file-stream-rotator#usage
26+
active: false, // activate to use rotating logs
27+
fileName: 'access-%DATE%.log', // if rotating logs are active, this fileName setting will be used
28+
frequency: 'daily',
29+
verbose: false
30+
}
31+
}
32+
}
33+
},
1534
port: process.env.PORT || 3001,
1635
app: {
1736
title: defaultEnvConfig.app.title + ' - Test Environment'

config/lib/express.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
var config = require('../config'),
77
express = require('express'),
88
morgan = require('morgan'),
9+
logger = require('./logger'),
910
bodyParser = require('body-parser'),
1011
session = require('express-session'),
1112
MongoStore = require('connect-mongo')(session),
@@ -67,11 +68,11 @@ module.exports.initMiddleware = function (app) {
6768
// Initialize favicon middleware
6869
app.use(favicon('./modules/core/client/img/brand/favicon.ico'));
6970

71+
// Enable logger (morgan)
72+
app.use(morgan(logger.getFormat(), logger.getOptions()));
73+
7074
// Environment dependent middleware
7175
if (process.env.NODE_ENV === 'development') {
72-
// Enable logger (morgan)
73-
app.use(morgan('dev'));
74-
7576
// Disable views cache
7677
app.set('view cache', false);
7778
} else if (process.env.NODE_ENV === 'production') {

config/lib/logger.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
'use strict';
2+
3+
var _ = require('lodash'),
4+
config = require('../config'),
5+
chalk = require('chalk'),
6+
fileStreamRotator = require('file-stream-rotator'),
7+
fs = require('fs');
8+
9+
// list of valid formats for the logging
10+
var validFormats = ['combined', 'common', 'dev', 'short', 'tiny'];
11+
12+
// build logger service
13+
var logger = {
14+
getFormat: getLogFormat, // log format to use
15+
getOptions: getLogOptions // log options to use
16+
};
17+
18+
// export the logger service
19+
module.exports = logger;
20+
21+
/**
22+
* The format to use with the logger
23+
*
24+
* Returns the log.format option set in the current environment configuration
25+
*/
26+
function getLogFormat () {
27+
var format = config.log && config.log.format ? config.log.format.toString() : 'combined';
28+
29+
// make sure we have a valid format
30+
if (!_.includes(validFormats, format)) {
31+
format = 'combined';
32+
33+
if (process.env.NODE_ENV !== 'test') {
34+
console.log();
35+
console.log(chalk.yellow('Warning: An invalid format was provided. The logger will use the default format of "' + format + '"'));
36+
console.log();
37+
}
38+
}
39+
40+
return format;
41+
}
42+
43+
/**
44+
* The options to use with the logger
45+
*
46+
* Returns the log.options object set in the current environment configuration.
47+
* NOTE: Any options, requiring special handling (e.g. 'stream'), that encounter an error will be removed from the options.
48+
*/
49+
function getLogOptions () {
50+
var options = config.log && config.log.options ? _.clone(config.log.options, true) : {};
51+
52+
// check if the current environment config has the log stream option set
53+
if (_.has(options, 'stream')) {
54+
55+
try {
56+
57+
// check if we need to use rotating logs
58+
if (_.has(options, 'stream.rotatingLogs') && options.stream.rotatingLogs.active) {
59+
60+
if (options.stream.rotatingLogs.fileName.length && options.stream.directoryPath.length) {
61+
62+
// ensure the log directory exists
63+
if (!fs.existsSync(options.stream.directoryPath)) {
64+
fs.mkdirSync(options.stream.directoryPath);
65+
}
66+
67+
options.stream = fileStreamRotator.getStream({
68+
filename: options.stream.directoryPath + '/' + options.stream.rotatingLogs.fileName,
69+
frequency: options.stream.rotatingLogs.frequency,
70+
verbose: options.stream.rotatingLogs.verbose
71+
});
72+
73+
} else {
74+
// throw a new error so we can catch and handle it gracefully
75+
throw new Error('An invalid fileName or directoryPath was provided for the rotating logs option.');
76+
}
77+
78+
} else {
79+
80+
// create the WriteStream to use for the logs
81+
if (options.stream.fileName.length && options.stream.directoryPath.length) {
82+
83+
// ensure the log directory exists
84+
if (!fs.existsSync(options.stream.directoryPath)) {
85+
fs.mkdirSync(options.stream.directoryPath);
86+
}
87+
88+
options.stream = fs.createWriteStream(options.stream.directoryPath + '/' + config.log.options.stream.fileName, { flags: 'a' });
89+
} else {
90+
// throw a new error so we can catch and handle it gracefully
91+
throw new Error('An invalid fileName or directoryPath was provided for stream option.');
92+
}
93+
}
94+
} catch (err) {
95+
96+
// remove the stream option
97+
delete options.stream;
98+
99+
if (process.env.NODE_ENV !== 'test') {
100+
console.log();
101+
console.log(chalk.red('An error has occured during the creation of the WriteStream. The stream option has been omitted.'));
102+
console.log(chalk.red(err));
103+
console.log();
104+
}
105+
}
106+
}
107+
108+
return options;
109+
}

0 commit comments

Comments
 (0)