Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cogentapps committed Apr 29, 2023
1 parent 4cf0915 commit 16ad81f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions server/src/database/knex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
25 changes: 13 additions & 12 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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.');

Expand All @@ -165,24 +165,25 @@ export default class ChatServer {
console.log(e);
}

setInterval(() => {
const displayStatistics = () => {
const activeUsers = getActiveUsersInLast5Minutes();

const activeUsersToDisplay = activeUsers.slice(0, 10);
const extraActiveUsers = activeUsers.slice(10);

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);
}
}

Expand Down
14 changes: 14 additions & 0 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

0 comments on commit 16ad81f

Please sign in to comment.