Skip to content
This repository was archived by the owner on Feb 19, 2024. It is now read-only.

Commit

Permalink
fix(log): fix vip logging to prevent bad formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ostowe committed Mar 2, 2022
1 parent 040124b commit 6f90289
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
13 changes: 9 additions & 4 deletions packages/vip-go/services/generateLogInfo.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const util = require('util');

/**
* Utility function to format log info for the Debug package.
*
Expand All @@ -9,7 +11,6 @@ module.exports = function generateLogInfo(method, messages) {
const hasKey = (key) => messages.find(
(message) => typeof message === 'object' && message[key],
);

// Allow-listed valid Sentry keys.
const sentryKeys = [
'tags',
Expand All @@ -30,11 +31,12 @@ module.exports = function generateLogInfo(method, messages) {
}, {});
}

const message = messages;
let error = false;

if (method === 'error') {
error = !(messages[0] instanceof Error) ? new Error(messages[0]) : messages[0];
error = !(messages[0] instanceof Error)
? new Error(util.format(...messages))
: messages[0];
}

// Construct info object.
Expand All @@ -46,7 +48,10 @@ module.exports = function generateLogInfo(method, messages) {
stack: error.stack,
};
} else {
info = { ...info, message };
info = {
...info,
message: util.format(...messages),
};
}

return {
Expand Down
14 changes: 12 additions & 2 deletions packages/vip-go/services/logService.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let Sentry;
const getService = (namespace) => {
let service = defaultService;
let sentryConfig = {};
let vipLogger = {};
// Init logger with proper namespace.
const logger = debug(namespace);

Expand All @@ -43,6 +44,7 @@ const getService = (namespace) => {
|| process.env.IRVING_EXECUTION_CONTEXT === 'development_server'
) {
Sentry = require('@sentry/node');
vipLogger = require('@automattic/vip-go').logger(namespace);
} else {
Sentry = require('@sentry/react');
}
Expand Down Expand Up @@ -74,11 +76,19 @@ const getService = (namespace) => {
acc[method] = (...messages) => {
// Construct log info object & log it with debug.
const logInfo = generateLogInfo(method, messages);
logger(...logInfo.message);
if (vipLogger[method]) {
vipLogger[method](logInfo.message);
} else {
logger(logInfo.message);
}

// Log stack to the console if we have an error.
if (logInfo.stack) {
logger(logInfo.stack);
if (vipLogger[method]) {
vipLogger[method](logInfo.stack);
} else {
logger(logInfo.stack);
}
}

// If we have an error, do a couple more things with it.
Expand Down

0 comments on commit 6f90289

Please sign in to comment.