Skip to content

Commit c57f79e

Browse files
authored
revamped thee logs (#64)
* revamped thee logs * init time logs
1 parent e9557fc commit c57f79e

3 files changed

Lines changed: 160 additions & 116 deletions

File tree

lib/countly-bulk-user.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ function CountlyBulkUser(conf) {
7070
cc.debugBulkUser = conf.debug;
7171

7272
if (!conf.device_id) {
73-
cc.log(cc.logLevelEnums.ERROR, "device_id is missing");
73+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulkUser, 'device_id' is missing.");
7474
return;
7575
}
7676

7777
if (!conf.server) {
78-
cc.log(cc.logLevelEnums.ERROR, "server instance is missing");
78+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulkUser, 'server' instance is missing.");
7979
return;
8080
}
8181

@@ -112,16 +112,16 @@ function CountlyBulkUser(conf) {
112112
consents[i] = { features: groupedFeatures[i] };
113113
}
114114
else {
115-
cc.log(cc.logLevelEnums.WARNING, `group_features, Incorrect feature list for ${i} value: ${groupedFeatures[i]}`);
115+
cc.log(cc.logLevelEnums.WARNING, `CountlyBulkUser group_features, Incorrect feature list for [${i}] value: [${groupedFeatures[i]}].`);
116116
}
117117
}
118118
else {
119-
cc.log(cc.logLevelEnums.WARNING, `group_features, Feature name ${i} is already reserved`);
119+
cc.log(cc.logLevelEnums.WARNING, `CountlyBulkUser group_features, Feature name [${i}] is already reserved.`);
120120
}
121121
}
122122
}
123123
else {
124-
cc.log(cc.logLevelEnums.WARNING, `group_features, Incorrect features: ${groupedFeatures}`);
124+
cc.log(cc.logLevelEnums.ERROR, `CountlyBulkUser group_features, Incorrect features: [${groupedFeatures}].`);
125125
}
126126
};
127127

@@ -133,19 +133,19 @@ function CountlyBulkUser(conf) {
133133
this.check_consent = function(feature) {
134134
if (!conf.require_consent) {
135135
// we don't need to have specific consents
136-
cc.log(cc.logLevelEnums.INFO, `check_consent, Require consent is off. Giving consent for : [ ${feature} ] feature.`);
136+
cc.log(cc.logLevelEnums.INFO, `CountlyBulkUser check_consent, Require consent is off. Giving consent for: [${feature}] feature.`);
137137
return true;
138138
}
139139
if (consents[feature] && consents[feature].optin) {
140-
cc.log(cc.logLevelEnums.INFO, `check_consent, Giving consent for : [ ${feature} ] feature.`);
140+
cc.log(cc.logLevelEnums.INFO, `CountlyBulkUser check_consent, Giving consent for: [${feature}] feature.`);
141141
return true;
142142
}
143143
if (consents[feature] && !consents[feature].optin) {
144-
cc.log(cc.logLevelEnums.ERROR, `check_consent, User is not optin. Consent refused for : [ ${feature} ] feature.`);
144+
cc.log(cc.logLevelEnums.ERROR, `CountlyBulkUser check_consent, User is not optin. Consent refused for: [${feature}] feature.`);
145145
return false;
146146
}
147147

148-
cc.log(cc.logLevelEnums.WARNING, `check_consent, No feature available for ${feature}`);
148+
cc.log(cc.logLevelEnums.ERROR, `CountlyBulkUser check_consent, No feature available for: [${feature}].`);
149149

150150
return false;
151151
};
@@ -155,7 +155,7 @@ function CountlyBulkUser(conf) {
155155
* @param {string|array} feature - name of the feature, possible values, "sessions","events","views","crashes","attribution","users" or customly provided through {@link CountlyBulkUser.group_features}
156156
*/
157157
this.add_consent = function(feature) {
158-
cc.log(cc.logLevelEnums.INFO, `add_consent, Adding consent for ${feature}`);
158+
cc.log(cc.logLevelEnums.INFO, `CountlyBulkUser add_consent, Adding consent for: [${feature}].`);
159159
if (feature.constructor === Array) {
160160
for (var i = 0; i < feature.length; i++) {
161161
this.add_consent(feature[i]);
@@ -186,7 +186,7 @@ function CountlyBulkUser(conf) {
186186
}
187187
}
188188
else {
189-
cc.log(cc.logLevelEnums.WARNING, `add_consent, No feature available for ${feature}`);
189+
cc.log(cc.logLevelEnums.ERROR, `CountlyBulkUser add_consent, No feature available for: [${feature}].`);
190190
}
191191
};
192192

@@ -195,7 +195,7 @@ function CountlyBulkUser(conf) {
195195
* @param {string|array} feature - name of the feature, possible values, "sessions","events","views","crashes","attribution","users" or custom provided through {@link CountlyBulkUser.group_features}
196196
*/
197197
this.remove_consent = function(feature) {
198-
cc.log(cc.logLevelEnums.INFO, `remove_consent, Removing consent for ${feature}`);
198+
cc.log(cc.logLevelEnums.INFO, `CountlyBulkUser remove_consent, Removing consent for: [${feature}].`);
199199
this.remove_consent_internal(feature, true);
200200
};
201201

@@ -224,10 +224,9 @@ function CountlyBulkUser(conf) {
224224
updateConsent();
225225
}
226226
}
227-
cc.log(cc.logLevelEnums.DEBUG, "remove_consent, Consent removal triggered.");
228227
}
229228
else {
230-
cc.log(cc.logLevelEnums.WARNING, `remove_consent, No feature available for ${feature}`);
229+
cc.log(cc.logLevelEnums.ERROR, `CountlyBulkUser remove_consent, No feature available to remove for: [${feature}].`);
231230
}
232231
};
233232

@@ -249,7 +248,7 @@ function CountlyBulkUser(conf) {
249248
}
250249
}
251250
conf.server.add_bulk_request({ consent: JSON.stringify(consentMessage) });
252-
cc.log(cc.logLevelEnums.DEBUG, "Consent update request has been sent to the queue.");
251+
cc.log(cc.logLevelEnums.DEBUG, "CountlyBulkUser updateConsent, Consent update request has been sent to the queue.");
253252
}, 1000);
254253
};
255254

@@ -331,7 +330,7 @@ function CountlyBulkUser(conf) {
331330
* @returns {module:lib/countly-bulk-user} instance
332331
* */
333332
this.add_event = function(event) {
334-
cc.log(`Trying to add the event: [ ${event.key} ]`);
333+
cc.log(cc.logLevelEnums.INFO, `CountlyBulkUser add_event, Trying to add the event with key: [${event.key}].`);
335334
// initially no consent is given
336335
var respectiveConsent = false;
337336
// to match the internal events and their respective required consents. Sets respectiveConsent to true if the consent is given
@@ -496,11 +495,11 @@ function CountlyBulkUser(conf) {
496495
this.report_feedback = function(feedback, timestamp) {
497496
if (this.check_consent("star-rating") || this.check_consent("feedback")) {
498497
if (!feedback.widget_id) {
499-
cc.log(cc.logLevelEnums.ERROR, "report_feedback, Feedback must contain widget_id property");
498+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulkUser report_feedback, Feedback must contain 'widget_id' property.");
500499
return;
501500
}
502501
if (!feedback.rating) {
503-
cc.log(cc.logLevelEnums.ERROR, "report_feedback, Feedback must contain rating property");
502+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulkUser report_feedback, Feedback must contain 'rating' property.");
504503
return;
505504
}
506505
if (this.check_consent("events")) {
@@ -514,7 +513,7 @@ function CountlyBulkUser(conf) {
514513
event.timestamp = timestamp;
515514
}
516515
event.segmentation = cc.getProperties(feedback, props);
517-
cc.log(cc.logLevelEnums.INFO, "report_feedback, Reporting feedback: ", event);
516+
cc.log(cc.logLevelEnums.INFO, `CountlyBulkUser report_feedback, Reporting feedback for the widget with ID: [${feedback.widget_id}].`);
518517
this.add_event(event);
519518
}
520519
}
@@ -539,7 +538,7 @@ function CountlyBulkUser(conf) {
539538
var props = ["type", "name", "stz", "etz", "apm_metrics", "apm_attr"];
540539
for (var i = 0; i < props.length; i++) {
541540
if (props[i] !== "apm_attr" && typeof trace[props[i]] === "undefined") {
542-
cc.log(cc.logLevelEnums.ERROR, "report_trace, APM trace must have a", props[i]);
541+
cc.log(cc.logLevelEnums.ERROR, `CountlyBulkUser report_trace, APM trace must have a: [${props[i]}] param.`);
543542
return;
544543
}
545544
}
@@ -551,7 +550,7 @@ function CountlyBulkUser(conf) {
551550
e.dow = date.getDay();
552551
var query = prepareQuery({ apm: JSON.stringify(e) });
553552

554-
cc.log(cc.logLevelEnums.INFO, "report_trace, Adding APM trace: ", e);
553+
cc.log(cc.logLevelEnums.INFO, "CountlyBulkUser report_trace, Adding APM trace to the queue.");
555554

556555
conf.server.add_request(query);
557556
}
@@ -773,6 +772,7 @@ function CountlyBulkUser(conf) {
773772
if (this.check_consent("users")) {
774773
var query = prepareQuery({ user_details: { custom: customData } });
775774
conf.server.add_request(query);
775+
cc.log(cc.logLevelEnums.DEBUG, "CountlyBulkUser custom_save, Adding the request to the queue.");
776776
}
777777
customData = {};
778778
return this;

lib/countly-bulk.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ function CountlyBulk(conf) {
7777

7878
cc.debugBulk = conf.debug || false;
7979
if (!conf.app_key) {
80-
cc.log(cc.logLevelEnums.ERROR, "app_key is missing");
80+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk, 'app_key' is missing.");
8181
return;
8282
}
8383

8484
if (!conf.url) {
85-
cc.log(cc.logLevelEnums.ERROR, "url is missing");
85+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk, 'url' is missing.");
8686
return;
8787
}
8888

@@ -115,7 +115,7 @@ function CountlyBulk(conf) {
115115
catch (ex) {
116116
// problem creating directory
117117
// eslint-disable-next-line no-console
118-
console.log(ex.stack);
118+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk, Failed white creating the '/data' directory. Error: ", ex.stack);
119119
}
120120
}
121121

@@ -136,7 +136,7 @@ function CountlyBulk(conf) {
136136
query = cc.truncateObject(query, self.maxKeyLength, self.maxValueSize, self.maxSegmentationValues, "add_request", self.debug);
137137
if (cluster.isMaster) {
138138
if (!query.device_id) {
139-
cc.log(cc.logLevelEnums.ERROR, "add_request, device_id is missing", query);
139+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk add_request, 'device_id' is missing.");
140140
return;
141141
}
142142

@@ -145,7 +145,7 @@ function CountlyBulk(conf) {
145145
}
146146
if (query.timestamp) {
147147
if ((`${query.timestamp}`).length !== 13 && (`${query.timestamp}`).length !== 10) {
148-
cc.log(cc.logLevelEnums.WARNING, "add_request, incorrect timestamp format, must be in milliseconds or seconds", query);
148+
cc.log(cc.logLevelEnums.WARNING, "CountlyBulk add_request, incorrect timestamp format, must be in milliseconds or seconds.");
149149
}
150150
}
151151
query.sdk_name = SDK_NAME;
@@ -156,10 +156,11 @@ function CountlyBulk(conf) {
156156
query.dow = query.dow || date.getDay();
157157

158158
requestQueue.push(query);
159-
cc.log(cc.logLevelEnums.INFO, "add_request, adding request", query);
159+
cc.log(cc.logLevelEnums.INFO, "CountlyBulk add_request, Adding request to the queue.");
160160
storeSet("cly_req_queue", requestQueue);
161161
}
162162
else {
163+
cc.log(cc.logLevelEnums.INFO, "CountlyBulk add_request, Sending message to the parent process. Adding the raw request to the queue.");
163164
process.send({ cly_bulk: { cly_queue: query } });
164165
}
165166
return this;
@@ -183,7 +184,7 @@ function CountlyBulk(conf) {
183184
for (var i in requests) {
184185
query = requests[i];
185186
if (!query.device_id) {
186-
cc.log(cc.logLevelEnums.ERROR, "add_bulk_request, device_id is missing", query);
187+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk add_bulk_request, 'device_id' is missing.");
187188
return;
188189
}
189190

@@ -192,7 +193,7 @@ function CountlyBulk(conf) {
192193
}
193194

194195
if ((`${query.timestamp}`).length !== 13 && (`${query.timestamp}`).length !== 10) {
195-
cc.log(cc.logLevelEnums.ERROR, "add_bulk_request, incorrect timestamp format", query);
196+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk add_bulk_request, incorrect timestamp format.");
196197
}
197198

198199
query.sdk_name = SDK_NAME;
@@ -201,12 +202,13 @@ function CountlyBulk(conf) {
201202
var date = new Date((`${query.timestamp}`).length === 13 ? query.timestamp : parseInt(query.timestamp) * 1000);
202203
query.hour = query.hour || date.getHours();
203204
query.dow = query.dow || date.getDay();
204-
cc.log(cc.logLevelEnums.INFO, "add_bulk_request, adding bulk request", query);
205+
cc.log(cc.logLevelEnums.INFO, "CountlyBulk add_bulk_request, adding the request into queue.");
205206
requestQueue.push(query);
206207
}
207208
storeSet("cly_req_queue", requestQueue);
208209
}
209210
else {
211+
cc.log(cc.logLevelEnums.INFO, "CountlyBulk add_bulk_request, Sending message to the parent process. Adding the raw request to the queue.");
210212
process.send({ cly_bulk: { cly_bulk_queue: requests } });
211213
}
212214
return this;
@@ -233,12 +235,12 @@ function CountlyBulk(conf) {
233235
* */
234236
this.add_event = function(device_id, event) {
235237
if (!device_id) {
236-
cc.log(cc.logLevelEnums.ERROR, "add_event, device_id is missing");
238+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk add_event, 'device_id' is missing.");
237239
return;
238240
}
239241

240242
if (!event.key) {
241-
cc.log(cc.logLevelEnums.ERROR, "add_event, Event must have key property");
243+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk add_event, Event must have 'key' property.");
242244
return;
243245
}
244246
if (cluster.isMaster) {
@@ -253,14 +255,15 @@ function CountlyBulk(conf) {
253255
var date = new Date((`${e.timestamp}`).length === 13 ? e.timestamp : parseInt(e.timestamp) * 1000);
254256
e.hour = date.getHours();
255257
e.dow = date.getDay();
256-
cc.log(cc.logLevelEnums.INFO, "add_event, Adding event: ", event);
258+
cc.log(cc.logLevelEnums.INFO, `CountlyBulk add_event, Adding event: [${event.key}].`);
257259
if (!eventQueue[device_id]) {
258260
eventQueue[device_id] = [];
259261
}
260262
eventQueue[device_id].push(e);
261263
storeSet("cly_bulk_event", eventQueue);
262264
}
263265
else {
266+
cc.log(cc.logLevelEnums.INFO, `CountlyBulk add_event, Sending message to the parent process. Adding event: [${event.key}].`);
264267
process.send({ cly_bulk: { device_id: device_id, event: event } });
265268
}
266269
return this;
@@ -281,6 +284,7 @@ function CountlyBulk(conf) {
281284
this.start = function(callback) {
282285
if (cluster.isMaster) {
283286
if (!initiated) {
287+
cc.log(cc.logLevelEnums.INFO, "CountlyBulk start, starting to process the requests.");
284288
empty_queue_callback = callback;
285289
initiated = true;
286290
heartBeat();
@@ -301,6 +305,7 @@ function CountlyBulk(conf) {
301305
* server.stop();
302306
* */
303307
this.stop = function() {
308+
cc.log(cc.logLevelEnums.INFO, "CountlyBulk stop, stopping to process the requests.");
304309
initiated = false;
305310
return this;
306311
};
@@ -401,9 +406,9 @@ function CountlyBulk(conf) {
401406
isEmpty = false;
402407
readyToProcess = false;
403408
var params = bulkQueue.shift();
404-
cc.log(cc.logLevelEnums.DEBUG, "Processing request", params);
409+
cc.log(cc.logLevelEnums.DEBUG, "CountlyBulk heartBeat, Processing the request:", params);
405410
makeRequest(params, (err, res) => {
406-
cc.log(cc.logLevelEnums.DEBUG, "Request Finished", res, err);
411+
cc.log(cc.logLevelEnums.DEBUG, "CountlyBulk heartBeat, Request finished with response: ", res);
407412
if (err) {
408413
bulkQueue.unshift(res);
409414
failTimeout = cc.getTimestamp() + conf.fail_timeout;
@@ -486,7 +491,7 @@ function CountlyBulk(conf) {
486491
*/
487492
function makeRequest(params, callback) {
488493
try {
489-
cc.log(cc.logLevelEnums.DEBUG, "Sending HTTP request");
494+
cc.log(cc.logLevelEnums.DEBUG, "CountlyBulk makeRequest, Sending HTTP request.");
490495
var serverOptions = parseUrl(conf.url);
491496
var data = prepareParams(params);
492497
var method = "GET";
@@ -541,7 +546,7 @@ function CountlyBulk(conf) {
541546
}
542547

543548
req.on("error", (err) => {
544-
cc.log(cc.logLevelEnums.ERROR, "Connection failed.", err);
549+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk makeRequest, Connection failed. Error:", err);
545550
if (typeof callback === "function") {
546551
callback(true, params);
547552
}
@@ -551,7 +556,7 @@ function CountlyBulk(conf) {
551556
}
552557
catch (e) {
553558
// fallback
554-
cc.log(cc.logLevelEnums.ERROR, "Failed HTTP request", e);
559+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk makeRequest, Failed HTTP request. Error: ", e);
555560
if (typeof callback === "function") {
556561
callback(true, params);
557562
}
@@ -592,6 +597,7 @@ function CountlyBulk(conf) {
592597
}
593598
catch (ex) {
594599
// there was no file, probably new init
600+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk readFile, Nothing to read. Might be first init. Error: ", ex);
595601
data = null;
596602
}
597603

@@ -601,8 +607,7 @@ function CountlyBulk(conf) {
601607
}
602608
catch (ex) {
603609
// problem parsing, corrupted file?
604-
// eslint-disable-next-line no-console
605-
console.log(ex.stack);
610+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk readFile, Problem while parsing. Error:", ex.stack);
606611
// backup corrupted file data
607612
fs.writeFile(path.resolve(__dirname, `${conf.storage_path}__${key}.${cc.getTimestamp()}${Math.random()}.json`), data, () => {});
608613
// start with new clean object
@@ -628,7 +633,7 @@ function CountlyBulk(conf) {
628633
fs.writeFile(dir, JSON.stringify(ob), (err) => {
629634
if (err) {
630635
// eslint-disable-next-line no-console
631-
console.log(err);
636+
cc.log(cc.logLevelEnums.ERROR, "CountlyBulk writeFile, Problem while writing. Error:", err);
632637
}
633638
if (typeof callback === "function") {
634639
callback(err);

0 commit comments

Comments
 (0)