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

This commit is contained in:
alljinx
2023-05-09 15:03:00 +02:00
committed by GitHub
parent 1302853187
commit 2caea310be
5 changed files with 58 additions and 2 deletions

View File

@@ -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', () => {});

View File

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

View File

@@ -236,6 +236,8 @@
/**
* @interface LogLevels
* @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`.

View File

@@ -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;
}

View File

@@ -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,
@@ -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),
{