improve logging
parent
4cf0915670
commit
16ad81f403
|
@ -20,6 +20,7 @@ export default class KnexDatabaseAdapter extends Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async initialize() {
|
public async initialize() {
|
||||||
|
console.log(`Initializing database adapter for ${this.knexConfig.client}.`);
|
||||||
await this.createTables();
|
await this.createTables();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ import express from 'express';
|
||||||
import { execSync } from 'child_process';
|
import { execSync } from 'child_process';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import https from 'https';
|
import https from 'https';
|
||||||
import path from 'path';
|
|
||||||
import { configureAuth0 } from './auth0';
|
import { configureAuth0 } from './auth0';
|
||||||
import { config } from './config';
|
import { config } from './config';
|
||||||
import Database from './database/index';
|
import Database from './database/index';
|
||||||
|
@ -25,6 +24,7 @@ import { configurePassport } from './passport';
|
||||||
import SyncRequestHandler, { getNumUpdatesProcessedIn5Minutes } from './endpoints/sync';
|
import SyncRequestHandler, { getNumUpdatesProcessedIn5Minutes } from './endpoints/sync';
|
||||||
import LegacySyncRequestHandler from './endpoints/sync-legacy';
|
import LegacySyncRequestHandler from './endpoints/sync-legacy';
|
||||||
import { getActiveUsersInLast5Minutes } from './endpoints/base';
|
import { getActiveUsersInLast5Minutes } from './endpoints/base';
|
||||||
|
import { formatTime } from './utils';
|
||||||
|
|
||||||
process.on('unhandledRejection', (reason, p) => {
|
process.on('unhandledRejection', (reason, p) => {
|
||||||
console.error('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
console.error('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
||||||
|
@ -132,8 +132,8 @@ export default class ChatServer {
|
||||||
await this.database.initialize();
|
await this.database.initialize();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const callback = () => {
|
const callback = (https = false) => {
|
||||||
console.log(`Listening on port ${port}.`);
|
console.log(`Open ${config.publicSiteURL || `http${https ? 's' : ''}://localhost:3000`} in your browser.`);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (config.tls?.key && config.tls?.cert) {
|
if (config.tls?.key && config.tls?.cert) {
|
||||||
|
@ -144,7 +144,7 @@ export default class ChatServer {
|
||||||
cert: fs.readFileSync(config.tls.cert),
|
cert: fs.readFileSync(config.tls.cert),
|
||||||
}, this.app);
|
}, this.app);
|
||||||
|
|
||||||
server.listen(port, callback);
|
server.listen(port, () => callback(true));
|
||||||
} else if (config.tls?.selfSigned) {
|
} else if (config.tls?.selfSigned) {
|
||||||
console.log('Configuring self-signed TLS.');
|
console.log('Configuring self-signed TLS.');
|
||||||
|
|
||||||
|
@ -165,7 +165,7 @@ export default class ChatServer {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
setInterval(() => {
|
const displayStatistics = () => {
|
||||||
const activeUsers = getActiveUsersInLast5Minutes();
|
const activeUsers = getActiveUsersInLast5Minutes();
|
||||||
|
|
||||||
const activeUsersToDisplay = activeUsers.slice(0, 10);
|
const activeUsersToDisplay = activeUsers.slice(0, 10);
|
||||||
|
@ -173,16 +173,17 @@ export default class ChatServer {
|
||||||
|
|
||||||
const numRecentUpdates = getNumUpdatesProcessedIn5Minutes();
|
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) {
|
if (extraActiveUsers.length) {
|
||||||
console.log(` - ${activeUsers.length} active users: ${activeUsersToDisplay.join(', ')} and ${extraActiveUsers.length} more`);
|
console.log(` - Active users: ${activeUsersToDisplay.join(', ')} and ${extraActiveUsers.length} more`);
|
||||||
} else {
|
} else if (activeUsers.length) {
|
||||||
console.log(` - ${activeUsers.length} active users: ${activeUsersToDisplay.join(', ')}`);
|
console.log(` - Active users: ${activeUsersToDisplay.join(', ')}`);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
console.log(` - ${numRecentUpdates} updates processed`);
|
setInterval(displayStatistics, 1000 * 60 * 5);
|
||||||
}, 1000 * 60);
|
setTimeout(displayStatistics, 1000 * 30);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,3 +3,17 @@ import crypto from 'crypto';
|
||||||
export function randomID(bytes = 16) {
|
export function randomID(bytes = 16) {
|
||||||
return crypto.randomBytes(bytes).toString('hex');
|
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}`;
|
||||||
|
}
|
Loading…
Reference in New Issue