Skip to content
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

Adds npmlog as default logger #1209

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"mongodb": "~2.1.0",
"multer": "^1.1.0",
"node-gcm": "^0.14.0",
"npmlog": "^2.0.3",
"parse": "^1.8.0",
"parse-server-fs-adapter": "^1.0.0",
"parse-server-gcs-adapter": "^1.0.0",
Expand Down
34 changes: 9 additions & 25 deletions src/LiveQuery/PLog.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
let LogLevel = {
'VERBOSE': 0,
'DEBUG': 1,
'INFO': 2,
'ERROR': 3,
'NONE': 4
}
import log from 'npmlog';

function getCurrentLogLevel() {
if (PLog.logLevel && PLog.logLevel in LogLevel) {
return LogLevel[PLog.logLevel];
}
return LogLevel['ERROR'];
}
let logger = log.newGroup('parse-live-query-server');

function verbose(): void {
if (getCurrentLogLevel() <= LogLevel['VERBOSE']) {
console.log.apply(console, arguments)
}
logger.verbose('parse-live-query-server', ...arguments)
}

function log(): void {
if (getCurrentLogLevel() <= LogLevel['INFO']) {
console.log.apply(console, arguments)
}
function info(): void {
logger.info('parse-live-query-server', ...arguments);
}

function error(): void {
if (getCurrentLogLevel() <= LogLevel['ERROR']) {
console.error.apply(console, arguments)
}
logger.error('parse-live-query-server', ...arguments);
}

let PLog = {
log: log,
log: info,
info: info,
error: error,
verbose: verbose,
logLevel: 'INFO'
logger: logger
};

module.exports = PLog;
2 changes: 1 addition & 1 deletion src/LiveQuery/ParseLiveQueryServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ParseLiveQueryServer {

config = config || {};
// Set LogLevel
PLog.logLevel = config.logLevel || 'INFO';
PLog.logger.level = config.logLevel || 'INFO';

// Store keys, convert obj to map
let keyPairs = config.keyPairs || {};
Expand Down
3 changes: 2 additions & 1 deletion src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var batch = require('./batch'),
Parse = require('parse/node').Parse,
authDataManager = require('./authDataManager');

import log from 'npmlog';
import cache from './cache';
import Config from './Config';
import parseServerPackage from '../package.json';
Expand Down Expand Up @@ -254,7 +255,7 @@ class ParseServer {
if (!process.env.TESTING) {
process.on('uncaughtException', (err) => {
if ( err.code === "EADDRINUSE" ) { // user-friendly message for this common error
console.log(`Unable to listen on port ${err.port}. The port is already in use.`);
log.error('parse-server', `Unable to listen on port ${err.port}. The port is already in use.`);
process.exit(0);
} else {
throw err;
Expand Down
24 changes: 7 additions & 17 deletions src/PromiseRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// components that external developers may be modifying.

import express from 'express';
import log from 'npmlog';

export default class PromiseRouter {
// Each entry should be an object with:
Expand Down Expand Up @@ -146,28 +147,21 @@ export default class PromiseRouter {
}
}

// Global flag. Set this to true to log every request and response.
PromiseRouter.verbose = process.env.VERBOSE || false;

// A helper function to make an express handler out of a a promise
// handler.
// Express handlers should never throw; if a promise handler throws we
// just treat it like it resolved to an error.
function makeExpressHandler(promiseHandler) {
return function(req, res, next) {
try {
if (PromiseRouter.verbose) {
console.log(req.method, req.originalUrl, req.headers,
JSON.stringify(req.body, null, 2));
}
log.verbose('parse-server', req.method, req.originalUrl, req.headers,
JSON.stringify(req.body, null, 2));
promiseHandler(req).then((result) => {
if (!result.response && !result.location && !result.text) {
console.log('BUG: the handler did not include a "response" or a "location" field');
log.error('parse-server', 'the handler did not include a "response" or a "location" field');
throw 'control should not get here';
}
if (PromiseRouter.verbose) {
console.log('response:', JSON.stringify(result, null, 2));
}
log.verbose('parse-server', JSON.stringify(result, null, 2));

var status = result.status || 200;
res.status(status);
Expand All @@ -186,15 +180,11 @@ function makeExpressHandler(promiseHandler) {
}
res.json(result.response);
}, (e) => {
if (PromiseRouter.verbose) {
console.log('error:', e);
}
log.verbose('parse-server error:', e);
next(e);
});
} catch (e) {
if (PromiseRouter.verbose) {
console.log('error:', e);
}
log.verbose('parse-server exception:', e);
next(e);
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/cloud-code/httpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import request from 'request';
import Parse from 'parse/node';
import HTTPResponse from './HTTPResponse';
import querystring from 'querystring';
import log from 'npmlog';

var encodeBody = function({body, headers = {}}) {
if (typeof body !== 'object') {
Expand All @@ -14,13 +15,13 @@ var encodeBody = function({body, headers = {}}) {
if (contentTypeKeys.length == 0) {
// no content type
// As per https://parse.com/docs/cloudcode/guide#cloud-code-advanced-sending-a-post-request the default encoding is supposedly x-www-form-urlencoded

body = querystring.stringify(body);
headers['Content-Type'] = 'application/x-www-form-urlencoded';
} else {
/* istanbul ignore next */
if (contentTypeKeys.length > 1) {
console.error('multiple content-type headers are set.');
log.error('parse-server', 'Parse.Cloud.httpRequest', 'multiple content-type headers are set.');
}
// There maybe many, we'll just take the 1st one
var contentType = contentTypeKeys[0];
Expand Down Expand Up @@ -62,8 +63,8 @@ module.exports = function(options) {
return promise.reject(error);
}
let httpResponse = new HTTPResponse(response);
// Consider <200 && >= 400 as errors

// Consider <200 && >= 400 as errors
if (httpResponse.status < 200 || httpResponse.status >= 400) {
if (callbacks.error) {
callbacks.error(httpResponse);
Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import log from 'npmlog';
import ParseServer from './ParseServer';
import { GCSAdapter } from 'parse-server-gcs-adapter';
import { S3Adapter } from 'parse-server-s3-adapter';
import { FileSystemAdapter } from 'parse-server-fs-adapter';

if (process.env.VERBOSE || process.env.VERBOSE_PARSE_SERVER) {
log.level = 'VERBOSE';
}

if (process.env.DEBUG || process.env.DEBUG_PARSE_SERVER) {
log.level = 'DEBUG';
}

// Factory function
let _ParseServer = function(options) {
let server = new ParseServer(options);
Expand Down
5 changes: 3 additions & 2 deletions src/middlewares.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cache from './cache';
import log from 'npmlog';

var Parse = require('parse/node').Parse;

Expand Down Expand Up @@ -128,7 +129,7 @@ function handleParseHeaders(req, res, next) {
})
.catch((error) => {
// TODO: Determine the correct error scenario.
console.log(error);
log.error('parse-server', 'error getting auth for sessionToken', error);
throw new Parse.Error(Parse.Error.UNKNOWN_ERROR, error);
});
}
Expand Down Expand Up @@ -178,7 +179,7 @@ var handleParseErrors = function(err, req, res, next) {
res.status(err.status);
res.json({error: err.message});
} else {
console.log('Uncaught internal server error.', err, err.stack);
log.error('parse-server', 'Uncaught internal server error.', err, err.stack);
res.status(500);
res.json({code: Parse.Error.INTERNAL_SERVER_ERROR,
message: 'Internal server error.'});
Expand Down
5 changes: 3 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import log from 'npmlog';
var mongodb = require('mongodb');
var Parse = require('parse/node').Parse;

Expand Down Expand Up @@ -691,13 +692,13 @@ function untransformObject(schema, className, mongoObject, isNestedObject = fals
expected = schema.getExpectedType(className, newKey);
}
if (!expected) {
console.log(
log.info('parse-server', 'transform.js',
'Found a pointer column not in the schema, dropping it.',
className, newKey);
break;
}
if (expected && expected[0] != '*') {
console.log('Found a pointer in a non-pointer column, dropping it.', className, key);
log.info('parse-server', 'transform.js', 'Found a pointer in a non-pointer column, dropping it.', className, key);
break;
}
if (mongoObject[key] === null) {
Expand Down