From 3ae120b3baa8e4fd165dd1535ccaa22d721873ed Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Tue, 19 Nov 2019 12:48:23 -0300 Subject: [PATCH] Add ability to log requests --- README.md | 38 +++++++++++++++++++++++++++++--------- src/server/app.ts | 10 ++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9a6d6c2..6ca1424 100644 --- a/README.md +++ b/README.md @@ -134,17 +134,37 @@ unix domain socket. To access the server, go to http://localhost:3000 (or another port). -# Testing +# Logging + +By default, Peer Calls server will log only basic information. Client-side +logging is disabled by default. + +Server-side logs can be configured via the `DEBUG` environment variable. Setting +it to `peercalls,peercalls:*` will enable all server-side logging: + +- `DEBUG=peercalls,peercalls:* npm run start:server` + +Client-side logs can be configured via `localStorage.DEBUG` and +`localStorage.LOG` variables: + +- Setting `localStorage.LOG=1` enables logging of Redux actions and state + changes +- Setting `localStorage.DEBUG=peercalls,peercalls:*` enables all other + client-side logging + +# Development + +Below are some common NPM scripts that are used for development: -```bash -npm install -npm test ``` - -To run all tests and build production artifacts, run: - -```bash -npm run ci +npm start start the precompiled server. +npm run build build all client-side resources. +npm run start:server start and compile server-side TypeScript on the fly, + restarts the server when the resources change. +npm run start:watch start the server, and recompile client-side resources + when the sources change. +npm test run all tests. +npm run ci run all linting, tests and build the client-side ``` # Browser Support diff --git a/src/server/app.ts b/src/server/app.ts index 2799210..1443dbe 100644 --- a/src/server/app.ts +++ b/src/server/app.ts @@ -11,6 +11,7 @@ import index from './routes/index' import dot from 'express-dot-engine' const debug = _debug('peercalls') +const logRequest = _debug('peercalls:requests') const BASE_URL: string = config.baseUrl const SOCKET_URL = `${BASE_URL}/ws` @@ -29,6 +30,15 @@ app.engine('html', dot.__express) app.set('view engine', 'html') app.set('views', path.join(__dirname, '../../views')) +app.use((req, res, next) => { + const start = Date.now() + res.on('finish', () => { + const duration = Date.now() - start + logRequest('%s %s %sms', req.method, req.originalUrl, duration) + }) + next() +}) + const router = express.Router() router.use('/res', express.static(path.join(__dirname, '../../res'))) router.use('/static', express.static(path.join(__dirname, '../../build')))