style: use logger rather than console

This commit is contained in:
moonrailgun 2024-01-06 00:16:43 +08:00
parent 6f53519f6c
commit 4bd069081a
4 changed files with 15 additions and 12 deletions

View File

@ -24,6 +24,7 @@ import { env } from './utils/env';
import cors from 'cors'; import cors from 'cors';
import { serverStatusRouter } from './router/serverStatus'; import { serverStatusRouter } from './router/serverStatus';
import { initCronjob } from './cronjob'; import { initCronjob } from './cronjob';
import { logger } from './utils/logger';
const port = settings.port; const port = settings.port;
@ -68,16 +69,16 @@ if (env.allowOpenapi) {
} }
app.use((err: any, req: any, res: any, next: any) => { app.use((err: any, req: any, res: any, next: any) => {
console.error(err); logger.error(err);
res.status(500).json({ message: err.message }); res.status(500).json({ message: err.message });
}); });
httpServer.listen(port, () => { httpServer.listen(port, () => {
ViteExpress.bind(app, httpServer, () => { ViteExpress.bind(app, httpServer, () => {
console.log(`Server is listening on port ${port}...`); logger.info(`Server is listening on port ${port}...`);
if (env.allowOpenapi) { 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}`);
}); });
}); });

View File

@ -1,6 +1,7 @@
import { Monitor, Notification } from '@prisma/client'; import { Monitor, Notification } from '@prisma/client';
import { prisma } from '../_client'; import { prisma } from '../_client';
import { MonitorRunner } from './runner'; import { MonitorRunner } from './runner';
import { logger } from '../../utils/logger';
export type MonitorUpsertData = Pick< export type MonitorUpsertData = Pick<
Monitor, Monitor,
@ -93,7 +94,7 @@ export class MonitorManager {
*/ */
async startAll() { async startAll() {
if (this.isStarted === true) { if (this.isStarted === true) {
console.warn('MonitorManager.startAll should only call once, skipped.'); logger.warn('MonitorManager.startAll should only call once, skipped.');
return; return;
} }
@ -115,11 +116,11 @@ export class MonitorManager {
this.monitorRunner[m.id] = runner; this.monitorRunner[m.id] = runner;
await runner.startMonitor(); await runner.startMonitor();
} catch (err) { } catch (err) {
console.error('Start monitor error:', String(err)); logger.error('Start monitor error:', String(err));
} }
}) })
).then(() => { ).then(() => {
console.log('All monitor has been begin.'); logger.info('All monitor has been begin.');
}); });
} }

View File

@ -92,7 +92,7 @@ export class MonitorRunner {
run(); run();
console.log(`Start monitor ${monitor.name}(${monitor.id})`); logger.info(`Start monitor ${monitor.name}(${monitor.id})`);
} }
stopMonitor() { stopMonitor() {
@ -104,7 +104,7 @@ export class MonitorRunner {
this.timer = null; this.timer = null;
} }
console.log(`Stop monitor ${monitor.name}(${monitor.id})`); logger.info(`Stop monitor ${monitor.name}(${monitor.id})`);
} }
async restartMonitor() { async restartMonitor() {

View File

@ -1,11 +1,12 @@
import dgram from 'dgram'; import dgram from 'dgram';
import { recordServerStatus } from '../model/serverStatus'; import { recordServerStatus } from '../model/serverStatus';
import { logger } from '../utils/logger';
export function initUdpServer(port: number) { export function initUdpServer(port: number) {
const server = dgram.createSocket('udp4'); const server = dgram.createSocket('udp4');
server.on('error', (err) => { server.on('error', (err) => {
console.log(`Init error:\n${err.stack}`); logger.info(`Init error:\n${err.stack}`);
server.close(); server.close();
}); });
@ -14,7 +15,7 @@ export function initUdpServer(port: number) {
const raw = String(msg); const raw = String(msg);
const json = JSON.parse(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); recordServerStatus(json);
} catch (err) {} } catch (err) {}
@ -22,7 +23,7 @@ export function initUdpServer(port: number) {
server.on('listening', () => { server.on('listening', () => {
const address = server.address(); 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); server.bind(port);