From 4bd069081a77560fa3f131f7aa05456e3199460f Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 6 Jan 2024 00:16:43 +0800 Subject: [PATCH] style: use logger rather than console --- src/server/main.ts | 9 +++++---- src/server/model/monitor/manager.ts | 7 ++++--- src/server/model/monitor/runner.ts | 4 ++-- src/server/udp/server.ts | 7 ++++--- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/server/main.ts b/src/server/main.ts index 6933abf2..0680a4f8 100644 --- a/src/server/main.ts +++ b/src/server/main.ts @@ -24,6 +24,7 @@ import { env } from './utils/env'; import cors from 'cors'; import { serverStatusRouter } from './router/serverStatus'; import { initCronjob } from './cronjob'; +import { logger } from './utils/logger'; const port = settings.port; @@ -68,16 +69,16 @@ if (env.allowOpenapi) { } app.use((err: any, req: any, res: any, next: any) => { - console.error(err); + logger.error(err); res.status(500).json({ message: err.message }); }); httpServer.listen(port, () => { ViteExpress.bind(app, httpServer, () => { - console.log(`Server is listening on port ${port}...`); + logger.info(`Server is listening on port ${port}...`); if (env.allowOpenapi) { - console.log(`Openapi UI: http://127.0.0.1:${port}/open/_ui`); + logger.info(`Openapi UI: http://127.0.0.1:${port}/open/_ui`); } - console.log(`Website: http://127.0.0.1:${port}`); + logger.info(`Website: http://127.0.0.1:${port}`); }); }); diff --git a/src/server/model/monitor/manager.ts b/src/server/model/monitor/manager.ts index 2a077c0d..7e1bb14c 100644 --- a/src/server/model/monitor/manager.ts +++ b/src/server/model/monitor/manager.ts @@ -1,6 +1,7 @@ import { Monitor, Notification } from '@prisma/client'; import { prisma } from '../_client'; import { MonitorRunner } from './runner'; +import { logger } from '../../utils/logger'; export type MonitorUpsertData = Pick< Monitor, @@ -93,7 +94,7 @@ export class MonitorManager { */ async startAll() { if (this.isStarted === true) { - console.warn('MonitorManager.startAll should only call once, skipped.'); + logger.warn('MonitorManager.startAll should only call once, skipped.'); return; } @@ -115,11 +116,11 @@ export class MonitorManager { this.monitorRunner[m.id] = runner; await runner.startMonitor(); } catch (err) { - console.error('Start monitor error:', String(err)); + logger.error('Start monitor error:', String(err)); } }) ).then(() => { - console.log('All monitor has been begin.'); + logger.info('All monitor has been begin.'); }); } diff --git a/src/server/model/monitor/runner.ts b/src/server/model/monitor/runner.ts index 0fbc3f9a..e1843d01 100644 --- a/src/server/model/monitor/runner.ts +++ b/src/server/model/monitor/runner.ts @@ -92,7 +92,7 @@ export class MonitorRunner { run(); - console.log(`Start monitor ${monitor.name}(${monitor.id})`); + logger.info(`Start monitor ${monitor.name}(${monitor.id})`); } stopMonitor() { @@ -104,7 +104,7 @@ export class MonitorRunner { this.timer = null; } - console.log(`Stop monitor ${monitor.name}(${monitor.id})`); + logger.info(`Stop monitor ${monitor.name}(${monitor.id})`); } async restartMonitor() { diff --git a/src/server/udp/server.ts b/src/server/udp/server.ts index 54c64022..56dd3f05 100644 --- a/src/server/udp/server.ts +++ b/src/server/udp/server.ts @@ -1,11 +1,12 @@ import dgram from 'dgram'; import { recordServerStatus } from '../model/serverStatus'; +import { logger } from '../utils/logger'; export function initUdpServer(port: number) { const server = dgram.createSocket('udp4'); server.on('error', (err) => { - console.log(`Init error:\n${err.stack}`); + logger.info(`Init error:\n${err.stack}`); server.close(); }); @@ -14,7 +15,7 @@ export function initUdpServer(port: number) { const raw = String(msg); const json = JSON.parse(String(msg)); - console.log('[UDP] recevice tianji report:', raw, 'info', rinfo); + logger.info('[UDP] recevice tianji report:', raw, 'info', rinfo); recordServerStatus(json); } catch (err) {} @@ -22,7 +23,7 @@ export function initUdpServer(port: number) { server.on('listening', () => { const address = server.address(); - console.log(`UDP Server is listening: ${address.address}:${address.port}`); + logger.info(`UDP Server is listening: ${address.address}:${address.port}`); }); server.bind(port);