Skip to content

Commit

Permalink
Issue #63: logExtended no longer ignores context.
Browse files Browse the repository at this point in the history
- coverage increase
- server-added context now under server key.
  • Loading branch information
fgm committed Jul 26, 2018
1 parent 6f5e623 commit a3efbd9
Show file tree
Hide file tree
Showing 12 changed files with 2,064 additions and 1,147 deletions.
25 changes: 17 additions & 8 deletions lib/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,36 @@ var Logger = function () {
/**
* Build a context object from log() details.
*
* @private
* @protected
*
* @see Logger.log
*
* @param {Object} details
* The message details passed to log().
* @param {string} source
* The source for the event.
* @param {Object} context
* Optional: a pre-existing context.
*
* @returns {Object}
* The context with details moved to the message_details subkey.
*/

}, {
key: "buildContext",
value: function buildContext(details) {
var _context;
value: function buildContext(details, source) {
var _extends2;

var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

var context = (_context = {}, _defineProperty(_context, Logger.KEY_DETAILS, details), _defineProperty(_context, Logger.KEY_SOURCE, this.side), _context);
var context1 = _extends({}, context, (_extends2 = {}, _defineProperty(_extends2, Logger.KEY_DETAILS, details), _defineProperty(_extends2, Logger.KEY_SOURCE, source), _extends2));

if (details[Logger.KEY_HOST]) {
context[Logger.KEY_HOST] = details[Logger.KEY_HOST];
delete context[Logger.KEY_DETAILS][Logger.KEY_HOST];
context1[Logger.KEY_HOST] = details[Logger.KEY_HOST];
delete context1[Logger.KEY_DETAILS][Logger.KEY_HOST];
}
return context;

return context1;
}

/**
Expand Down Expand Up @@ -246,7 +253,7 @@ var Logger = function () {
var process = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;

this.validateLevel(level);
var context1 = this.buildContext(details);
var context1 = this.buildContext(details, this.side);

var context2 = process ? this.applyProcessors(context1) : context1;

Expand Down Expand Up @@ -371,6 +378,8 @@ var Logger = function () {
* A possibly invalid severity level.
*
* @returns {void}
*
* @throws InvalidArgumentException
*/

}, {
Expand Down
2 changes: 1 addition & 1 deletion lib/Processors/BrowserProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var BrowserProcessor = function (_ProcessorBase) {
// Overwrite existing browser keys in context, keeping non-overwritten ones.
for (var key in browserDefaults) {
if (browserDefaults.hasOwnProperty(key)) {
result.browser[key] = this.navigator[key] ? this.navigator[key] : browserDefaults[key];
result.browser[key] = this.navigator[key] || browserDefaults[key];
}
}

Expand Down
96 changes: 78 additions & 18 deletions lib/ServerLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Object.defineProperty(exports, "__esModule", {

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
Expand Down Expand Up @@ -75,9 +77,11 @@ var ServerLogger = function (_Logger) {
* @param {WebApp} webapp
* The Meteor WebApp service.
* @param {Object} parameters
* - enableMethod: enable the filog:log method or not. Defaults to true.
* = maxReqListeners: Node JS legacy parameter. Defaults to 11.
* - logRequestHeaders: add request headers to the log context. Defaults to true.
* - servePath: the path on which to expose the logger endpoint. Defaults to "/logger".
* - enableMethod: enable the filog:log method or not. Defaults to true.
* - verbose. Defaults to false.
*/
function ServerLogger(strategy) {
var webapp = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
Expand All @@ -94,7 +98,8 @@ var ServerLogger = function (_Logger) {
logRequestHeaders: true,
// Preserve the legacy Filog default, but allow configuration.
maxReqListeners: 11,
servePath: "/logger"
servePath: "/logger",
verbose: false
};

// Loop on defaults, not arguments, to avoid injecting any random junk.
Expand All @@ -115,20 +120,61 @@ var ServerLogger = function (_Logger) {
}

/**
* Handle a log message from the client.
* Build a context object from log() details.
*
* @param {IncomingMessage} req
* The request.
* @param {ServerResponse} res
* The response.
* @param {function} next
* A callback, not used currently.
* @protected
*
* @returns {void}
* @see Logger.log
*
* @param {Object} details
* The message details passed to log().
* @param {string} source
* The source for the event.
* @param {Object} context
* Optional: a pre-existing context.
*
* @returns {Object}
* The context with details moved to the message_details subkey.
*/


_createClass(ServerLogger, [{
key: "buildContext",
value: function buildContext(details, source) {
var _extends2;

var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

// Ignore source and host keys from caller context.
var sourceDetails = context[_Logger3.default.KEY_DETAILS],
ignoredSource = context[_Logger3.default.KEY_SOURCE],
ignoredHostName = context[_Logger3.default.KEY_HOST],
context1 = _objectWithoutProperties(context, [_Logger3.default.KEY_DETAILS, _Logger3.default.KEY_SOURCE, _Logger3.default.KEY_HOST]);

// In case of conflict, argument details overwrites caller details.


var mergedDetails = _extends({}, sourceDetails, details);

var context2 = _extends({}, _get(ServerLogger.prototype.__proto__ || Object.getPrototypeOf(ServerLogger.prototype), "buildContext", this).call(this, mergedDetails, source), (_extends2 = {}, _defineProperty(_extends2, source, context1), _defineProperty(_extends2, _Logger3.default.KEY_HOST, this.hostname), _extends2));

return context2;
}

/**
* Handle a log message from the client.
*
* @param {IncomingMessage} req
* The request.
* @param {ServerResponse} res
* The response.
* @param {function} next
* A callback, not used currently.
*
* @returns {void}
*/

}, {
key: "handleClientLogRequest",
value: function handleClientLogRequest(req, res) {
var _this2 = this;
Expand Down Expand Up @@ -214,20 +260,30 @@ var ServerLogger = function (_Logger) {
*
* @returns {void}
*
* @FIXME context is ignored.
* @throws InvalidArgumentException
*/

}, {
key: "logExtended",
value: function logExtended(level, message, details, context, source) {
var _context;

this.validateLevel(level);
var context1 = this.buildContext(details, source, context);
console.log(context1);
var context2 = this.applyProcessors(context1);

var context1 = _process2.default ? this.applyProcessors(details) : details;
var processedDetails = context2[_Logger3.default.KEY_DETAILS],
processedSource = context2[_Logger3.default.KEY_SOURCE],
processedSourceContext = context2[source],
processedHost = context2[_Logger3.default.KEY_HOST],
processedTs = context2[_Logger3.default.KEY_TS],
serverContext = _objectWithoutProperties(context2, [_Logger3.default.KEY_DETAILS, _Logger3.default.KEY_SOURCE, source, _Logger3.default.KEY_HOST, _Logger3.default.KEY_TS]);

context1.source = source;
this.stamp(context1, "log");
var context3 = (_context = {}, _defineProperty(_context, _Logger3.default.KEY_DETAILS, processedDetails), _defineProperty(_context, _Logger3.default.KEY_SOURCE, processedSource), _defineProperty(_context, source, processedSourceContext), _defineProperty(_context, _Logger3.default.KEY_HOST, processedHost), _defineProperty(_context, _Logger3.default.KEY_TS, processedTs), _defineProperty(_context, ServerLogger.side, serverContext), _context);
this.stamp(context3, "log");

this.send(this.strategy, level, message, context1);
this.send(this.strategy, level, message, context3);
}

/**
Expand Down Expand Up @@ -285,11 +341,15 @@ var ServerLogger = function (_Logger) {
value: function setupConnect(webapp, servePath) {
this.webapp = webapp;
if (this.webapp) {
this.output.write("Serving logger on " + servePath + ".\n");
if (this.verbose) {
this.output.write("Serving logger on " + servePath + ".\n");
}
var app = this.webapp.connectHandlers;
app.use(this.servePath, this.handleClientLogRequest.bind(this));
} else {
this.output.write("Not serving logger, path " + servePath + ".\n");
if (this.verbose) {
this.output.write("Not serving logger, path " + servePath + ".\n");
}
}
}
}], [{
Expand All @@ -304,7 +364,7 @@ var ServerLogger = function (_Logger) {
if (rawMessage) {
if (typeof rawMessage === "string") {
return rawMessage;
} else if (typeof rawMessage.toString === "function") {
} else if (rawMessage.toString.constructor.name === "Function") {
return rawMessage.toString();
}
}
Expand Down
Loading

0 comments on commit a3efbd9

Please sign in to comment.