Move logging to disk via bunyan

- Logging is available in main process as well as renderer process, and
  entries all go to one set of rotating files. Log entries in the
  renderer process go to DevTools as well as the console. Entries from
  the main process only show up in the console.
- We save three days of logs, one day per file in %userData%/logs
- The 'debug' object store is deleted in a new database migration
- Timestamps and level included in the new log we generate for publish
  as well as the devtools
- The bunyan API is exposed via windows.log (providing the ability to
  log at different levels, and save objects instead of just text), so we
  can move our code to it over time.

FREEBIE
This commit is contained in:
Scott Nonnenberg 2017-09-25 15:00:19 -07:00
parent 33f5a804fe
commit 6b11f67dc6
No known key found for this signature in database
GPG key ID: A4931C09644C654B
12 changed files with 315 additions and 108 deletions

137
js/logging.js Normal file
View file

@ -0,0 +1,137 @@
'use strict';
const electron = require('electron');
const bunyan = require('bunyan');
const _ = require('lodash');
const ipc = electron.ipcRenderer;
const PHONE_REGEX = /\+\d{7,12}(\d{3})/g;
// Default Bunyan levels: https://github.com/trentm/node-bunyan#levels
// To make it easier to visually scan logs, we make all levels the same length
const BLANK_LEVEL = ' ';
const LEVELS = {
60: 'fatal',
50: 'error',
40: 'warn ',
30: 'info ',
20: 'debug',
10: 'trace',
};
// Backwards-compatible logging, simple strings and no level (defaulted to INFO)
function now() {
const date = new Date();
return date.toJSON();
}
function log() {
const args = Array.prototype.slice.call(arguments, 0);
const consoleArgs = ['INFO ', now()].concat(args);
console._log.apply(console, consoleArgs);
const str = args.join(' ').replace(PHONE_REGEX, "+[REDACTED]$1");
ipc.send('log-info', str)
}
if (window.console) {
console._log = console.log;
console.log = log;
};
// The mechanics of preparing a log for publish
function getHeader() {
return window.navigator.userAgent + ' node/' + window.config.node_version;
}
function getLevel(level) {
var text = LEVELS[level];
if (!text) {
return BLANK_LEVEL;
}
return text.toUpperCase();
}
function formatLine(entry) {
return getLevel(entry.level) + ' ' + entry.time + ' ' + entry.msg;
}
function format(entries) {
return entries.map(formatLine).join('\n');
}
function fetch() {
return getHeader() + '\n' + format(ipc.sendSync('fetch-log'));
};
function publish(log) {
log = log || fetch();
return new Promise(function(resolve) {
const payload = textsecure.utils.jsonThing({
files: {
'debugLog.txt': {
content: log
}
}
});
$.post('https://api.github.com/gists', payload)
.then(function(response) {
console._log('Posted debug log to ', response.html_url);
resolve(response.html_url);
})
.fail(resolve);
});
};
// A modern logging interface for the browser
// We create our own stream because we don't want to output JSON to the devtools console.
// Anyway, the default process.stdout stream goes to the command-line, not the devtools.
const logger = bunyan.createLogger({
name: 'log',
streams: [{
level: 'debug',
stream: {
write: function(entry) {
console._log(formatLine(JSON.parse(entry)));
}
}
}]
});
// The Bunyan API: https://github.com/trentm/node-bunyan#log-method-api
function logAtLevel() {
const level = arguments[0];
const args = Array.prototype.slice.call(arguments, 1);
const ipcArgs = ['log-' + level].concat(args);
ipc.send.apply(ipc, ipcArgs);
logger[level].apply(logger, args);
}
window.log = {
fatal: _.partial(logAtLevel, 'fatal'),
error: _.partial(logAtLevel, 'error'),
warn: _.partial(logAtLevel, 'warn'),
info: _.partial(logAtLevel, 'info'),
debug: _.partial(logAtLevel, 'debug'),
trace: _.partial(logAtLevel, 'trace'),
fetch,
publish,
}
window.onerror = function(message, script, line, col, error) {
log.error(error.stack);
};