Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
"tslib": "^2.3.0"
},
"devDependencies": {
"@types/jest": "^26.0.23",
"@types/node": "^15.14.0",
"@typescript-eslint/eslint-plugin": "^4.28.1",
"@typescript-eslint/parser": "^4.28.1",
"eslint": "^7.29.0",
"@types/jest": "^29.2.6",
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
"eslint": "^8.32.0",
"eslint-config-prettier": "^8.3.0",
"jest": "^27.0.6",
"jest": "^29.3.1",
"jest-date-mock": "^1.0.8",
"npm-run-all": "^4.1.5",
"prettier": "^2.3.2",
"ts-jest": "^27.0.3",
"ts-jest": "^29.0.5",
"typescript": "^4.3.5"
},
"scripts": {
Expand Down
32 changes: 28 additions & 4 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ export default class Logger {
return this;
}

/**
* Reset logStreamName for refresh from logStreamNameResolver.
*/
public async resetLogStreamName(): Promise<void> {
await this.deleteCache('logStreamName');
}

/**
* Bootstrap Logger.
*
Expand Down Expand Up @@ -136,7 +143,12 @@ export default class Logger {
originalConsole[level] = globalConsole[level].bind(globalConsole);
globalConsole[level] = async (message, ...args): Promise<void> => {
// Listen overridden console.*() function calls (type="console", level="*")
await this.onError(new Error(message), { type: 'console', level });
await this.onError(new Error(message), {
type: 'console',
level,
args,
originalMessage: message,
});
if (!this.muting) {
originalConsole[level](message, ...args);
}
Expand Down Expand Up @@ -167,9 +179,21 @@ export default class Logger {
return;
}

const message = this.messageFormatter
? await this.messageFormatter(e, info) // Custom formatter
: JSON.stringify({ message: e.message, ...info }); // Simple JSON formatter
let message: string | null;
if (this.messageFormatter) {
message = await this.messageFormatter(e, info); // Custom formatter
} else {
const { args, originalMessage, ...otherInfo } = info ?? {};
const converToString = (value: any) =>
!(value instanceof Error) && typeof value === 'object'
? JSON.stringify(value)
: value;
let msg = converToString(originalMessage ?? e.message);
if (args && Array.isArray(args)) {
msg = [msg, ...args.map(converToString)].join(' ');
}
message = JSON.stringify({ message: msg, ...otherInfo }); // Simple JSON formatter
}

// Abort when received null
if (!message) {
Expand Down
18 changes: 17 additions & 1 deletion tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ let globalConsole: DummyConsole;
let storage: DummyStorage;
let eventTarget: DummyEventTarget;

jest.useFakeTimers('legacy');
jest.useFakeTimers({
legacyFakeTimers: true,
});

const install = (options: Partial<InstallOptions> = {}): void => {
logger = new Logger('key', 'secret', 'ap-northeast-1', 'example');
Expand Down Expand Up @@ -78,6 +80,20 @@ describe('Collecting errors', (): void => {
]);
});

it('should receive from console with args', async (): Promise<void> => {
await globalConsole.error({ a: 1 }, { b: 2 }, ['a'], 'b');
expect((logger as any).events).toStrictEqual([
{
message: JSON.stringify({
message: '{"a":1} {"b":2} ["a"] b',
type: 'console',
level: 'error',
}),
timestamp: 0,
},
]);
});

it('should receive from custom trigger', async (): Promise<void> => {
await logger.onError(new Error('Something went wrong'), { type: 'custom' });
expect((logger as any).events).toStrictEqual([
Expand Down
41 changes: 31 additions & 10 deletions tests/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,45 @@ export class DummyStorage implements StorageInterface {
export interface DummyConsoleMessage {
message: unknown;
level: Level;
args?: any[];
}

export class DummyConsole implements ConsoleInterface {
public messages: DummyConsoleMessage[] = [];
public debug(message?: unknown): void {
this.messages.push({ message, level: 'debug' });
public debug(message?: unknown, ...optionalParams: any[]): void {
const msg: DummyConsoleMessage = { message, level: 'debug' };
if (optionalParams.length) {
msg.args = optionalParams;
}
this.messages.push(msg);
}
public info(message?: unknown): void {
this.messages.push({ message, level: 'info' });
public info(message?: unknown, ...optionalParams: any[]): void {
const msg: DummyConsoleMessage = { message, level: 'info' };
if (optionalParams.length) {
msg.args = optionalParams;
}
this.messages.push(msg);
}
public log(message?: unknown): void {
this.messages.push({ message, level: 'log' });
public log(message?: unknown, ...optionalParams: any[]): void {
const msg: DummyConsoleMessage = { message, level: 'log' };
if (optionalParams.length) {
msg.args = optionalParams;
}
this.messages.push(msg);
}
public error(message?: unknown): void {
this.messages.push({ message, level: 'error' });
public error(message?: unknown, ...optionalParams: any[]): void {
const msg: DummyConsoleMessage = { message, level: 'error' };
if (optionalParams.length) {
msg.args = optionalParams;
}
this.messages.push(msg);
}
public warn(message?: unknown): void {
this.messages.push({ message, level: 'warn' });
public warn(message?: unknown, ...optionalParams: any[]): void {
const msg: DummyConsoleMessage = { message, level: 'warn' };
if (optionalParams.length) {
msg.args = optionalParams;
}
this.messages.push(msg);
}
}

Expand Down