Skip to content

Commit

Permalink
refactor: Write JSON strings as structured logs
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Apr 6, 2023
1 parent 88ca273 commit e5f5162
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion node/packages/sdk/lib/create-error-captured-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = (error, options = {}) => {
stack: tags.stacktrace,
};
if (options.fingerprint) errorLogData.fingerprint = options.fingerprint;
console.error(errorLogData);
console.error(JSON.stringify(errorLogData));

return capturedEvent;
};
Expand Down
2 changes: 1 addition & 1 deletion node/packages/sdk/lib/create-warning-captured-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = (message, options = {}) => {
stack: stackTrace,
};
if (options.fingerprint) warnLogData.fingerprint = options.fingerprint;
console.warn(warnLogData);
console.warn(JSON.stringify(warnLogData));

return capturedEvent;
};
Expand Down
17 changes: 14 additions & 3 deletions node/packages/sdk/lib/instrumentation/node-console.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const isError = require('type/error/is');
const isPlainObject = require('type/plain-object/is');
const util = require('util');
const createErrorCapturedEvent = require('../create-error-captured-event');
const createWarningCapturedEvent = require('../create-warning-captured-event');
Expand Down Expand Up @@ -34,7 +33,13 @@ module.exports.install = () => {
original.error.apply(this, args);
try {
const input = args[0];
if (args.length === 1 && isPlainObject(input) && input.source === 'serverlessSdk') return;
if (
args.length === 1 &&
typeof input === 'string' &&
input.startsWith('{"source":"serverlessSdk",')
) {
return;
}
createErrorCapturedEvent(args.length === 1 && isError(input) ? input : resolveMessage(args), {
_origin: 'nodeConsole',
});
Expand All @@ -46,7 +51,13 @@ module.exports.install = () => {
nodeConsole.warn = function (...args) {
original.warn.apply(this, args);
try {
if (args.length === 1 && isPlainObject(args[0]) && args[0].source === 'serverlessSdk') return;
if (
args.length === 1 &&
typeof args[0] === 'string' &&
args[0].startsWith('{"source":"serverlessSdk",')
) {
return;
}
createWarningCapturedEvent(resolveMessage(args), { _origin: 'nodeConsole' });
} catch (error) {
reportError(error);
Expand Down
2 changes: 1 addition & 1 deletion node/packages/sdk/lib/report-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = (error, options = {}) => {
errorData.message = message;
if (error.code) errorData.code = error.code;
if (error.stack) errorData.stack = resolveStackTraceString(error);
console.error(errorData);
console.error(JSON.stringify(errorData));
try {
// Require on spot to avoid otherwise difficult to mitigate circular dependency
require('./create-error-captured-event')(errorData.message, {
Expand Down
14 changes: 8 additions & 6 deletions node/packages/sdk/lib/report-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ const createWarningCapturedEvent = require('./create-warning-captured-event');

module.exports = (message, code, options = {}) => {
const type = options.type || 'INTERNAL';
console.warn({
source: 'serverlessSdk',
type: `WARNING_TYPE_SDK_${type}`,
message,
code,
});
console.warn(
JSON.stringify({
source: 'serverlessSdk',
type: `WARNING_TYPE_SDK_${type}`,
message,
code,
})
);
createWarningCapturedEvent(message, {
_origin: 'nodeConsole',
_type: type === 'USER' ? 'sdkUser' : 'sdkInternal',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('lib/instrumentation/node-console.js', () => {
let capturedEvent = null;
serverlessSdk._eventEmitter.once('captured-event', (event) => (capturedEvent = event));
// eslint-disable-next-line no-console
console.warn({ source: 'serverlessSdk', message: 'Something is wrong' });
console.warn(JSON.stringify({ source: 'serverlessSdk', message: 'Something is wrong' }));

expect(capturedEvent).to.be.null;
});
Expand All @@ -66,15 +66,17 @@ describe('lib/instrumentation/node-console.js', () => {
let capturedEvent = null;
serverlessSdk._eventEmitter.once('captured-event', (event) => (capturedEvent = event));
// eslint-disable-next-line no-console
console.error({
source: 'serverlessSdk',
type: 'ERROR_TYPE_CAUGHT_SDK_INTERNAL',
description: 'Internal Serverless SDK Error',
name: 'Error',
message: 'Something failed',
code: 'ERROR_CODE',
stack: 'at /foo.js:12:1\nat /bar.js:13:1',
});
console.error(
JSON.stringify({
source: 'serverlessSdk',
type: 'ERROR_TYPE_CAUGHT_SDK_INTERNAL',
description: 'Internal Serverless SDK Error',
name: 'Error',
message: 'Something failed',
code: 'ERROR_CODE',
stack: 'at /foo.js:12:1\nat /bar.js:13:1',
})
);
expect(capturedEvent).to.be.null;
});
});

0 comments on commit e5f5162

Please sign in to comment.