Skip to content

Commit

Permalink
Merge branch 'log-server-access-for-verbose' of https://github.com/lu…
Browse files Browse the repository at this point in the history
…stoykov/parcel into lustoykov-log-server-access-for-verbose
  • Loading branch information
devongovett committed Dec 17, 2018
2 parents e653e66 + 3d5fb27 commit 4021245
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/core/integration-tests/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, '_log');
await get('/');

assert(!spy.called);

// restore back defaults
logger._log.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, '_log');

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('Request: http://localhost'));

// restore back defaults
logger._log.restore();
logger.setOptions({logLevel: 3});
});
});
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(`Request: ${fullUrl}`);
}
};
}

Expand Down

0 comments on commit 4021245

Please sign in to comment.