From e9f806b88aff67380cdd35f180d26fd8030e868c Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Mon, 10 Jun 2024 09:45:30 +0200 Subject: [PATCH] style: organise console levels and semantics --- apps/server/src/app.ts | 12 ++++---- apps/server/src/classes/Logger.ts | 5 ++-- apps/server/src/index.ts | 14 ++++++--- apps/server/src/utils/console.ts | 48 +++++++++++++++++++++++++++++-- 4 files changed, 64 insertions(+), 15 deletions(-) diff --git a/apps/server/src/app.ts b/apps/server/src/app.ts index 82afb50e9..052ef7c79 100644 --- a/apps/server/src/app.ts +++ b/apps/server/src/app.ts @@ -20,7 +20,7 @@ import { clearUploadfolder, } from './setup/index.js'; import { ONTIME_VERSION } from './ONTIME_VERSION.js'; -import { consoleGreen } from './utils/console.js'; +import { consoleSuccess, consoleHighlight } from './utils/console.js'; // Import Routers import { appRouter } from './api-data/index.js'; @@ -47,7 +47,8 @@ import { initRundown } from './services/rundown-service/RundownService.js'; import { generateCrashReport } from './utils/generateCrashReport.js'; import { getNetworkInterfaces } from './utils/networkInterfaces.js'; -console.log(`Starting Ontime version ${ONTIME_VERSION}`); +console.log('\n'); +consoleHighlight(`Starting Ontime version ${ONTIME_VERSION}`); if (!isProduction) { console.log(`Ontime running in ${environment} environment`); @@ -205,8 +206,9 @@ export const startServer = async ( expressServer.listen(serverPort, '0.0.0.0', () => { const nif = getNetworkInterfaces(); + consoleSuccess(`Local: http://localhost:${serverPort}/editor`); for (const key of Object.keys(nif)) { - consoleGreen(`Ontime running on: http://${nif[key].address}:${serverPort}`); + consoleSuccess(`Network: http://${nif[key].address}:${serverPort}/editor`); } }); @@ -252,7 +254,7 @@ export const startIntegrations = async (config?: { osc: OSCSettings; http: HttpS * @return {Promise} */ export const shutdown = async (exitCode = 0) => { - console.log(`Ontime shutting down with code ${exitCode}`); + consoleHighlight(`Ontime shutting down with code ${exitCode}`); // clear the restore file if it was a normal exit // 0 means it was a SIGNAL @@ -270,7 +272,7 @@ export const shutdown = async (exitCode = 0) => { process.exit(exitCode); }; -process.on('exit', (code) => console.log(`Ontime shutdown with code: ${code}`)); +process.on('exit', (code) => consoleHighlight(`Ontime shutdown with code: ${code}`)); process.on('unhandledRejection', async (error) => { generateCrashReport(error); diff --git a/apps/server/src/classes/Logger.ts b/apps/server/src/classes/Logger.ts index d242c7288..6e520c212 100644 --- a/apps/server/src/classes/Logger.ts +++ b/apps/server/src/classes/Logger.ts @@ -4,7 +4,7 @@ import { generateId, millisToString } from 'ontime-utils'; import { clock } from '../services/Clock.js'; import { isProduction } from '../setup/index.js'; import { socket } from '../adapters/WebsocketAdapter.js'; -import { consoleRed } from '../utils/console.js'; +import { consoleSubdued, consoleRed } from '../utils/console.js'; class Logger { private queue: Log[]; @@ -47,8 +47,7 @@ class Logger { if (log.level === LogLevel.Severe) { consoleRed(`[${log.level}] \t ${log.origin} \t ${log.text}`); } else { - // eslint-disable-next-line no-console - console.log(`[${log.level}] \t ${log.origin} \t ${log.text}`); + consoleSubdued(`[${log.level}] \t ${log.origin} \t ${log.text}`); } } diff --git a/apps/server/src/index.ts b/apps/server/src/index.ts index 06be7998a..7675b4d7a 100644 --- a/apps/server/src/index.ts +++ b/apps/server/src/index.ts @@ -1,16 +1,22 @@ /* eslint-disable no-console */ +import { consoleHighlight, consoleRed } from './utils/console.js'; import { initAssets, startIntegrations, startServer } from './app.js'; async function startOntime() { try { - console.log('Request: Initialise assets...'); + console.log('\n'); + consoleHighlight('Request: Initialise assets...'); await initAssets(); - console.log('Request: Start server...'); + + console.log('\n'); + consoleHighlight('Request: Start server...'); await startServer(); - console.log('Request: Start integrations...'); + + console.log('\n'); + consoleHighlight('Request: Start integrations...'); await startIntegrations(); } catch (error) { - console.log(`Request failed: ${error}`); + consoleRed(`Request failed: ${error}`); } } diff --git a/apps/server/src/utils/console.ts b/apps/server/src/utils/console.ts index be1f3a6f2..c4cd4d1df 100644 --- a/apps/server/src/utils/console.ts +++ b/apps/server/src/utils/console.ts @@ -3,13 +3,55 @@ /** * Utility function to log messages in green */ -export function consoleGreen(message: string) { - console.log(`\x1b[32m${message}\x1b[0m`); +export function consoleSuccess(message: string) { + console.log(inGreen(message)); } /** * Utility function to log messages in red */ export function consoleRed(message: string) { - console.error(`\x1b[31m${message}\x1b[0m`); + console.error(inRed(message)); +} + +/** + * Utility function to log messages with dimmed appearance + */ +export function consoleHighlight(message: string) { + console.log(inCyan(message)); +} + +/** + * Utility function to log messages with dimmed appearance + */ +export function consoleSubdued(message: string) { + console.log(inGray(message)); +} + +/** + * Utility function for console, styles text in green + */ +function inGreen(message: string): string { + return `\x1b[32m${message}\x1b[0m`; +} + +/** + * Utility function for console, styles text in red + */ +function inRed(message: string): string { + return `\x1b[31m${message}\x1b[0m`; +} + +/** + * Utility function for console, styles text in cyan + */ +function inCyan(message: string): string { + return `\x1b[96m${message}\x1b[0m`; +} + +/** + * Utility function for console, styles text in gray + */ +function inGray(message: string): string { + return `\x1b[2m${message}\x1b[0m`; }