Skip to content

feat: Add option to change the log level of logs emitted by Cloud Functions #8530

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
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
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