Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log dev server access for log level verbose or more #2402

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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