Skip to content

Commit de9d057

Browse files
authored
feat: Add option Parse.nodeLogging to fully log Parse.Object in Node.js environments (#1594)
1 parent 38a2e8a commit de9d057

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

src/CoreManager.ts

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ const config: Config & Record<string, any> = {
355355
IDEMPOTENCY: false,
356356
ALLOW_CUSTOM_OBJECT_ID: false,
357357
PARSE_ERRORS: [],
358+
NODE_LOGGING: false,
358359
};
359360

360361
function requireMethods(name: string, methods: string[], controller: any) {

src/Parse.ts

+22
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,28 @@ const Parse = {
314314
return CoreManager.get('ALLOW_CUSTOM_OBJECT_ID');
315315
},
316316

317+
/**
318+
* Setting this property to `true` enables enhanced logging for `Parse.Object`
319+
* in Node.js environments. Specifically, it will log:
320+
*
321+
* ```
322+
* ParseObject: className: <CLASS_NAME>, id: <OBJECT_ID>
323+
* Attributes: <OBJECT_ATTRIBUTES>
324+
* ```
325+
*
326+
* @warning This should not be enabled in production environments as this may
327+
* expose sensitive information in server logs.
328+
*
329+
* @property {boolean} Parse.nodeLogging
330+
* @static
331+
*/
332+
set nodeLogging(value) {
333+
CoreManager.set('NODE_LOGGING', value);
334+
},
335+
get nodeLogging() {
336+
return CoreManager.get('NODE_LOGGING');
337+
},
338+
317339
_request(...args) {
318340
return CoreManager.getRESTController().request.apply(null, args);
319341
},

src/ParseObject.ts

+7
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ class ParseObject<T extends Attributes = Attributes> {
186186
throw new Error("Can't create an invalid Parse Object");
187187
}
188188
}
189+
if (CoreManager.get('NODE_LOGGING')) {
190+
this[Symbol.for('nodejs.util.inspect.custom')] = function () {
191+
return `ParseObject: className: ${this.className}, id: ${
192+
this.id
193+
}\nAttributes: ${JSON.stringify(this.attributes, null, 2)}`;
194+
};
195+
}
189196
}
190197

191198
/**

src/__tests__/Parse-test.js

+7
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ describe('Parse module', () => {
206206
Parse.allowCustomObjectId = false;
207207
});
208208

209+
it('can set nodeLogging', () => {
210+
expect(Parse.nodeLogging).toBe(false);
211+
Parse.nodeLogging = true;
212+
expect(CoreManager.get('NODE_LOGGING')).toBe(true);
213+
Parse.nodeLogging = false;
214+
});
215+
209216
it('getServerHealth', () => {
210217
const controller = {
211218
request: jest.fn(),

src/__tests__/ParseObject-test.js

+10
Original file line numberDiff line numberDiff line change
@@ -3532,4 +3532,14 @@ describe('ParseObject pin', () => {
35323532
});
35333533
CoreManager.set('ALLOW_CUSTOM_OBJECT_ID', false);
35343534
});
3535+
3536+
it('can log an object', () => {
3537+
CoreManager.set('NODE_LOGGING', true);
3538+
const o = new ParseObject('Person', { foo: 'bar' });
3539+
const symbol = Symbol.for('nodejs.util.inspect.custom');
3540+
expect(o[symbol]()).toBe(
3541+
`ParseObject: className: Person, id: undefined\nAttributes: {\n \"foo\": \"bar\"\n}`
3542+
);
3543+
CoreManager.set('NODE_LOGGING', false);
3544+
});
35353545
});

types/Parse.d.ts

+16
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,22 @@ declare const Parse: {
322322
* @static
323323
*/
324324
allowCustomObjectId: any;
325+
/**
326+
* Setting this property to `true` enables enhanced logging for `Parse.Object`
327+
* in Node.js environments. Specifically, it will log:
328+
*
329+
* ```
330+
* ParseObject: className: <CLASS_NAME>, id: <OBJECT_ID>
331+
* Attributes: <OBJECT_ATTRIBUTES>
332+
* ```
333+
*
334+
* @warning This should not be enabled in production environments as this may
335+
* expose sensitive information in server logs.
336+
*
337+
* @property {boolean} Parse.nodeLogging
338+
* @static
339+
*/
340+
nodeLogging: any;
325341
_request(...args: any[]): any;
326342
_ajax(...args: any[]): any;
327343
_decode(_: any, value: any): any;

0 commit comments

Comments
 (0)