diff --git a/config/config.example.js b/config/config.example.js index 2ae63dc08..1fae5ae9c 100644 --- a/config/config.example.js +++ b/config/config.example.js @@ -291,11 +291,6 @@ module.exports = { */ verbose: false, - /* RPC errors are shown by default, but if you really don't care, - * you can suppress them - */ - suppressRPCErrors: false, - /* clients can use the /settings/ app to opt out of usage feedback * which informs the server of things like how much each app is being * used, and whether certain clientside features are supported by diff --git a/lib/log.js b/lib/log.js index 7170fd0e2..941150530 100644 --- a/lib/log.js +++ b/lib/log.js @@ -14,6 +14,7 @@ var messageTemplate = function (type, time, tag, info) { }; var write = function (ctx, content) { + if (!ctx.store) { return; } ctx.store.log(ctx.channelName, content); }; @@ -44,7 +45,12 @@ var handlers = { } }; +var noop = function () {}; + var createLogType = function (ctx, type) { + if (logLevels.indexOf(type) < logLevels.indexOf(ctx.logLevel)) { + return noop; + } return function (tag, info) { var time = new Date().toISOString(); var content; @@ -53,7 +59,6 @@ var createLogType = function (ctx, type) { } catch (e) { return; } - if (ctx.logToStdout && typeof(handlers[type]) === 'function') { handlers[type](ctx, time, tag, info); } @@ -61,17 +66,29 @@ var createLogType = function (ctx, type) { }; }; -// Log.verbose('THING', x); +var createMethods = function (ctx) { + var log = {}; + logLevels.forEach(function (type) { + log[type] = createLogType(ctx, type); + }); + return log; +}; + Logger.create = function (config, cb) { - if (!config.logPath) { - // XXX don't crash, print that you won't log to file - throw new Error("Logger: Expected filePath"); + if (typeof(config.logLevel) !== 'string') { + config.logLevel = 'info'; } - /* config: { - filePath: '???', - logLevel: 'silly', - } */ + var ctx = { + channelName: launchTime, + logFeedback: Boolean(config.logFeedback), + logLevel: config.logLevel, + }; + + if (!config.logPath) { + console.log("No logPath configured. Logging to file disabled"); + return void cb(Object.freeze(createMethods(ctx))); + } var date = new Date(); var launchTime = ('' + date.getUTCFullYear()).slice(-2) + date.toISOString(); @@ -79,20 +96,8 @@ Logger.create = function (config, cb) { Store.create({ filePath: config.logPath, }, function (store) { - var ctx = { - store: store, - channelName: launchTime, - logFeedback: Boolean(config.logFeedback), - // TODO respect configured log settings - logLevel: logLevels.indexOf(config.logLevel), // 0 for silly, 1 for debug - }; - - var log = {}; - logLevels.forEach(function (type) { - log[type] = createLogType(ctx, type); - }); - - cb(Object.freeze(log)); + ctx.store = store; + cb(Object.freeze(createMethods(ctx))); }); }; diff --git a/rpc.js b/rpc.js index 9993f3a19..00ab2949b 100644 --- a/rpc.js +++ b/rpc.js @@ -12,7 +12,7 @@ var Fse = require("fs-extra"); var Path = require("path"); var Https = require("https"); const Package = require('./package.json'); -const Pinned = require('./pinned'); +const Pinned = require('./scripts/pinned'); const Saferphore = require("saferphore"); const nThen = require("nthen"); const getFolderSize = require("get-folder-size"); @@ -25,15 +25,16 @@ var Store = require("./storage/file"); var DEFAULT_LIMIT = 50 * 1024 * 1024; var SESSION_EXPIRATION_TIME = 60 * 1000; -var SUPPRESS_RPC_ERRORS = false; var Log; var WARN = function (e, output) { - if (!SUPPRESS_RPC_ERRORS && e && output) { - console.error(new Date().toISOString() + ' [' + String(e) + ']', output); - console.error(new Error(e).stack); - console.error(); + if (e && output) { + Log.warn(e, { + output: output, + message: String(e), + stack: new Error(e).stack, + }); } }; @@ -1629,8 +1630,6 @@ RPC.create = function ( // load pin-store... Log.silly('LOADING RPC MODULE'); - if (config.suppressRPCErrors) { SUPPRESS_RPC_ERRORS = true; } - var keyOrDefaultString = function (key, def) { return typeof(config[key]) === 'string'? config[key]: def; }; diff --git a/server.js b/server.js index 8f7b493fa..24d7fecc3 100644 --- a/server.js +++ b/server.js @@ -243,7 +243,7 @@ httpServer.listen(config.httpPort,config.httpAddress,function(){ var port = config.httpPort; var ps = port === 80? '': ':' + port; - console.log('\n[%s] server available http://%s%s', new Date().toISOString(), hostName, ps); + console.log('[%s] server available http://%s%s', new Date().toISOString(), hostName, ps); }); if (config.httpSafePort) { Http.createServer(app).listen(config.httpSafePort, config.httpAddress); @@ -254,17 +254,19 @@ var wsConfig = { server: httpServer }; var rpc; var historyKeeper; +var log; + // Initialize tasks, then rpc, then store, then history keeper and then start the server var nt = nThen(function (w) { // set up logger var Logger = require("./lib/log"); - console.log("Loading logging module"); + //console.log("Loading logging module"); Logger.create(config, w(function (_log) { - config.log = _log; + log = config.log = _log; })); }).nThen(function (w) { var Tasks = require("./storage/tasks"); - console.log("loading task scheduler"); + //log.debug('loading task scheduler'); Tasks.create(config, w(function (e, tasks) { config.tasks = tasks; })); @@ -297,7 +299,7 @@ var nt = nThen(function (w) { }).nThen(function () { if (config.useExternalWebsocket) { return; } if (websocketPort !== config.httpPort) { - console.log("setting up a new websocket server"); + log.debug("setting up a new websocket server"); wsConfig = { port: websocketPort}; } var wsSrv = new WebSocketServer(wsConfig);