From 16ad81f4037f9f5385bb89a5e2137bb70620dede Mon Sep 17 00:00:00 2001 From: Cogent Apps Date: Sat, 29 Apr 2023 18:30:44 +0000 Subject: [PATCH] improve logging --- server/src/database/knex.ts | 1 + server/src/index.ts | 25 +++++++++++++------------ server/src/utils.ts | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/server/src/database/knex.ts b/server/src/database/knex.ts index 0b5e1228..dbcf1df0 100644 --- a/server/src/database/knex.ts +++ b/server/src/database/knex.ts @@ -20,6 +20,7 @@ export default class KnexDatabaseAdapter extends Database { } public async initialize() { + console.log(`Initializing database adapter for ${this.knexConfig.client}.`); await this.createTables(); } diff --git a/server/src/index.ts b/server/src/index.ts index f741795c..6f1e9747 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -5,7 +5,6 @@ import express from 'express'; import { execSync } from 'child_process'; import fs from 'fs'; import https from 'https'; -import path from 'path'; import { configureAuth0 } from './auth0'; import { config } from './config'; import Database from './database/index'; @@ -25,6 +24,7 @@ import { configurePassport } from './passport'; import SyncRequestHandler, { getNumUpdatesProcessedIn5Minutes } from './endpoints/sync'; import LegacySyncRequestHandler from './endpoints/sync-legacy'; import { getActiveUsersInLast5Minutes } from './endpoints/base'; +import { formatTime } from './utils'; process.on('unhandledRejection', (reason, p) => { console.error('Unhandled Rejection at: Promise', p, 'reason:', reason); @@ -132,8 +132,8 @@ export default class ChatServer { await this.database.initialize(); try { - const callback = () => { - console.log(`Listening on port ${port}.`); + const callback = (https = false) => { + console.log(`Open ${config.publicSiteURL || `http${https ? 's' : ''}://localhost:3000`} in your browser.`); }; if (config.tls?.key && config.tls?.cert) { @@ -144,7 +144,7 @@ export default class ChatServer { cert: fs.readFileSync(config.tls.cert), }, this.app); - server.listen(port, callback); + server.listen(port, () => callback(true)); } else if (config.tls?.selfSigned) { console.log('Configuring self-signed TLS.'); @@ -165,7 +165,7 @@ export default class ChatServer { console.log(e); } - setInterval(() => { + const displayStatistics = () => { const activeUsers = getActiveUsersInLast5Minutes(); const activeUsersToDisplay = activeUsers.slice(0, 10); @@ -173,16 +173,17 @@ export default class ChatServer { const numRecentUpdates = getNumUpdatesProcessedIn5Minutes(); - console.log(`Statistics (last 5m):`); + console.log(`[${formatTime()}] ${activeUsers.length} active users and ${numRecentUpdates} updates processed in last 5m`); if (extraActiveUsers.length) { - console.log(` - ${activeUsers.length} active users: ${activeUsersToDisplay.join(', ')} and ${extraActiveUsers.length} more`); - } else { - console.log(` - ${activeUsers.length} active users: ${activeUsersToDisplay.join(', ')}`); + console.log(` - Active users: ${activeUsersToDisplay.join(', ')} and ${extraActiveUsers.length} more`); + } else if (activeUsers.length) { + console.log(` - Active users: ${activeUsersToDisplay.join(', ')}`); } - - console.log(` - ${numRecentUpdates} updates processed`); - }, 1000 * 60); + } + + setInterval(displayStatistics, 1000 * 60 * 5); + setTimeout(displayStatistics, 1000 * 30); } } diff --git a/server/src/utils.ts b/server/src/utils.ts index 6562d722..ab52b3ac 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -2,4 +2,18 @@ import crypto from 'crypto'; export function randomID(bytes = 16) { return crypto.randomBytes(bytes).toString('hex'); +} + +export function formatTime() { + // MM/DD/YYYY HH:MM:SS AM/PM + const date = new Date(); + let hours = date.getHours(); + const minutes = date.getMinutes(); + const seconds = date.getSeconds(); + const ampm = hours >= 12 ? 'PM' : 'AM'; + hours %= 12; + const month = date.getMonth() + 1; + const day = date.getDate(); + const year = date.getFullYear(); + return `${month}/${day}/${year} ${hours}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')} ${ampm}`; } \ No newline at end of file