From 71f88127290999285e97c22ce7c3c93119fc9255 Mon Sep 17 00:00:00 2001 From: alljinx Date: Thu, 4 May 2023 08:07:37 +0200 Subject: [PATCH 1/5] Add cloudFunctionRan function to logLevels configuration #8529 --- spec/CloudCodeLogger.spec.js | 23 +++++++++++++++++++++++ src/Options/Definitions.js | 5 +++++ src/Options/docs.js | 1 + src/Options/index.js | 4 ++++ src/Routers/FunctionsRouter.js | 2 +- 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/spec/CloudCodeLogger.spec.js b/spec/CloudCodeLogger.spec.js index 2cde9de640..7fcb16ea76 100644 --- a/spec/CloudCodeLogger.spec.js +++ b/spec/CloudCodeLogger.spec.js @@ -182,6 +182,29 @@ describe('Cloud Code Logger', () => { }); }); + it('should log cloud function ran using the custom log level', async done => { + await reconfigureServer({ + silent: true, + logLevels: { + cloudFunctionRan: 'warn', + }, + }); + + Parse.Cloud.define('aFunction', () => { + return 'it worked!'; + }); + + spy = spyOn(Config.get('test').loggerController.adapter, 'log').and.callThrough(); + + Parse.Cloud.run('aFunction', { foo: 'bar' }).then(() => { + const log = spy.calls.allArgs().find(log => log[1].startsWith('Ran cloud function '))?.[0]; + + expect(log).toEqual('warn'); + + done(); + }); + }); + it('should log cloud function triggers using the custom log level', async () => { Parse.Cloud.beforeSave('TestClass', () => {}); Parse.Cloud.afterSave('TestClass', () => {}); diff --git a/src/Options/Definitions.js b/src/Options/Definitions.js index 7987363ff2..be3d0d0bc7 100644 --- a/src/Options/Definitions.js +++ b/src/Options/Definitions.js @@ -993,6 +993,11 @@ module.exports.AuthAdapter = { }, }; module.exports.LogLevels = { + cloudFunctionRan: { + env: 'PARSE_SERVER_LOG_LEVELS_CLOUD_FUNCTION_RAN', + help: 'Log level used by the Cloud Code Functions. Default is `info`.', + default: 'info', + }, triggerAfter: { env: 'PARSE_SERVER_LOG_LEVELS_TRIGGER_AFTER', help: diff --git a/src/Options/docs.js b/src/Options/docs.js index b5a78aace1..d89831aee4 100644 --- a/src/Options/docs.js +++ b/src/Options/docs.js @@ -236,6 +236,7 @@ /** * @interface LogLevels + * @property {String} cloudFunctionRan Log level used by the Cloud Code Functions. Default is `info`. * @property {String} triggerAfter Log level used by the Cloud Code Triggers `afterSave`, `afterDelete`, `afterSaveFile`, `afterDeleteFile`, `afterFind`, `afterLogout`. Default is `info`. * @property {String} triggerBeforeError Log level used by the Cloud Code Triggers `beforeSave`, `beforeSaveFile`, `beforeDeleteFile`, `beforeFind`, `beforeLogin` on error. Default is `error `. * @property {String} triggerBeforeSuccess Log level used by the Cloud Code Triggers `beforeSave`, `beforeSaveFile`, `beforeDeleteFile`, `beforeFind`, `beforeLogin` on success. Default is `info`. diff --git a/src/Options/index.js b/src/Options/index.js index 009b31a5d5..a1aebb0cc9 100644 --- a/src/Options/index.js +++ b/src/Options/index.js @@ -577,4 +577,8 @@ export interface LogLevels { :DEFAULT: error */ triggerBeforeError: ?string; + /* Log level used by the Cloud Code Functions. Default is `info`. + :DEFAULT: info + */ + cloudFunctionRan: ?string; } diff --git a/src/Routers/FunctionsRouter.js b/src/Routers/FunctionsRouter.js index d239908103..1d8a11afe8 100644 --- a/src/Routers/FunctionsRouter.js +++ b/src/Routers/FunctionsRouter.js @@ -140,7 +140,7 @@ export class FunctionsRouter extends PromiseRouter { result => { try { const cleanResult = logger.truncateLogMessage(JSON.stringify(result.response.result)); - logger.info( + logger[req.config.logLevels.cloudFunctionRan]( `Ran cloud function ${functionName} for user ${userString} with:\n Input: ${cleanInput}\n Result: ${cleanResult}`, { functionName, From 4030f905744effd48f3599337448580faf1446d1 Mon Sep 17 00:00:00 2001 From: alljinx <42472198+alljinx@users.noreply.github.com> Date: Tue, 9 May 2023 09:56:12 +0200 Subject: [PATCH 2/5] Update spec/CloudCodeLogger.spec.js Co-authored-by: Manuel <5673677+mtrezza@users.noreply.github.com> Signed-off-by: alljinx <42472198+alljinx@users.noreply.github.com> --- spec/CloudCodeLogger.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/CloudCodeLogger.spec.js b/spec/CloudCodeLogger.spec.js index 7fcb16ea76..1b98020247 100644 --- a/spec/CloudCodeLogger.spec.js +++ b/spec/CloudCodeLogger.spec.js @@ -182,7 +182,7 @@ describe('Cloud Code Logger', () => { }); }); - it('should log cloud function ran using the custom log level', async done => { + it('should log cloud function execution using the custom log level', async done => { await reconfigureServer({ silent: true, logLevels: { From e56de7c434a6287a75ece2a41a68abec057bf0a1 Mon Sep 17 00:00:00 2001 From: alljinx <42472198+alljinx@users.noreply.github.com> Date: Tue, 9 May 2023 09:58:07 +0200 Subject: [PATCH 3/5] Update spec/CloudCodeLogger.spec.js Co-authored-by: Manuel <5673677+mtrezza@users.noreply.github.com> Signed-off-by: alljinx <42472198+alljinx@users.noreply.github.com> --- spec/CloudCodeLogger.spec.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/CloudCodeLogger.spec.js b/spec/CloudCodeLogger.spec.js index 1b98020247..78dd659204 100644 --- a/spec/CloudCodeLogger.spec.js +++ b/spec/CloudCodeLogger.spec.js @@ -198,9 +198,7 @@ describe('Cloud Code Logger', () => { Parse.Cloud.run('aFunction', { foo: 'bar' }).then(() => { const log = spy.calls.allArgs().find(log => log[1].startsWith('Ran cloud function '))?.[0]; - expect(log).toEqual('warn'); - done(); }); }); From 09f18ff76f9da2175c365d05a2a2597aa1902f97 Mon Sep 17 00:00:00 2001 From: alljinx <42472198+alljinx@users.noreply.github.com> Date: Tue, 9 May 2023 09:59:13 +0200 Subject: [PATCH 4/5] Update src/Options/index.js Co-authored-by: Manuel <5673677+mtrezza@users.noreply.github.com> Signed-off-by: alljinx <42472198+alljinx@users.noreply.github.com> --- src/Options/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Options/index.js b/src/Options/index.js index a1aebb0cc9..5847c33089 100644 --- a/src/Options/index.js +++ b/src/Options/index.js @@ -580,5 +580,5 @@ export interface LogLevels { /* Log level used by the Cloud Code Functions. Default is `info`. :DEFAULT: info */ - cloudFunctionRan: ?string; + cloudFunctionSuccess: ?string; } From dd72866ca8933bf6b4346fe1fb9eececabf9e652 Mon Sep 17 00:00:00 2001 From: alljinx Date: Tue, 9 May 2023 10:39:12 +0200 Subject: [PATCH 5/5] Add cloudFunctionError --- spec/CloudCodeLogger.spec.js | 33 ++++++++++++++++++++++++--------- src/Options/Definitions.js | 11 ++++++++--- src/Options/docs.js | 3 ++- src/Options/index.js | 6 +++++- src/Routers/FunctionsRouter.js | 4 ++-- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/spec/CloudCodeLogger.spec.js b/spec/CloudCodeLogger.spec.js index 78dd659204..b4dd5a42d8 100644 --- a/spec/CloudCodeLogger.spec.js +++ b/spec/CloudCodeLogger.spec.js @@ -183,24 +183,39 @@ describe('Cloud Code Logger', () => { }); it('should log cloud function execution using the custom log level', async done => { + Parse.Cloud.define('aFunction', () => { + return 'it worked!'; + }); + + Parse.Cloud.define('bFunction', () => { + throw new Error('Failed'); + }); + + await Parse.Cloud.run('aFunction', { foo: 'bar' }).then(() => { + const log = spy.calls.allArgs().find(log => log[1].startsWith('Ran cloud function '))?.[0]; + expect(log).toEqual('info'); + }); + await reconfigureServer({ silent: true, logLevels: { - cloudFunctionRan: 'warn', + cloudFunctionSuccess: 'warn', + cloudFunctionError: 'info', }, }); - Parse.Cloud.define('aFunction', () => { - return 'it worked!'; - }); - spy = spyOn(Config.get('test').loggerController.adapter, 'log').and.callThrough(); - Parse.Cloud.run('aFunction', { foo: 'bar' }).then(() => { - const log = spy.calls.allArgs().find(log => log[1].startsWith('Ran cloud function '))?.[0]; - expect(log).toEqual('warn'); + try { + await Parse.Cloud.run('bFunction', { foo: 'bar' }); + throw new Error('bFunction should have failed'); + } catch { + const log = spy.calls + .allArgs() + .find(log => log[1].startsWith('Failed running cloud function bFunction for '))?.[0]; + expect(log).toEqual('info'); done(); - }); + } }); it('should log cloud function triggers using the custom log level', async () => { diff --git a/src/Options/Definitions.js b/src/Options/Definitions.js index be3d0d0bc7..d6acc948e3 100644 --- a/src/Options/Definitions.js +++ b/src/Options/Definitions.js @@ -993,9 +993,14 @@ module.exports.AuthAdapter = { }, }; module.exports.LogLevels = { - cloudFunctionRan: { - env: 'PARSE_SERVER_LOG_LEVELS_CLOUD_FUNCTION_RAN', - help: 'Log level used by the Cloud Code Functions. Default is `info`.', + cloudFunctionError: { + env: 'PARSE_SERVER_LOG_LEVELS_CLOUD_FUNCTION_ERROR', + help: 'Log level used by the Cloud Code Functions on error. Default is `error`.', + default: 'error', + }, + cloudFunctionSuccess: { + env: 'PARSE_SERVER_LOG_LEVELS_CLOUD_FUNCTION_SUCCESS', + help: 'Log level used by the Cloud Code Functions on success. Default is `info`.', default: 'info', }, triggerAfter: { diff --git a/src/Options/docs.js b/src/Options/docs.js index d89831aee4..8ebf63b97d 100644 --- a/src/Options/docs.js +++ b/src/Options/docs.js @@ -236,7 +236,8 @@ /** * @interface LogLevels - * @property {String} cloudFunctionRan Log level used by the Cloud Code Functions. Default is `info`. + * @property {String} cloudFunctionError Log level used by the Cloud Code Functions on error. Default is `error`. + * @property {String} cloudFunctionSuccess Log level used by the Cloud Code Functions on success. Default is `info`. * @property {String} triggerAfter Log level used by the Cloud Code Triggers `afterSave`, `afterDelete`, `afterSaveFile`, `afterDeleteFile`, `afterFind`, `afterLogout`. Default is `info`. * @property {String} triggerBeforeError Log level used by the Cloud Code Triggers `beforeSave`, `beforeSaveFile`, `beforeDeleteFile`, `beforeFind`, `beforeLogin` on error. Default is `error `. * @property {String} triggerBeforeSuccess Log level used by the Cloud Code Triggers `beforeSave`, `beforeSaveFile`, `beforeDeleteFile`, `beforeFind`, `beforeLogin` on success. Default is `info`. diff --git a/src/Options/index.js b/src/Options/index.js index 5847c33089..59e040b57f 100644 --- a/src/Options/index.js +++ b/src/Options/index.js @@ -577,8 +577,12 @@ export interface LogLevels { :DEFAULT: error */ triggerBeforeError: ?string; - /* Log level used by the Cloud Code Functions. Default is `info`. + /* Log level used by the Cloud Code Functions on success. Default is `info`. :DEFAULT: info */ cloudFunctionSuccess: ?string; + /* Log level used by the Cloud Code Functions on error. Default is `error`. + :DEFAULT: error + */ + cloudFunctionError: ?string; } diff --git a/src/Routers/FunctionsRouter.js b/src/Routers/FunctionsRouter.js index 1d8a11afe8..4972453fc3 100644 --- a/src/Routers/FunctionsRouter.js +++ b/src/Routers/FunctionsRouter.js @@ -140,7 +140,7 @@ export class FunctionsRouter extends PromiseRouter { result => { try { const cleanResult = logger.truncateLogMessage(JSON.stringify(result.response.result)); - logger[req.config.logLevels.cloudFunctionRan]( + logger[req.config.logLevels.cloudFunctionSuccess]( `Ran cloud function ${functionName} for user ${userString} with:\n Input: ${cleanInput}\n Result: ${cleanResult}`, { functionName, @@ -155,7 +155,7 @@ export class FunctionsRouter extends PromiseRouter { }, error => { try { - logger.error( + logger[req.config.logLevels.cloudFunctionError]( `Failed running cloud function ${functionName} for user ${userString} with:\n Input: ${cleanInput}\n Error: ` + JSON.stringify(error), {