Skip to content

Commit

Permalink
style: use logger rather than console
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Jan 5, 2024
1 parent 6f53519 commit 4bd0690
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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}`);
});
});
7 changes: 4 additions & 3 deletions src/server/model/monitor/manager.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
}

Expand All @@ -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.');
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/model/monitor/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down
7 changes: 4 additions & 3 deletions src/server/udp/server.ts
Original file line number Diff line number Diff line change
@@ -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();
});

Expand All @@ -14,15 +15,15 @@ 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) {}
});

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);
Expand Down

0 comments on commit 4bd0690

Please sign in to comment.