diff --git a/lib/configproxy.js b/lib/configproxy.js index c808417b..c65d57ef 100644 --- a/lib/configproxy.js +++ b/lib/configproxy.js @@ -5,7 +5,8 @@ // var http = require('http'), - httpProxy = require('http-proxy'); + httpProxy = require('http-proxy'), + log = require('loglevel'); var bound = function (that, method) { // bind a method, to ensure `this=that` when it is called @@ -92,7 +93,7 @@ var ConfigurableProxy = function (options) { try { return handler.apply(that, arguments); } catch (e) { - console.log("Error in handler for " + + log.error("Error in handler for " + req.method + ' ' + req.url + ': ', e ); } @@ -111,7 +112,7 @@ var ConfigurableProxy = function (options) { ConfigurableProxy.prototype.fail = function (req, res, code, msg) { msg = msg || ''; - console.log('[' + code + '] ' + req.method + ' ' + req.url + ': ' + msg); + log.error('[' + code + '] ' + req.method + ' ' + req.url + ': ' + msg); res.writeHead(code); res.write(msg); res.end(); @@ -136,7 +137,7 @@ ConfigurableProxy.prototype.get_routes = function (req, res) { ConfigurableProxy.prototype.post_routes = function (req, res, path, data) { // POST adds a new route - console.log('POST', path, data); + log.debug('POST', path, data); // ensure path starts with / if (path[0] != '/') { @@ -153,7 +154,7 @@ ConfigurableProxy.prototype.post_routes = function (req, res, path, data) { ConfigurableProxy.prototype.delete_routes = function (req, res, path) { // DELETE removes an existing route - console.log('DELETE', path); + log.debug('DELETE', path); if (this.routes[path] === undefined) { res.writeHead(404); } else { @@ -193,11 +194,11 @@ ConfigurableProxy.prototype.target_for_url = function (url) { ConfigurableProxy.prototype.handle_ws = function (req, res, head) { // no local route found, time to proxy var target = this.target_for_url(req.url); - console.log("PROXY WS " + req.url + " to " + target); + log.debug("PROXY WS " + req.url + " to " + target); this.proxy.ws(req, res, head, { target: target }, function (e) { - console.log("Proxy error: ", e); + log.error("Proxy error: ", e); res.writeHead(502); res.write("Proxy target missing"); res.end(); @@ -206,11 +207,11 @@ ConfigurableProxy.prototype.handle_ws = function (req, res, head) { ConfigurableProxy.prototype.handle_proxy_request = function (req, res) { var target = this.target_for_url(req.url); - console.log("PROXY " + req.method + " " + req.url + " to " + target); + log.debug("PROXY " + req.method + " " + req.url + " to " + target); this.proxy.web(req, res, { target: target }, function (e) { - console.log("Proxy error: ", e); + log.error("Proxy error: ", e); res.writeHead(502); res.write("Proxy target missing"); res.end(); diff --git a/lib/main.js b/lib/main.js index 799087c6..332cc9a0 100644 --- a/lib/main.js +++ b/lib/main.js @@ -3,11 +3,12 @@ cli entrypoint for starting a Configurable Proxy */ -var fs = require('fs'); -var args = require('commander'); +var fs = require('fs'), + args = require('commander'), + log = require('loglevel'); args - .version('0.0.0') + .version('0.0.1') // .option('-h', '--help', 'show help') .option('--key ', 'SSL key to use, if any') .option('--cert ', 'SSL certificate to use, if any') @@ -17,8 +18,10 @@ args .option('--upstream-port ', 'Port of the default proxy target', parseInt) .option('--api-ip ', 'Inward-facing IP for API request', 'localhost') .option('--api-port ', 'Inward-facing port for API request', parseInt) + .option('--log-level ', 'Log level (debug, info, warn, error)', 'info'); args.parse(process.argv); +log.setLevel(args.logLevel); var ConfigurableProxy = require('./configproxy.js').ConfigurableProxy; @@ -47,8 +50,8 @@ listen.api_port = args.apiPort || listen.port + 1; proxy.proxy_server.listen(listen.port, listen.ip); proxy.api_server.listen(listen.api_port, listen.api_ip); -console.log("Proxying %s:%s to %s:%s", (listen.ip || '*'), listen.port, +log.info("Proxying %s:%s to %s:%s", (listen.ip || '*'), listen.port, proxy.upstream_ip, proxy.upstream_port ); -console.log("Proxy API at %s:%s/api/routes", (listen.api_ip || '*'), listen.api_port); +log.info("Proxy API at %s:%s/api/routes", (listen.api_ip || '*'), listen.api_port); diff --git a/package.json b/package.json index 7f817437..db8269bc 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,9 @@ "url": "https://github.com/jupyter/jupyterhub" }, "dependencies": { + "commander": "~2.2", "http-proxy": "~1.1", - "commander": "~2.2" + "loglevel": "~1.0" }, "files": [ "lib/configproxy.js",