Skip to content

Commit 7ef27f0

Browse files
authored
Improved logs (#44)
* new logs * more changes * error fields * holly molly
1 parent 5236fe8 commit 7ef27f0

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

lib/countly-common.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,23 @@ var cc = {
209209
isResponseValid: function isResponseValid(statusCode, str) {
210210
// status code and response format check
211211
if (!(statusCode >= 200 && statusCode < 300)) {
212+
this.log(this.logLevelEnums.ERROR, `isResponseValid, The server status code is not within the expected range: [${statusCode}]`);
212213
return false;
213214
}
214215

215216
// Try to parse JSON
216217
try {
217-
return !!JSON.parse(str).result;
218+
var response = JSON.parse(str);
219+
if (response.result) {
220+
// if the 'result' field exists, we return 'true'
221+
return true;
222+
}
223+
// result field was not there
224+
this.log(this.logLevelEnums.ERROR, `isResponseValid, The server response has no 'result' field`);
225+
return false;
218226
}
219227
catch (e) {
220-
this.log("Http response is in the wrong format.", e);
228+
this.log(this.logLevelEnums.ERROR, `isResponseValid, Http response is in the wrong format: [${e}]`);
221229
return false;
222230
}
223231
},

lib/countly.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,22 +180,21 @@ Countly.Bulk = Bulk;
180180
}
181181
}
182182
catch (ex) {
183-
// problem creating dir
184-
// eslint-disable-next-line no-console
185-
console.log(ex.stack);
183+
// problem creating data dir
184+
cc.log(cc.logLevelEnums.ERROR, `init, Failed to create the data directory:${ex}`);
186185
}
187186
// clear stored device ID if flag is set
188187
if (conf.clear_stored_device_id) {
189-
cc.log(cc.logLevelEnums.WARNING, "Erasing stored ID");
188+
cc.log(cc.logLevelEnums.WARNING, "init, Erasing stored ID");
190189
storeSet("cly_id", null);
191190
storeSet("cly_id_type", null);
192191
}
193192

194193
if (Countly.url === "") {
195-
cc.log(cc.logLevelEnums.ERROR, "Please provide server URL");
194+
cc.log(cc.logLevelEnums.ERROR, "init, Please provide server URL");
196195
}
197196
else {
198-
cc.log(cc.logLevelEnums.INFO, "Countly initialized");
197+
cc.log(cc.logLevelEnums.INFO, "init, Countly initialized");
199198
if (cluster.isMaster) {
200199
// fetch stored ID and ID type
201200
var storedId = storeGet("cly_id", null);
@@ -1443,7 +1442,7 @@ Countly.Bulk = Bulk;
14431442
if (err) {
14441443
requestQueue.unshift(res);
14451444
failTimeout = cc.getTimestamp() + failTimeoutAmount;
1446-
cc.log(cc.logLevelEnums.ERROR, "makeRequest, Encountered a problem while making the request.");
1445+
cc.log(cc.logLevelEnums.ERROR, `makeRequest, Encountered a problem while making the request: [${err}]`);
14471446
}
14481447
storeSet("cly_queue", requestQueue);
14491448
readyToProcess = true;
@@ -1607,6 +1606,7 @@ Countly.Bulk = Bulk;
16071606
str += chunk;
16081607
});
16091608
res.on("end", () => {
1609+
cc.log(cc.logLevelEnums.DEBUG, `makeRequest, Response status code: [${res.statusCode}], response: [${str}]`);
16101610
// checks result field, JSON format and status code
16111611
if (cc.isResponseValid(res.statusCode, str)) {
16121612
callback(false, params, str);
@@ -1720,8 +1720,7 @@ Countly.Bulk = Bulk;
17201720
}
17211721
catch (ex) {
17221722
// problem parsing, corrupted file?
1723-
// eslint-disable-next-line no-console
1724-
console.log(ex.stack);
1723+
cc.log(cc.logLevelEnums.ERROR, `readFile, Failed to parse the file: [${ex}]`);
17251724
// backup corrupted file data
17261725
fs.writeFile(path.resolve(__dirname, `${Countly.storage_path}__${key}.${cc.getTimestamp()}${Math.random()}.json`), data, () => {});
17271726
// start with new clean object
@@ -1743,8 +1742,7 @@ Countly.Bulk = Bulk;
17431742
}
17441743
catch (ex) {
17451744
// tried to save whats possible
1746-
// eslint-disable-next-line no-console
1747-
console.log(ex.stack);
1745+
cc.log(cc.logLevelEnums.ERROR, `forceStore, Saving files failed: [${ex}]`);
17481746
}
17491747
}
17501748
};
@@ -1764,8 +1762,7 @@ Countly.Bulk = Bulk;
17641762
var dir = path.resolve(__dirname, `${Countly.storage_path}__${key}.json`);
17651763
fs.writeFile(dir, JSON.stringify(ob), (err) => {
17661764
if (err) {
1767-
// eslint-disable-next-line no-console
1768-
console.log(err);
1765+
cc.log(cc.logLevelEnums.ERROR, `writeFile, Writing files failed: [${err}]`);
17691766
}
17701767
if (typeof callback === "function") {
17711768
callback(err);

0 commit comments

Comments
 (0)