Skip to content

Fix logging for objects that define toJSON #916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 14, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- Adds support for setting user labels on functions via `runWith()`.
- Adds support for FIREBASE_CONFIG env as the name of a JSON file
- Fixes an issue where objects that define `toJSON` could not be logged successfully (#907).
- Formalize module exports. Loggers can now be accessed at 'firebase-functions/logger' and 'firebase-functions/logger/compat'
- Fixes an issue where Remote Config coiuld not be emulated in Windows machines on the classic Command Prompt.
17 changes: 17 additions & 0 deletions spec/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';

import * as logger from '../src/logger';

const SUPPORTS_STRUCTURED_LOGS =
Expand Down Expand Up @@ -109,6 +110,22 @@ describe(`logger (${
});
});

it('should not break on objects that override toJSON', () => {
const obj: any = { a: new Date('August 26, 1994 12:24:00Z') };

const entry: logger.LogEntry = {
severity: 'ERROR',
message: 'testing toJSON',
obj,
};
logger.write(entry);
expectStderr({
severity: 'ERROR',
message: 'testing toJSON',
obj: { a: '1994-08-26T12:24:00.000Z' },
});
});

it('should not alter parameters that are logged', () => {
const circ: any = { b: 'foo' };
circ.array = [circ];
Expand Down
4 changes: 4 additions & 0 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ function removeCircular(obj: any, refs: any[] = []): any {
if (typeof obj !== 'object' || !obj) {
return obj;
}
// If the object defines its own toJSON, prefer that.
if (obj['toJSON']) {
return obj.toJSON();
}
if (refs.includes(obj)) {
return '[Circular]';
} else {
Expand Down