Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
[FEATURE] Production logs in ELF format (RLJS-239)
Browse files Browse the repository at this point in the history
- 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
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 47 deletions.
29 changes: 6 additions & 23 deletions api/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,13 @@ var logger = new winston.Logger({
prettyPrint: true,
colorize: true,
timestamp: true,
handleExceptions: true
handleExceptions: true,
logLevel: 'info',
showLevel: true,
silent: config.get('NODE_ENV') === 'test'
})
]
],
exitOnError: false
});

var loggerStream = {write: function (data) {
logger.info(data.replace(/\n$/, ''));
}};

var logInfo = logger.info;

logger.info = function() {
if (config.get('NODE_ENV') !== 'test') {
logInfo.apply(logger, arguments);
}
};

var logError = logger.error;

logger.error = function() {
if (config.get('NODE_ENV') !== 'test') {
logError.apply(logger, arguments);
}
};

exports.logger = logger;
exports.loggerStream = loggerStream;
10 changes: 4 additions & 6 deletions server/express_app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const ripple = require('ripple-lib');
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const logger = require('./logger.js');
const logger = require('./logger.js').logger;
const morgan = require('./logger.js').morgan;
const config = require('../api/lib/config');
const router = require('./router.js');
const version = require('./version.js');
Expand All @@ -24,13 +24,11 @@ app.use(function(req, res, next) {
next();
});

if (config.get('NODE_ENV') !== 'test') {
app.use(morgan('dev', { stream: logger.loggerStream }));
}
morgan(app);

if (config.get('debug')) {
app.use(function (req, res, next) {
logger.logger.info(req.method, req.url, req.body);
logger.info(req.method, req.url, req.body);
next();
})
}
Expand Down
76 changes: 58 additions & 18 deletions server/logger.js
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;

0 comments on commit 47d1866

Please sign in to comment.