Skip to content

Commit 1019ab3

Browse files
committed
log updates payload when logging
1 parent c6e8a7a commit 1019ab3

1 file changed

Lines changed: 36 additions & 17 deletions

File tree

lib/api/updates.js

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ const hasUpdatesStreamLogging = !!(config.log && config.log.updateStream);
2222

2323
const formatLogValue = value => String(value);
2424

25+
const getJournalPayload = entry => {
26+
const data = {};
27+
Object.keys(entry).forEach(key => {
28+
if (!['_id', 'ignore', 'user', 'modseq', 'unseenChange', 'created'].includes(key)) {
29+
if (entry.command !== 'COUNTERS' && key === 'unseen') {
30+
return;
31+
}
32+
data[key] = entry[key];
33+
}
34+
});
35+
36+
return data;
37+
};
38+
39+
const stringifyJournalPayload = (entry, space) => JSON.stringify(getJournalPayload(entry), null, space);
40+
2541
const logUpdatesStream = (level, session, message, ...args) => {
2642
if (!hasUpdatesStreamLogging) {
2743
return;
@@ -35,32 +51,36 @@ const logUpdatesEvent = (session, source, entry) => {
3551
return;
3652
}
3753

54+
const payload = stringifyJournalPayload(entry);
55+
3856
if (entry.command === 'COUNTERS') {
3957
return log.verbose(
4058
'API',
41-
'[%s] action=updates-event source=%s user=%s event=%s eventId=%s mailbox=%s total=%s unseen=%s',
59+
'[%s] action=updates-event source=%s user=%s event=%s eventId=%s mailbox=%s total=%s unseen=%s payload=%s',
4260
session.id,
4361
source,
4462
formatLogValue(session.user.id),
4563
entry.command,
4664
formatLogValue(entry._id),
4765
formatLogValue(entry.mailbox),
4866
formatLogValue(entry.total),
49-
formatLogValue(entry.unseen)
67+
formatLogValue(entry.unseen),
68+
payload
5069
);
5170
}
5271

5372
log.verbose(
5473
'API',
55-
'[%s] action=updates-event source=%s user=%s event=%s eventId=%s mailbox=%s message=%s modseq=%s',
74+
'[%s] action=updates-event source=%s user=%s event=%s eventId=%s mailbox=%s message=%s modseq=%s payload=%s',
5675
session.id,
5776
source,
5877
formatLogValue(session.user.id),
5978
formatLogValue(entry.command),
6079
formatLogValue(entry._id),
6180
formatLogValue(entry.mailbox),
6281
formatLogValue(entry.message),
63-
formatLogValue(entry.modseq)
82+
formatLogValue(entry.modseq),
83+
payload
6484
);
6585
};
6686

@@ -201,9 +221,11 @@ module.exports = (db, server, notifier) => {
201221
} catch (err) {
202222
log.error(
203223
'API',
204-
'[%s] action=updates-event-write-fail source=live user=%s error=%s',
224+
'[%s] action=updates-event-write-fail source=live user=%s event=%s payload=%s error=%s',
205225
session.id,
206226
session.user.id.toString(),
227+
formatLogValue(message.command),
228+
stringifyJournalPayload(message),
207229
err.stack || err
208230
);
209231
}
@@ -387,18 +409,8 @@ module.exports = (db, server, notifier) => {
387409
};
388410

389411
function formatJournalData(e) {
390-
let data = {};
391-
Object.keys(e).forEach(key => {
392-
if (!['_id', 'ignore', 'user', 'modseq', 'unseenChange', 'created'].includes(key)) {
393-
if (e.command !== 'COUNTERS' && key === 'unseen') {
394-
return;
395-
}
396-
data[key] = e[key];
397-
}
398-
});
399-
400412
let response = [];
401-
response.push('data: ' + JSON.stringify(data, false, 2).split('\n').join('\ndata: '));
413+
response.push('data: ' + stringifyJournalPayload(e, 2).split('\n').join('\ndata: '));
402414
if (e._id) {
403415
response.push('id: ' + e._id.toString());
404416
}
@@ -496,7 +508,14 @@ function loadJournalStream(db, res, user, lastEventId, done, onEntry) {
496508
res.write(data);
497509
onEntry('replay', e);
498510
} catch (err) {
499-
log.error('API', 'action=updates-event-write-fail user=%s event=%s error=%s', user.toString(), e.command, err.stack || err);
511+
log.error(
512+
'API',
513+
'action=updates-event-write-fail user=%s event=%s payload=%s error=%s',
514+
user.toString(),
515+
formatLogValue(e.command),
516+
stringifyJournalPayload(e),
517+
err.stack || err
518+
);
500519
}
501520

502521
processed++;

0 commit comments

Comments
 (0)