Skip to content

Commit

Permalink
Log dev server access for log level verbose or more
Browse files Browse the repository at this point in the history
  • Loading branch information
lustoykov committed Dec 12, 2018
1 parent 697b234 commit 108a4e0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/core/parcel-bundler/src/Server.js
Expand Up @@ -40,6 +40,8 @@ function middleware(bundler) {
});

return function(req, res, next) {
logAccessIfVerbose();

// Wait for the bundler to finish bundling if needed
if (bundler.pending) {
bundler.once('bundled', respond);
Expand Down Expand Up @@ -107,6 +109,13 @@ function middleware(bundler) {
res.writeHead(404);
res.end();
}

function logAccessIfVerbose() {
const protocol = req.connection.encrypted ? 'https' : 'http';
const fullUrl = `${protocol}://${req.headers.host}${req.url}`;

logger.verbose(fullUrl);
}
};
}

Expand Down
37 changes: 37 additions & 0 deletions packages/core/parcel-bundler/test/server.js
@@ -1,9 +1,11 @@
const assert = require('assert');
const path = require('path');
const fs = require('@parcel/fs');
const logger = require('@parcel/Logger');
const {bundler} = require('./utils');
const http = require('http');
const https = require('https');
const sinon = require('sinon');

describe('server', function() {
let server;
Expand Down Expand Up @@ -214,4 +216,39 @@ describe('server', function() {
await fs.readFile(path.join(__dirname, '/dist/index.html'), 'utf8')
);
});

it('should not log dev server access for log level <= 3', async function() {
let b = bundler(path.join(__dirname, '/integration/html/index.html'), {
publicUrl: '/'
});
server = await b.serve(0);
const spy = sinon.spy(logger, 'verbose');
await get('/');

assert(!spy.called);

// restore back defaults
logger.verbose.restore();
});

it('should log dev server access for log level > 3', async function() {
let b = bundler(path.join(__dirname, '/integration/html/index.html'), {
publicUrl: '/'
});
server = await b.serve(0);
logger.setOptions({logLevel: 4});
const spy = sinon.spy(logger, 'verbose');

assert(!spy.called);

await get('/');

assert(spy.calledOnce);
// partial matching for call args, since port is a moving target
assert(spy.args[0][0].includes('http://localhost'));

// restore back defaults
logger.verbose.restore();
logger.setOptions({logLevel: 3});
});
});

0 comments on commit 108a4e0

Please sign in to comment.