Skip to content

Commit

Permalink
Fix logger errors and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarengathomas committed Oct 27, 2017
1 parent 8b1dd92 commit c8de2be
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var
var ONE_DAY = 60 * 60 * 24 * 1000;
var ONE_MONTH = ONE_DAY * 30;

var accessLogger;

function ReverseProxy(opts) {
if (!(this instanceof ReverseProxy)) {
return new ReverseProxy(opts);
Expand All @@ -30,7 +32,6 @@ function ReverseProxy(opts) {
this.opts = opts = opts || {};

var log;
var accessLogger;

if (opts.logs) {

Expand All @@ -52,14 +53,24 @@ function ReverseProxy(opts) {
transports.push(new winston.transports.Console());
}

log = winston.createLogger({ transports: transports });
var printFormat = winston.format.printf;

var format = printFormat(function (info) {
return new Date().toLocaleString() + ' - ' + info.message;
});

log = this.log = winston.createLogger({
transports: transports,
format: format,
});

if (opts.logs.access) {
var accessLogStream = fs.createWriteStream(accessLogFilename, { flags: 'a' });

accessLogger = morgan('combined', { stream: accessLogStream });
}
}

var _this = this;

if (opts.cluster && typeof opts.cluster !== 'number' ||  opts.cluster > 32) {
Expand Down Expand Up @@ -197,6 +208,7 @@ ReverseProxy.prototype.setupHttpProxy = function (proxy, websocketsUpgrade, log,
var src = _this._getSource(req);
var target = _this._getTarget(src, req);
var done = finalhandler(req, res);

accessLogger(req, res, function (err) {
if (err) {
return done(err);
Expand Down Expand Up @@ -433,7 +445,7 @@ ReverseProxy.prototype.register = function (src, target, opts) {

route.urls.push(target);

this.log && this.log.info('Registered a new route from: ' + src + ' to: ' + target);
this.log && this.log.info('Registered a new route from: ' + src.href + ' to: ' + target.href);
return this;
};

Expand Down Expand Up @@ -513,6 +525,9 @@ ReverseProxy.prototype.unregister = function (src, target) {
}
}

src = src ? src.href : 'unknown';
target = target ? target.href : 'unknown';

this.log && this.log.info('Unregistered a route from: ' + src + ' to: ' + target);
}
return this;
Expand Down

0 comments on commit c8de2be

Please sign in to comment.