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 console invocations to the terminal. #426

Merged
merged 1 commit into from
Jun 13, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import getSecurityHeadersMiddleware from './getSecurityHeadersMiddleware';
import loadRawBodyMiddleware from './loadRawBodyMiddleware';
import openStackFrameInEditorMiddleware from './openStackFrameInEditorMiddleware';
import openURLMiddleware from './openURLMiddleware';
import logToConsoleMiddleware from './logToConsoleMiddleware';
import statusPageMiddleware from './statusPageMiddleware';
import systraceProfileMiddleware from './systraceProfileMiddleware';
import getDevToolsMiddleware from './getDevToolsMiddleware';
Expand Down Expand Up @@ -53,6 +54,7 @@ export default class MiddlewareManager {
.use('/debugger-ui', serveStatic(debuggerUIFolder))
.use(openStackFrameInEditorMiddleware(this.options))
.use(openURLMiddleware)
.use(logToConsoleMiddleware)
.use(copyToClipBoardMiddleware)
.use(statusPageMiddleware)
.use(systraceProfileMiddleware)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

import chalk from 'chalk';

let cache = [];
let timer;

const log = ({level, data, id}) => {
thymikee marked this conversation as resolved.
Show resolved Hide resolved
const logFunction = level !== 'trace' && console[level] ? level : 'log';
const color =
level === 'error' ? 'red' : level === 'warn' ? 'yellow' : 'white';

console[logFunction].apply(console, [
chalk.inverse[color].bold(` ${level.toUpperCase()} `),
...data,
]);
};

// Hold messages and flush them to reduce the amount of out-of-order logs
const flush = () => {
timer = null;
cache.sort((a, b) => a.id - b.id).forEach(log);
cache = [];
};

export default (req, res, next) => {
if (req.url === '/log-to-console') {
cache.push(JSON.parse(req.rawBody));
if (!timer) {
timer = setTimeout(flush, 200);
}

res.end('OK');
} else {
next();
}
};