Skip to content

Commit

Permalink
Log objects rather than JSON strings and option for single line logs
Browse files Browse the repository at this point in the history
This reverts commit fcd914b.
  • Loading branch information
spenthil committed Jun 10, 2016
1 parent a00d795 commit bc961d8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 24 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ That's it! You are now running a standalone version of Parse Server on your mach

**Using a remote MongoDB?** Pass the `--databaseURI DATABASE_URI` parameter when starting `parse-server`. Learn more about configuring Parse Server [here](#configuration). For a full list of available options, run `parse-server --help`.

**Want logs to be in placed in other folder?** Pass the `PARSE_SERVER_LOGS_FOLDER` environment variable when starting `parse-server`. Usage :- `PARSE_SERVER_LOGS_FOLDER='<path-to-logs-folder>' parse-server --appId APPLICATION_ID --masterKey MASTER_KEY`

### Saving your first object

Now that you're running Parse Server, it is time to save your first object. We'll use the [REST API](https://parse.com/docs/rest/guide), but you can easily do the same using any of the [Parse SDKs](https://parseplatform.github.io/#sdks). Run the following:
Expand Down Expand Up @@ -145,6 +143,20 @@ app.listen(1337, function() {

For a full list of available options, run `parse-server --help`.

## Logging

Parse Server will, by default, will log:
* to the console
* daily rotating files as new line delimited JSON

Logs are also be viewable in Parse Dashboard but it only displays the `messages` field of each log entry. For example, with VERBOSE set this will exclude `origin` on each request.

**Want to log each request and response?** Set the `VERBOSE` environment variable when starting `parse-server`. Usage :- `VERBOSE='1' parse-server --appId APPLICATION_ID --masterKey MASTER_KEY`

**Want logs to be in placed in other folder?** Pass the `PARSE_SERVER_LOGS_FOLDER` environment variable when starting `parse-server`. Usage :- `PARSE_SERVER_LOGS_FOLDER='<path-to-logs-folder>' parse-server --appId APPLICATION_ID --masterKey MASTER_KEY`

**Want each log to be on a single line?** Pass the `SHORT_LOGS` environment variable when starting `parse-server`. Usage :- `SHORT_LOGS='1' parse-server --appId APPLICATION_ID --masterKey MASTER_KEY`

# Documentation

The full documentation for Parse Server is available in the [wiki](https://github.com/ParsePlatform/parse-server/wiki). The [Parse Server guide](https://github.com/ParsePlatform/parse-server/wiki/Parse-Server-Guide) is a good place to get started. If you're interested in developing for Parse Server, the [Development guide](https://github.com/ParsePlatform/parse-server/wiki/Development-Guide) will help you get set up.
Expand Down
6 changes: 3 additions & 3 deletions spec/FileLoggerAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('verbose logs', () => {
level: 'verbose'
});
}).then((results) => {
expect(results[1].message.includes('"password": "********"')).toEqual(true);
expect(results[1].body.password).toEqual("********");
var headers = {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest'
Expand All @@ -77,7 +77,7 @@ describe('verbose logs', () => {
size: 100,
level: 'verbose'
}).then((results) => {
expect(results[1].message.includes('password=********')).toEqual(true);
expect(results[1].url.includes('password=********')).toEqual(true);
done();
});
});
Expand All @@ -95,7 +95,7 @@ describe('verbose logs', () => {
level: 'verbose'
});
}).then((results) => {
expect(results[1].message.includes('"password": "pw"')).toEqual(true);
expect(results[1].body.password).toEqual("pw");
done();
});
});
Expand Down
1 change: 1 addition & 0 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class Config {
}

this.applicationId = applicationId;
this.shortLogs = cacheInfo.shortLogs;
this.masterKey = cacheInfo.masterKey;
this.clientKey = cacheInfo.clientKey;
this.javascriptKey = cacheInfo.javascriptKey;
Expand Down
8 changes: 6 additions & 2 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ addParseCloud();
// and delete
// "loggerAdapter": a class like FileLoggerAdapter providing info, error,
// and query
// "shortLogs": single line log entries
// "databaseURI": a uri like mongodb://localhost:27017/dbname to tell us
// what database this Parse API connects to.
// "cloud": relative location to cloud code to require, or a function
Expand Down Expand Up @@ -90,6 +91,7 @@ class ParseServer {
filesAdapter,
push,
loggerAdapter,
shortLogs,
logsFolder,
databaseURI,
databaseOptions,
Expand Down Expand Up @@ -174,6 +176,7 @@ class ParseServer {
const cacheController = new CacheController(cacheControllerAdapter, appId);

AppCache.put(appId, {
appId,
masterKey: masterKey,
serverURL: serverURL,
collectionPrefix: collectionPrefix,
Expand All @@ -200,6 +203,7 @@ class ParseServer {
liveQueryController: liveQueryController,
sessionLength: Number(sessionLength),
expireInactiveSessions: expireInactiveSessions,
shortLogs,
revokeSessionOnPasswordReset
});

Expand All @@ -217,7 +221,7 @@ class ParseServer {
return ParseServer.app(this.config);
}

static app({maxUploadSize = '20mb'}) {
static app({maxUploadSize = '20mb', appId}) {
// This app serves the Parse API directly.
// It's the equivalent of https://api.parse.com/1 in the hosted Parse API.
var api = express();
Expand Down Expand Up @@ -263,7 +267,7 @@ class ParseServer {
return memo.concat(router.routes);
}, []);

let appRouter = new PromiseRouter(routes);
let appRouter = new PromiseRouter(routes, appId);

batch.mountOnto(appRouter);

Expand Down
51 changes: 36 additions & 15 deletions src/PromiseRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// themselves use our routing information, without disturbing express
// components that external developers may be modifying.

import AppCache from './cache';
import express from 'express';
import url from 'url';
import log from './logger';
Expand All @@ -19,8 +20,9 @@ export default class PromiseRouter {
// status: optional. the http status code. defaults to 200
// response: a json object with the content of the response
// location: optional. a location header
constructor(routes = []) {
constructor(routes = [], appId) {
this.routes = routes;
this.appId = appId;
this.mountRoutes();
}

Expand Down Expand Up @@ -107,16 +109,16 @@ export default class PromiseRouter {
for (var route of this.routes) {
switch(route.method) {
case 'POST':
expressApp.post(route.path, makeExpressHandler(route.handler));
expressApp.post(route.path, makeExpressHandler(this.appId, route.handler));
break;
case 'GET':
expressApp.get(route.path, makeExpressHandler(route.handler));
expressApp.get(route.path, makeExpressHandler(this.appId, route.handler));
break;
case 'PUT':
expressApp.put(route.path, makeExpressHandler(route.handler));
expressApp.put(route.path, makeExpressHandler(this.appId, route.handler));
break;
case 'DELETE':
expressApp.delete(route.path, makeExpressHandler(route.handler));
expressApp.delete(route.path, makeExpressHandler(this.appId, route.handler));
break;
default:
throw 'unexpected code branch';
Expand All @@ -129,16 +131,16 @@ export default class PromiseRouter {
for (var route of this.routes) {
switch(route.method) {
case 'POST':
expressApp.post(route.path, makeExpressHandler(route.handler));
expressApp.post(route.path, makeExpressHandler(this.appId, route.handler));
break;
case 'GET':
expressApp.get(route.path, makeExpressHandler(route.handler));
expressApp.get(route.path, makeExpressHandler(this.appId, route.handler));
break;
case 'PUT':
expressApp.put(route.path, makeExpressHandler(route.handler));
expressApp.put(route.path, makeExpressHandler(this.appId, route.handler));
break;
case 'DELETE':
expressApp.delete(route.path, makeExpressHandler(route.handler));
expressApp.delete(route.path, makeExpressHandler(this.appId, route.handler));
break;
default:
throw 'unexpected code branch';
Expand All @@ -152,17 +154,36 @@ export default class PromiseRouter {
// handler.
// Express handlers should never throw; if a promise handler throws we
// just treat it like it resolved to an error.
function makeExpressHandler(promiseHandler) {
function makeExpressHandler(appId, promiseHandler) {
let config = AppCache.get(appId);
return function(req, res, next) {
var url = maskSensitiveUrl(req);
try {
log.verbose(req.method, maskSensitiveUrl(req), req.headers,
JSON.stringify(maskSensitiveBody(req), null, 2));
var jsonBody;
if (config.shortLogs) {
jsonBody = JSON.stringify(maskSensitiveBody(req));
} else {
jsonBody = JSON.stringify(maskSensitiveBody(req), null, 2);
}
log.verbose(`REQUEST for [${req.method}] ${url}: ${jsonBody}`, {
method: req.method,
url: url,
headers: req.headers,
body: maskSensitiveBody(req)
});
promiseHandler(req).then((result) => {
if (!result.response && !result.location && !result.text) {
log.error('the handler did not include a "response" or a "location" field');
throw 'control should not get here';
}
log.verbose(JSON.stringify(result, null, 2));

var jsonResponse;
if (config.shortLogs) {
jsonResponse = JSON.stringify(result);
} else {
jsonResponse = JSON.stringify(result, null, 2);
}
log.verbose(`RESPONSE from [${req.method}] ${url}: ${jsonResponse}`, {result: result});

var status = result.status || 200;
res.status(status);
Expand All @@ -186,11 +207,11 @@ function makeExpressHandler(promiseHandler) {
}
res.json(result.response);
}, (e) => {
log.verbose('error:', e);
log.error(`Error generating response. ${e}`, {error: e});
next(e);
});
} catch (e) {
log.verbose('exception:', e);
log.error(`Error handling request: ${e}`, {error: e});
next(e);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/cli/cli-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export default {
env: "VERBOSE",
help: "Set the logging to verbose"
},
"shortLogs": {
env: "SHORT_LOGS",
help: "Single line log entries"
},
"revokeSessionOnPasswordReset": {
env: "PARSE_SERVER_REVOKE_SESSION_ON_PASSWORD_RESET",
help: "When a user changes their password, either through the reset password email or while logged in, all sessions are revoked if this is true. Set to false if you don't want to revoke sessions.",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import winston from 'winston';
import ParseServer from './ParseServer';
import logger from './logger';
import S3Adapter from 'parse-server-s3-adapter'
import FileSystemAdapter from 'parse-server-fs-adapter'
import TestUtils from './TestUtils';
Expand All @@ -16,4 +16,4 @@ _ParseServer.createLiveQueryServer = ParseServer.createLiveQueryServer;
let GCSAdapter = useExternal('GCSAdapter', 'parse-server-gcs-adapter');

export default ParseServer;
export { S3Adapter, GCSAdapter, FileSystemAdapter, TestUtils, _ParseServer as ParseServer };
export { S3Adapter, GCSAdapter, FileSystemAdapter, TestUtils, _ParseServer as ParseServer, logger };

0 comments on commit bc961d8

Please sign in to comment.