Skip to content

Instantly share code, notes, and snippets.

@if1live
Created July 11, 2020 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save if1live/a5dae2c5b19990a54857a233094fbf48 to your computer and use it in GitHub Desktop.
Save if1live/a5dae2c5b19990a54857a233094fbf48 to your computer and use it in GitHub Desktop.
cls-hooked + express + console.info()
/**
* test
* curl "http://127.0.0.1:3000/?id=1" & curl "http://127.0.0.1:3000/?id=2"
*/
const express = require('express');
const delay = require('delay');
const { getNamespace, createNamespace } = require('cls-hooked');
const NSID = 'writer';
const KEY_LOGS = 'logs';
function set(key, val) {
const ns = getNamespace(NSID);
if (ns && ns.active) { ns.set(key, val); }
}
function get(key) {
const ns = getNamespace(NSID);
return (ns && ns.active) ? ns.get(key) : undefined;
}
function monkeypatch_console() {
const propertyNames = ['info', 'warn', 'error']
for (const property of propertyNames) {
const default_fn = console[property].bind(console);
console[property] = (message) => {
const logs = get(KEY_LOGS) || [];
logs.push(`[${property.toUpperCase()}] ${message}`);
set(KEY_LOGS, logs);
default_fn(message);
};
}
}
monkeypatch_console();
const app = express();
app.use((req, res, next) => {
const default_json = res.json.bind(res);
res.json = (data) => default_json({ ...data, _logs: get(KEY_LOGS) });
const ns = getNamespace(NSID) || createNamespace(NSID);
ns.bindEmitter(req);
ns.bindEmitter(res);
ns.run(() => next());
});
app.get('/', async (req, res) => {
const data = await execute(req.query || {});
res.json(data);
});
async function execute(input) {
const id = input.id;
console.info(`before delay: ${id}`);
await delay(100);
console.warn(`after delay: ${id}`);
return { id };
}
const port = 3000;
app.listen(port, () => console.log(`listen: 127.0.0.1:${port}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment