Cloud code logs (#2370)
* Adds logging (info/error) when running cloudCode triggers * Adds logs for cloud-function calls - Match parse.com original logging
This commit is contained in:
committed by
Tyler Brock
parent
93b41170fd
commit
3b2da1d734
@@ -16,9 +16,14 @@ describe("Cloud Code Logger", () => {
|
|||||||
return logController.getLogs({from: Date.now() - 500, size: 1000});
|
return logController.getLogs({from: Date.now() - 500, size: 1000});
|
||||||
}).then((res) => {
|
}).then((res) => {
|
||||||
expect(res.length).not.toBe(0);
|
expect(res.length).not.toBe(0);
|
||||||
let lastLogs = res.slice(0, 2);
|
let lastLogs = res.slice(0, 3);
|
||||||
let errorMessage = lastLogs[0];
|
let cloudFunctionMessage = lastLogs[0];
|
||||||
let infoMessage = lastLogs[1];
|
let errorMessage = lastLogs[1];
|
||||||
|
let infoMessage = lastLogs[2];
|
||||||
|
expect(cloudFunctionMessage.level).toBe('info');
|
||||||
|
expect(cloudFunctionMessage.params).toEqual({});
|
||||||
|
expect(cloudFunctionMessage.message).toEqual('Ran cloud function loggerTest with:\nInput: {}\nResult: {}');
|
||||||
|
expect(cloudFunctionMessage.functionName).toEqual('loggerTest');
|
||||||
expect(errorMessage.level).toBe('error');
|
expect(errorMessage.level).toBe('error');
|
||||||
expect(errorMessage.error).toBe('there was an error');
|
expect(errorMessage.error).toBe('there was an error');
|
||||||
expect(errorMessage.message).toBe('logTest error log');
|
expect(errorMessage.message).toBe('logTest error log');
|
||||||
@@ -43,9 +48,13 @@ describe("Cloud Code Logger", () => {
|
|||||||
return logController.getLogs({from: Date.now() - 500, size: 1000})
|
return logController.getLogs({from: Date.now() - 500, size: 1000})
|
||||||
}).then((res) => {
|
}).then((res) => {
|
||||||
expect(res.length).not.toBe(0);
|
expect(res.length).not.toBe(0);
|
||||||
let lastLogs = res.slice(0, 2);
|
let lastLogs = res.slice(0, 3);
|
||||||
let errorMessage = lastLogs[0];
|
let cloudTriggerMessage = lastLogs[0];
|
||||||
let infoMessage = lastLogs[1];
|
let errorMessage = lastLogs[1];
|
||||||
|
let infoMessage = lastLogs[2];
|
||||||
|
expect(cloudTriggerMessage.level).toBe('info');
|
||||||
|
expect(cloudTriggerMessage.input).toEqual({});
|
||||||
|
expect(cloudTriggerMessage.message).toEqual('beforeSave triggered for MyObject\nInput: {}\nResult: {}');
|
||||||
expect(errorMessage.level).toBe('error');
|
expect(errorMessage.level).toBe('error');
|
||||||
expect(errorMessage.error).toBe('there was an error');
|
expect(errorMessage.error).toBe('there was an error');
|
||||||
expect(errorMessage.message).toBe('beforeSave MyObject error log');
|
expect(errorMessage.message).toBe('beforeSave MyObject error log');
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ var express = require('express'),
|
|||||||
|
|
||||||
import PromiseRouter from '../PromiseRouter';
|
import PromiseRouter from '../PromiseRouter';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import { logger } from '../logger';
|
||||||
|
|
||||||
function parseObject(obj) {
|
function parseObject(obj) {
|
||||||
if (Array.isArray(obj)) {
|
if (Array.isArray(obj)) {
|
||||||
@@ -76,7 +77,21 @@ export class FunctionsRouter extends PromiseRouter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
var response = FunctionsRouter.createResponseObject(resolve, reject);
|
var response = FunctionsRouter.createResponseObject((result) => {
|
||||||
|
logger.info(`Ran cloud function ${req.params.functionName} with:\nInput: ${JSON.stringify(params)}\nResult: ${JSON.stringify(result.response.result)}`, {
|
||||||
|
functionName: req.params.functionName,
|
||||||
|
params,
|
||||||
|
result: result.response.resut
|
||||||
|
});
|
||||||
|
resolve(result);
|
||||||
|
}, (error) => {
|
||||||
|
logger.error(`Failed running cloud function ${req.params.functionName} with:\nInput: ${JSON.stringify(params)}\Error: ${JSON.stringify(error)}`, {
|
||||||
|
functionName: req.params.functionName,
|
||||||
|
params,
|
||||||
|
error
|
||||||
|
});
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
// Force the keys before the function calls.
|
// Force the keys before the function calls.
|
||||||
Parse.applicationId = req.config.applicationId;
|
Parse.applicationId = req.config.applicationId;
|
||||||
Parse.javascriptKey = req.config.javascriptKey;
|
Parse.javascriptKey = req.config.javascriptKey;
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ function del(config, auth, className, objectId, clientSDK) {
|
|||||||
// Returns a promise for a {response, status, location} object.
|
// Returns a promise for a {response, status, location} object.
|
||||||
function create(config, auth, className, restObject, clientSDK) {
|
function create(config, auth, className, restObject, clientSDK) {
|
||||||
enforceRoleSecurity('create', className, auth);
|
enforceRoleSecurity('create', className, auth);
|
||||||
var write = new RestWrite(config, auth, className, null, restObject, clientSDK);
|
var write = new RestWrite(config, auth, className, null, restObject, null, clientSDK);
|
||||||
return write.execute();
|
return write.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// triggers.js
|
// triggers.js
|
||||||
import Parse from 'parse/node';
|
import Parse from 'parse/node';
|
||||||
import AppCache from './cache';
|
import AppCache from './cache';
|
||||||
|
import { logger } from './logger';
|
||||||
|
|
||||||
export const Types = {
|
export const Types = {
|
||||||
beforeSave: 'beforeSave',
|
beforeSave: 'beforeSave',
|
||||||
@@ -152,6 +153,36 @@ export function getResponseObject(request, resolve, reject) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function logTrigger(triggerType, className, input) {
|
||||||
|
if (triggerType.indexOf('after') != 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
logger.info(`${triggerType} triggered for ${className}\nInput: ${JSON.stringify(input)}`, {
|
||||||
|
className,
|
||||||
|
triggerType,
|
||||||
|
input
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function logTriggerSuccess(triggerType, className, input, result) {
|
||||||
|
logger.info(`${triggerType} triggered for ${className}\nInput: ${JSON.stringify(input)}\nResult: ${JSON.stringify(result)}`, {
|
||||||
|
className,
|
||||||
|
triggerType,
|
||||||
|
input,
|
||||||
|
result
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function logTriggerError(triggerType, className, input, error) {
|
||||||
|
logger.error(`${triggerType} failed for ${className}\nInput: ${JSON.stringify(input)}\Error: ${JSON.stringify(error)}`, {
|
||||||
|
className,
|
||||||
|
triggerType,
|
||||||
|
input,
|
||||||
|
error
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// To be used as part of the promise chain when saving/deleting an object
|
// To be used as part of the promise chain when saving/deleting an object
|
||||||
// Will resolve successfully if no trigger is configured
|
// Will resolve successfully if no trigger is configured
|
||||||
// Resolves to an object, empty or containing an object key. A beforeSave
|
// Resolves to an object, empty or containing an object key. A beforeSave
|
||||||
@@ -165,11 +196,19 @@ export function maybeRunTrigger(triggerType, auth, parseObject, originalParseObj
|
|||||||
var trigger = getTrigger(parseObject.className, triggerType, config.applicationId);
|
var trigger = getTrigger(parseObject.className, triggerType, config.applicationId);
|
||||||
if (!trigger) return resolve();
|
if (!trigger) return resolve();
|
||||||
var request = getRequestObject(triggerType, auth, parseObject, originalParseObject, config);
|
var request = getRequestObject(triggerType, auth, parseObject, originalParseObject, config);
|
||||||
var response = getResponseObject(request, resolve, reject);
|
var response = getResponseObject(request, (object) => {
|
||||||
|
logTriggerSuccess(triggerType, parseObject.className, parseObject.toJSON(), object);
|
||||||
|
resolve(object);
|
||||||
|
}, (error) => {
|
||||||
|
logTriggerError(triggerType, parseObject.className, parseObject.toJSON(), error);
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
// Force the current Parse app before the trigger
|
// Force the current Parse app before the trigger
|
||||||
Parse.applicationId = config.applicationId;
|
Parse.applicationId = config.applicationId;
|
||||||
Parse.javascriptKey = config.javascriptKey || '';
|
Parse.javascriptKey = config.javascriptKey || '';
|
||||||
Parse.masterKey = config.masterKey;
|
Parse.masterKey = config.masterKey;
|
||||||
|
// For the afterSuccess / afterDelete
|
||||||
|
logTrigger(triggerType, parseObject.className, parseObject.toJSON());
|
||||||
trigger(request, response);
|
trigger(request, response);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user