Skip to content

Commit

Permalink
Merge pull request #5 from parse-server-modules/logger
Browse files Browse the repository at this point in the history
Adds verbose logging
  • Loading branch information
flovilmart committed Mar 26, 2016
2 parents 718c445 + e806e8b commit d8fc510
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,18 @@ Status](https://travis-ci.org/parse-server-modules/parse-server-push-adapter.svg
Official Push adapter for parse-server

See [parse-server push configuration](https://github.com/ParsePlatform/parse-server/wiki/Push)


### see more logs

You can enable verbose logging with environment variables:

```
VERBOSE=1
or
VERBOSE_PARSE_SERVER_PUSH_ADAPTER=1
```

This will produce a more verbose output for all the push sending attempts
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"scripts": {
"build": "./node_modules/.bin/babel src/ -d lib/",
"test": "TESTING=1 babel-node ./node_modules/.bin/isparta cover -x **/spec/** ./node_modules/jasmine/bin/jasmine.js",
"test": "TESTING=1 babel-node ./node_modules/.bin/isparta cover --root src/ ./node_modules/jasmine/bin/jasmine.js",
"prepublish": "npm run build"
},
"keywords": [
Expand All @@ -34,6 +34,7 @@
"dependencies": {
"apn": "^1.7.5",
"node-gcm": "^0.14.0",
"npmlog": "^2.0.3",
"parse": "^1.8.1"
}
}
19 changes: 12 additions & 7 deletions src/APNS.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"use strict";

const Parse = require('parse/node').Parse;
// TODO: apn does not support the new HTTP/2 protocal. It is fine to use it in V1,
// but probably we will replace it in the future.
const apn = require('apn');
import apn from 'apn';
import Parse from 'parse';
import log from 'npmlog';

const LOG_PREFIX = 'parse-server-push-adapter APNS';

/**
* Create a new connection to the APN service.
Expand Down Expand Up @@ -46,23 +49,23 @@ function APNS(args) {

// Set apns client callbacks
conn.on('connected', () => {
console.log('APNS Connection %d Connected', conn.index);
log.verbose(LOG_PREFIX, 'APNS Connection %d Connected', conn.index);
});

conn.on('transmissionError', (errCode, notification, apnDevice) => {
handleTransmissionError(this.conns, errCode, notification, apnDevice);
});

conn.on('timeout', () => {
console.log('APNS Connection %d Timeout', conn.index);
log.verbose(LOG_PREFIX, 'APNS Connection %d Timeout', conn.index);
});

conn.on('disconnected', () => {
console.log('APNS Connection %d Disconnected', conn.index);
log.verbose(LOG_PREFIX, 'APNS Connection %d Disconnected', conn.index);
});

conn.on('socketError', () => {
console.log('APNS Connection %d Socket Error', conn.index);
log.verbose(LOG_PREFIX, 'APNS Connection %d Socket Error', conn.index);
});

conn.on('transmitted', function(notification, device) {
Expand All @@ -76,7 +79,7 @@ function APNS(args) {
}
});
}
console.log('APNS Connection %d Notification transmitted to %s', conn.index, device.token.toString('hex'));
log.verbose(LOG_PREFIX, 'APNS Connection %d Notification transmitted to %s', conn.index, device.token.toString('hex'));
});

this.conns.push(conn);
Expand Down Expand Up @@ -106,6 +109,7 @@ APNS.prototype.send = function(data, devices) {
let qualifiedConnIndexs = chooseConns(this.conns, device);
// We can not find a valid conn, just ignore this device
if (qualifiedConnIndexs.length == 0) {
log.error(LOG_PREFIX, 'no qualified connections for %s %s', device.appIdentifier, device.deviceToken);
return Promise.resolve({
transmitted: false,
device: {
Expand Down Expand Up @@ -154,6 +158,7 @@ function handleTransmissionError(conns, errCode, notification, apnDevice) {
// There is no more available conns, we give up in this case
if (newConnIndex < 0 || newConnIndex >= conns.length) {
if (apnDevice.callback) {
log.error(LOG_PREFIX, `cannot find vaild connection for ${apnDevice.token.toString('hex')}`);
apnDevice.callback({
response: {error: `APNS can not find vaild connection for ${apnDevice.token.toString('hex')}`, code: errCode},
status: errCode,
Expand Down
12 changes: 11 additions & 1 deletion src/GCM.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use strict";

import Parse from 'parse/node';
import Parse from 'parse';
import log from 'npmlog';
import gcm from 'node-gcm';
import { randomString } from './PushAdapterUtils';

const LOG_PREFIX = 'parse-server-push-adapter GCM';
const GCMTimeToLiveMax = 4 * 7 * 24 * 60 * 60; // GCM allows a max of 4 weeks
const GCMRegistrationTokensMax = 1000;

Expand All @@ -30,6 +32,7 @@ GCM.prototype.send = function(data, devices) {
// chunk if necessary
let slices = sliceDevices(devices, GCMRegistrationTokensMax);
if (slices.length > 1) {
log.verbose(LOG_PREFIX, `the number of devices exceeds ${GCMRegistrationTokensMax}`);
// Make 1 send per slice
let promises = slices.reduce((memo, slice) => {
let promise = this.send(data, slice, timestamp);
Expand Down Expand Up @@ -68,6 +71,8 @@ GCM.prototype.send = function(data, devices) {

let promises = deviceTokens.map(() => new Parse.Promise());
let registrationTokens = deviceTokens;
let length = registrationTokens.length;
log.verbose(LOG_PREFIX, `sending to ${length} ${length > 1 ? 'devices' : 'device'}`);
this.sender.send(message, { registrationTokens: registrationTokens }, 5, (error, response) => {
// example response:
/*
Expand All @@ -80,6 +85,11 @@ GCM.prototype.send = function(data, devices) {
{"error":"InvalidRegistration"},
{"error":"InvalidRegistration"}] }
*/
if (error) {
log.error(LOG_PREFIX, `send errored: %s`, JSON.stringify(error, null, 4));
} else {
log.verbose(LOG_PREFIX, `GCM Response: %s`, JSON.stringify(response, null, 4));
}
let { results, multicast_id } = response || {};
registrationTokens.forEach((token, index) => {
let promise = promises[index];
Expand Down
12 changes: 8 additions & 4 deletions src/ParsePushAdapter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const Parse = require('parse/node').Parse;
const GCM = require('./GCM');
const APNS = require('./APNS');

'use strict';
import Parse from 'parse';
import log from 'npmlog';
import APNS from './APNS';
import GCM from './GCM';
import { classifyInstallations } from './PushAdapterUtils';

const LOG_PREFIX = 'parse-server-push-adapter';

export class ParsePushAdapter {

supportsPushTracking = true;
Expand Down Expand Up @@ -48,6 +51,7 @@ export class ParsePushAdapter {
let sender = this.senderMap[pushType];
let devices = deviceMap[pushType];
if (!sender) {
log.verbose(LOG_PREFIX, `Can not find sender for push type ${pushType}, ${data}`)
let results = devices.map((device) => {
return Promise.resolve({
device,
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// ParsePushAdapter is the default implementation of
// PushAdapter, it uses GCM for android push and APNS
// for ios push.
import log from 'npmlog';

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

import ParsePushAdapter from './ParsePushAdapter';
import GCM from './GCM';
Expand Down

0 comments on commit d8fc510

Please sign in to comment.