Skip to content

Commit

Permalink
Merge branch 'main' into nikita/improve-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
schehata committed Jan 9, 2023
2 parents 5b039bf + f1a84b1 commit 1a4c70d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
12 changes: 12 additions & 0 deletions __tests__/log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ test('flushing child loggers', async () => {
await log.flush();
expect(fetch).toHaveBeenCalledTimes(3);
});

test('throwing exception', async () => {
global.fetch = jest.fn() as jest.Mock;
const err = new Error('test');
log.error('hello, world!', err);
await log.flush();
expect(fetch).toHaveBeenCalledTimes(1);
const payload = JSON.parse((fetch as jest.Mock).mock.calls[0][1].body);
expect(Object.keys(payload[0].fields).length).toEqual(3); // { name, message, stack }
expect(payload[0].fields.message).toEqual(err.message);
expect(payload[0].fields.name).toEqual(err.name);
});
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 25 additions & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ export class Logger {
const logEvent: LogEvent = { level, message, _time: new Date(Date.now()).toISOString(), fields: this.args || {} };

// check if passed args is an object, if its not an object, add it to fields.args
if (typeof args === 'object' && args !== null && Object.keys(args).length > 0) {
logEvent.fields = { ...logEvent.fields, ...args };
if (args instanceof Error) {
logEvent.fields = { ...logEvent.fields, message: args.message, stack: args.stack, name: args.name };
} else if (typeof args === 'object' && args !== null && Object.keys(args).length > 0) {
const parsedArgs = JSON.parse(JSON.stringify(args, jsonFriendlyErrorReplacer));
logEvent.fields = { ...logEvent.fields, ...parsedArgs };
} else if (args && args.length) {
logEvent.fields = { ...logEvent.fields, args: args };
}
Expand Down Expand Up @@ -153,7 +156,11 @@ export class Logger {
const fetch = await require('whatwg-fetch');
await fetch(url, reqOptions);
} else if (config.isBrowser && isVercel && navigator.sendBeacon) {
navigator.sendBeacon(url, body);
// sendBeacon fails if message size is greater than 64kb, so
// we fall back to fetch.
if (!navigator.sendBeacon(url, body)) {
await fetch(url, reqOptions);
}
} else {
await fetch(url, reqOptions);
}
Expand Down Expand Up @@ -226,3 +233,18 @@ export function prettyPrint(ev: LogEvent) {

console.log.apply(console, [msgString, ...args]);
}

function jsonFriendlyErrorReplacer(key: string, value: any) {
if (value instanceof Error) {
return {
// Pull all enumerable properties, supporting properties on custom Errors
...value,
// Explicitly pull Error's non-enumerable properties
name: value.name,
message: value.message,
stack: value.stack,
};
}

return value;
}

0 comments on commit 1a4c70d

Please sign in to comment.