Skip to content

Commit

Permalink
feat: Add option to change the log level of logs emitted by Cloud Fun…
Browse files Browse the repository at this point in the history
…ctions (#8530)
  • Loading branch information
alljinx authored May 9, 2023
1 parent 1302853 commit 2caea31
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
36 changes: 36 additions & 0 deletions spec/CloudCodeLogger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,42 @@ 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: {
cloudFunctionSuccess: 'warn',
cloudFunctionError: 'info',
},
});

spy = spyOn(Config.get('test').loggerController.adapter, 'log').and.callThrough();

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 () => {
Parse.Cloud.beforeSave('TestClass', () => {});
Parse.Cloud.afterSave('TestClass', () => {});
Expand Down
10 changes: 10 additions & 0 deletions src/Options/Definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,16 @@ module.exports.AuthAdapter = {
},
};
module.exports.LogLevels = {
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: {
env: 'PARSE_SERVER_LOG_LEVELS_TRIGGER_AFTER',
help:
Expand Down
2 changes: 2 additions & 0 deletions src/Options/docs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,12 @@ export interface LogLevels {
:DEFAULT: error
*/
triggerBeforeError: ?string;
/* 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;
}
4 changes: 2 additions & 2 deletions src/Routers/FunctionsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.cloudFunctionSuccess](
`Ran cloud function ${functionName} for user ${userString} with:\n Input: ${cleanInput}\n Result: ${cleanResult}`,
{
functionName,
Expand All @@ -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),
{
Expand Down

0 comments on commit 2caea31

Please sign in to comment.