Skip to content

Commit 2caea31

Browse files
authored
feat: Add option to change the log level of logs emitted by Cloud Functions (#8530)
1 parent 1302853 commit 2caea31

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

spec/CloudCodeLogger.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,42 @@ describe('Cloud Code Logger', () => {
182182
});
183183
});
184184

185+
it('should log cloud function execution using the custom log level', async done => {
186+
Parse.Cloud.define('aFunction', () => {
187+
return 'it worked!';
188+
});
189+
190+
Parse.Cloud.define('bFunction', () => {
191+
throw new Error('Failed');
192+
});
193+
194+
await Parse.Cloud.run('aFunction', { foo: 'bar' }).then(() => {
195+
const log = spy.calls.allArgs().find(log => log[1].startsWith('Ran cloud function '))?.[0];
196+
expect(log).toEqual('info');
197+
});
198+
199+
await reconfigureServer({
200+
silent: true,
201+
logLevels: {
202+
cloudFunctionSuccess: 'warn',
203+
cloudFunctionError: 'info',
204+
},
205+
});
206+
207+
spy = spyOn(Config.get('test').loggerController.adapter, 'log').and.callThrough();
208+
209+
try {
210+
await Parse.Cloud.run('bFunction', { foo: 'bar' });
211+
throw new Error('bFunction should have failed');
212+
} catch {
213+
const log = spy.calls
214+
.allArgs()
215+
.find(log => log[1].startsWith('Failed running cloud function bFunction for '))?.[0];
216+
expect(log).toEqual('info');
217+
done();
218+
}
219+
});
220+
185221
it('should log cloud function triggers using the custom log level', async () => {
186222
Parse.Cloud.beforeSave('TestClass', () => {});
187223
Parse.Cloud.afterSave('TestClass', () => {});

src/Options/Definitions.js

+10
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,16 @@ module.exports.AuthAdapter = {
993993
},
994994
};
995995
module.exports.LogLevels = {
996+
cloudFunctionError: {
997+
env: 'PARSE_SERVER_LOG_LEVELS_CLOUD_FUNCTION_ERROR',
998+
help: 'Log level used by the Cloud Code Functions on error. Default is `error`.',
999+
default: 'error',
1000+
},
1001+
cloudFunctionSuccess: {
1002+
env: 'PARSE_SERVER_LOG_LEVELS_CLOUD_FUNCTION_SUCCESS',
1003+
help: 'Log level used by the Cloud Code Functions on success. Default is `info`.',
1004+
default: 'info',
1005+
},
9961006
triggerAfter: {
9971007
env: 'PARSE_SERVER_LOG_LEVELS_TRIGGER_AFTER',
9981008
help:

src/Options/docs.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Options/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,12 @@ export interface LogLevels {
577577
:DEFAULT: error
578578
*/
579579
triggerBeforeError: ?string;
580+
/* Log level used by the Cloud Code Functions on success. Default is `info`.
581+
:DEFAULT: info
582+
*/
583+
cloudFunctionSuccess: ?string;
584+
/* Log level used by the Cloud Code Functions on error. Default is `error`.
585+
:DEFAULT: error
586+
*/
587+
cloudFunctionError: ?string;
580588
}

src/Routers/FunctionsRouter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class FunctionsRouter extends PromiseRouter {
140140
result => {
141141
try {
142142
const cleanResult = logger.truncateLogMessage(JSON.stringify(result.response.result));
143-
logger.info(
143+
logger[req.config.logLevels.cloudFunctionSuccess](
144144
`Ran cloud function ${functionName} for user ${userString} with:\n Input: ${cleanInput}\n Result: ${cleanResult}`,
145145
{
146146
functionName,
@@ -155,7 +155,7 @@ export class FunctionsRouter extends PromiseRouter {
155155
},
156156
error => {
157157
try {
158-
logger.error(
158+
logger[req.config.logLevels.cloudFunctionError](
159159
`Failed running cloud function ${functionName} for user ${userString} with:\n Input: ${cleanInput}\n Error: ` +
160160
JSON.stringify(error),
161161
{

0 commit comments

Comments
 (0)