diff --git a/src/CoreManager.ts b/src/CoreManager.ts index 03e2b6002..cdf1f1527 100644 --- a/src/CoreManager.ts +++ b/src/CoreManager.ts @@ -355,6 +355,7 @@ const config: Config & Record = { IDEMPOTENCY: false, ALLOW_CUSTOM_OBJECT_ID: false, PARSE_ERRORS: [], + NODE_LOGGING: false, }; function requireMethods(name: string, methods: string[], controller: any) { diff --git a/src/Parse.ts b/src/Parse.ts index 67e55b063..71f3f834f 100644 --- a/src/Parse.ts +++ b/src/Parse.ts @@ -314,6 +314,28 @@ const Parse = { return CoreManager.get('ALLOW_CUSTOM_OBJECT_ID'); }, + /** + * Setting this property to `true` enables enhanced logging for `Parse.Object` + * in Node.js environments. Specifically, it will log: + * + * ``` + * ParseObject: className: , id: + * Attributes: + * ``` + * + * @warning This should not be enabled in production environments as this may + * expose sensitive information in server logs. + * + * @property {boolean} Parse.nodeLogging + * @static + */ + set nodeLogging(value) { + CoreManager.set('NODE_LOGGING', value); + }, + get nodeLogging() { + return CoreManager.get('NODE_LOGGING'); + }, + _request(...args) { return CoreManager.getRESTController().request.apply(null, args); }, diff --git a/src/ParseObject.ts b/src/ParseObject.ts index 7979f7dcd..9ecb3292e 100644 --- a/src/ParseObject.ts +++ b/src/ParseObject.ts @@ -186,6 +186,13 @@ class ParseObject { throw new Error("Can't create an invalid Parse Object"); } } + if (CoreManager.get('NODE_LOGGING')) { + this[Symbol.for('nodejs.util.inspect.custom')] = function () { + return `ParseObject: className: ${this.className}, id: ${ + this.id + }\nAttributes: ${JSON.stringify(this.attributes, null, 2)}`; + }; + } } /** diff --git a/src/__tests__/Parse-test.js b/src/__tests__/Parse-test.js index 2709ff15c..2476e1cad 100644 --- a/src/__tests__/Parse-test.js +++ b/src/__tests__/Parse-test.js @@ -206,6 +206,13 @@ describe('Parse module', () => { Parse.allowCustomObjectId = false; }); + it('can set nodeLogging', () => { + expect(Parse.nodeLogging).toBe(false); + Parse.nodeLogging = true; + expect(CoreManager.get('NODE_LOGGING')).toBe(true); + Parse.nodeLogging = false; + }); + it('getServerHealth', () => { const controller = { request: jest.fn(), diff --git a/src/__tests__/ParseObject-test.js b/src/__tests__/ParseObject-test.js index 973878bf8..60f7cecab 100644 --- a/src/__tests__/ParseObject-test.js +++ b/src/__tests__/ParseObject-test.js @@ -3532,4 +3532,14 @@ describe('ParseObject pin', () => { }); CoreManager.set('ALLOW_CUSTOM_OBJECT_ID', false); }); + + it('can log an object', () => { + CoreManager.set('NODE_LOGGING', true); + const o = new ParseObject('Person', { foo: 'bar' }); + const symbol = Symbol.for('nodejs.util.inspect.custom'); + expect(o[symbol]()).toBe( + `ParseObject: className: Person, id: undefined\nAttributes: {\n \"foo\": \"bar\"\n}` + ); + CoreManager.set('NODE_LOGGING', false); + }); }); diff --git a/types/Parse.d.ts b/types/Parse.d.ts index 8eb283059..b53d19e7e 100644 --- a/types/Parse.d.ts +++ b/types/Parse.d.ts @@ -322,6 +322,22 @@ declare const Parse: { * @static */ allowCustomObjectId: any; + /** + * Setting this property to `true` enables enhanced logging for `Parse.Object` + * in Node.js environments. Specifically, it will log: + * + * ``` + * ParseObject: className: , id: + * Attributes: + * ``` + * + * @warning This should not be enabled in production environments as this may + * expose sensitive information in server logs. + * + * @property {boolean} Parse.nodeLogging + * @static + */ + nodeLogging: any; _request(...args: any[]): any; _ajax(...args: any[]): any; _decode(_: any, value: any): any;