This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Production logs in ELF format (RLJS-239)
- Use ELF Format for production express logs See: http://www.w3.org/TR/WD-logfile
- Loading branch information
Alan Cohen
committed
Feb 20, 2015
1 parent
7142262
commit 47d1866
Showing
3 changed files
with
68 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,76 @@ | ||
var winston = require('winston'); | ||
var config = require('../api/lib/config'); | ||
const winston = require('winston'); | ||
const config = require('../api/lib/config'); | ||
const morgan = require('morgan'); | ||
const utils = require('../api/lib/utils.js'); | ||
|
||
var logger = new winston.Logger({ | ||
|
||
var logger = exports.logger = new winston.Logger({ | ||
transports: [ | ||
new winston.transports.Console({ | ||
prettyPrint: true, | ||
colorize: true, | ||
handleExceptions: true, | ||
level: 'info', | ||
showLevel: true, | ||
timestamp: true, | ||
handleExceptions: true | ||
silent: config.get('NODE_ENV') === 'test' | ||
}) | ||
] | ||
], | ||
exitOnError: false | ||
}); | ||
|
||
var loggerStream = {write: function (data) { | ||
logger.info(data.replace(/\n$/, '')); | ||
}}; | ||
/** | ||
* Setup server logging middleware | ||
* | ||
* Production logs conform to Extended Log File Format: | ||
* http://www.w3.org/TR/WD-logfile | ||
*/ | ||
|
||
/** | ||
* Returns the a date-time in ELF format (e.g, 2015-02-20 00:02:08) | ||
*/ | ||
function elfDate() { | ||
return new Date().toISOString().replace('T', ' ').substr(0, 19); | ||
} | ||
|
||
var logInfo = logger.info; | ||
// ELF Headers | ||
var elfHeaders = | ||
'\n#Version: 1.0\n' + | ||
'#Date: ' + elfDate() + '\n' + | ||
'#Software: ' + utils.getPackageVersion() + '\n' + | ||
'#Fields: c-ip date time cs-method cs-uri cs(Version) sc-status time-taken cs(User-Agent)'; | ||
|
||
logger.info = function() { | ||
if (config.get('NODE_ENV') !== 'test') { | ||
logInfo.apply(logger, arguments); | ||
// Logging in ELF format (http://www.w3.org/TR/WD-logfile) | ||
var morganFormat = ':remote-addr :datetime :method :url :http-version :status ' | ||
+ ':response-time :user-agent'; | ||
|
||
// Both morgan and winston append a newline | ||
var stream = { | ||
write: function (data) { | ||
logger.info(data.slice(0, -1)); | ||
} | ||
}; | ||
|
||
var logError = logger.error; | ||
// Define the datetime token in ELF format | ||
morgan.token('datetime', elfDate); | ||
|
||
// Define the user-agent token in ELF format | ||
morgan.token('user-agent', function(req, res) { | ||
return req.get('User-Agent').split(' ').join('+'); | ||
}); | ||
|
||
exports.morgan = function(app) { | ||
if (config.get('NODE_ENV') === 'production') { | ||
app.use(morgan(morganFormat)); | ||
|
||
logger.timestamp = false; | ||
logger.prettyPrint = false; | ||
logger.colorize = false; | ||
logger.showLevel = false; | ||
|
||
logger.error = function() { | ||
if (config.get('NODE_ENV') !== 'test') { | ||
logError.apply(logger, arguments); | ||
logger.info(elfHeaders); | ||
} else { | ||
app.use(morgan('dev', {stream: stream})); | ||
} | ||
}; | ||
|
||
exports.logger = logger; | ||
exports.loggerStream = loggerStream; |