Skip to content

Commit d8fc510

Browse files
committed
Merge pull request #5 from parse-server-modules/logger
Adds verbose logging
2 parents 718c445 + e806e8b commit d8fc510

File tree

6 files changed

+53
-13
lines changed

6 files changed

+53
-13
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,18 @@ Status](https://travis-ci.org/parse-server-modules/parse-server-push-adapter.svg
88
Official Push adapter for parse-server
99

1010
See [parse-server push configuration](https://github.com/ParsePlatform/parse-server/wiki/Push)
11+
12+
13+
### see more logs
14+
15+
You can enable verbose logging with environment variables:
16+
17+
```
18+
VERBOSE=1
19+
20+
or
21+
22+
VERBOSE_PARSE_SERVER_PUSH_ADAPTER=1
23+
```
24+
25+
This will produce a more verbose output for all the push sending attempts

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
],
99
"scripts": {
1010
"build": "./node_modules/.bin/babel src/ -d lib/",
11-
"test": "TESTING=1 babel-node ./node_modules/.bin/isparta cover -x **/spec/** ./node_modules/jasmine/bin/jasmine.js",
11+
"test": "TESTING=1 babel-node ./node_modules/.bin/isparta cover --root src/ ./node_modules/jasmine/bin/jasmine.js",
1212
"prepublish": "npm run build"
1313
},
1414
"keywords": [
@@ -34,6 +34,7 @@
3434
"dependencies": {
3535
"apn": "^1.7.5",
3636
"node-gcm": "^0.14.0",
37+
"npmlog": "^2.0.3",
3738
"parse": "^1.8.1"
3839
}
3940
}

src/APNS.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"use strict";
22

3-
const Parse = require('parse/node').Parse;
43
// TODO: apn does not support the new HTTP/2 protocal. It is fine to use it in V1,
54
// but probably we will replace it in the future.
6-
const apn = require('apn');
5+
import apn from 'apn';
6+
import Parse from 'parse';
7+
import log from 'npmlog';
8+
9+
const LOG_PREFIX = 'parse-server-push-adapter APNS';
710

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

4750
// Set apns client callbacks
4851
conn.on('connected', () => {
49-
console.log('APNS Connection %d Connected', conn.index);
52+
log.verbose(LOG_PREFIX, 'APNS Connection %d Connected', conn.index);
5053
});
5154

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

5659
conn.on('timeout', () => {
57-
console.log('APNS Connection %d Timeout', conn.index);
60+
log.verbose(LOG_PREFIX, 'APNS Connection %d Timeout', conn.index);
5861
});
5962

6063
conn.on('disconnected', () => {
61-
console.log('APNS Connection %d Disconnected', conn.index);
64+
log.verbose(LOG_PREFIX, 'APNS Connection %d Disconnected', conn.index);
6265
});
6366

6467
conn.on('socketError', () => {
65-
console.log('APNS Connection %d Socket Error', conn.index);
68+
log.verbose(LOG_PREFIX, 'APNS Connection %d Socket Error', conn.index);
6669
});
6770

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

8285
this.conns.push(conn);
@@ -106,6 +109,7 @@ APNS.prototype.send = function(data, devices) {
106109
let qualifiedConnIndexs = chooseConns(this.conns, device);
107110
// We can not find a valid conn, just ignore this device
108111
if (qualifiedConnIndexs.length == 0) {
112+
log.error(LOG_PREFIX, 'no qualified connections for %s %s', device.appIdentifier, device.deviceToken);
109113
return Promise.resolve({
110114
transmitted: false,
111115
device: {
@@ -154,6 +158,7 @@ function handleTransmissionError(conns, errCode, notification, apnDevice) {
154158
// There is no more available conns, we give up in this case
155159
if (newConnIndex < 0 || newConnIndex >= conns.length) {
156160
if (apnDevice.callback) {
161+
log.error(LOG_PREFIX, `cannot find vaild connection for ${apnDevice.token.toString('hex')}`);
157162
apnDevice.callback({
158163
response: {error: `APNS can not find vaild connection for ${apnDevice.token.toString('hex')}`, code: errCode},
159164
status: errCode,

src/GCM.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"use strict";
22

3-
import Parse from 'parse/node';
3+
import Parse from 'parse';
4+
import log from 'npmlog';
45
import gcm from 'node-gcm';
56
import { randomString } from './PushAdapterUtils';
67

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

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

6972
let promises = deviceTokens.map(() => new Parse.Promise());
7073
let registrationTokens = deviceTokens;
74+
let length = registrationTokens.length;
75+
log.verbose(LOG_PREFIX, `sending to ${length} ${length > 1 ? 'devices' : 'device'}`);
7176
this.sender.send(message, { registrationTokens: registrationTokens }, 5, (error, response) => {
7277
// example response:
7378
/*
@@ -80,6 +85,11 @@ GCM.prototype.send = function(data, devices) {
8085
{"error":"InvalidRegistration"},
8186
{"error":"InvalidRegistration"}] }
8287
*/
88+
if (error) {
89+
log.error(LOG_PREFIX, `send errored: %s`, JSON.stringify(error, null, 4));
90+
} else {
91+
log.verbose(LOG_PREFIX, `GCM Response: %s`, JSON.stringify(response, null, 4));
92+
}
8393
let { results, multicast_id } = response || {};
8494
registrationTokens.forEach((token, index) => {
8595
let promise = promises[index];

src/ParsePushAdapter.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
const Parse = require('parse/node').Parse;
2-
const GCM = require('./GCM');
3-
const APNS = require('./APNS');
4-
1+
'use strict';
2+
import Parse from 'parse';
3+
import log from 'npmlog';
4+
import APNS from './APNS';
5+
import GCM from './GCM';
56
import { classifyInstallations } from './PushAdapterUtils';
67

8+
const LOG_PREFIX = 'parse-server-push-adapter';
9+
710
export class ParsePushAdapter {
811

912
supportsPushTracking = true;
@@ -48,6 +51,7 @@ export class ParsePushAdapter {
4851
let sender = this.senderMap[pushType];
4952
let devices = deviceMap[pushType];
5053
if (!sender) {
54+
log.verbose(LOG_PREFIX, `Can not find sender for push type ${pushType}, ${data}`)
5155
let results = devices.map((device) => {
5256
return Promise.resolve({
5357
device,

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// ParsePushAdapter is the default implementation of
33
// PushAdapter, it uses GCM for android push and APNS
44
// for ios push.
5+
import log from 'npmlog';
6+
7+
if (process.env.VERBOSE || process.env.VERBOSE_PARSE_SERVER_PUSH_ADAPTER) {
8+
log.level = 'verbose';
9+
}
510

611
import ParsePushAdapter from './ParsePushAdapter';
712
import GCM from './GCM';

0 commit comments

Comments
 (0)