mirror of
https://github.com/2234839/web-font.git
synced 2025-04-06 05:25:44 +08:00
🔥 新增日志记录
This commit is contained in:
parent
e24387233a
commit
efc2f723a4
@ -2,10 +2,12 @@ import { NestFactory } from '@nestjs/core';
|
|||||||
import { AppModule } from './app.module';
|
import { AppModule } from './app.module';
|
||||||
import serveStatic from 'serve-static';
|
import serveStatic from 'serve-static';
|
||||||
import { Response } from 'express';
|
import { Response } from 'express';
|
||||||
|
import { logger } from './middleware/logger.middleware';
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
app.enableCors();
|
app.enableCors();
|
||||||
|
app.use(logger);
|
||||||
|
|
||||||
await app.listen(3000);
|
await app.listen(3000);
|
||||||
}
|
}
|
||||||
|
26
src/middleware/logger.middleware.ts
Normal file
26
src/middleware/logger.middleware.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { Response, Request } from 'express';
|
||||||
|
|
||||||
|
const NS_PER_SEC = 1e9;
|
||||||
|
export async function logger(req: Request, res: Response, next) {
|
||||||
|
const time = process.hrtime();
|
||||||
|
next();
|
||||||
|
res.once('finish', () => {
|
||||||
|
const diff = process.hrtime(time);
|
||||||
|
console.log(
|
||||||
|
`[${req.headers['x-forwarded-for'] ||
|
||||||
|
req.connection.remoteAddress ||
|
||||||
|
req.socket.remoteAddress ||
|
||||||
|
req.connection.remoteAddress}][${(diff[0] * NS_PER_SEC + diff[1]) /
|
||||||
|
1000000}]`,
|
||||||
|
decodeURI_catch(req.url),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeURI_catch(url: string) {
|
||||||
|
try {
|
||||||
|
return decodeURI(url);
|
||||||
|
} catch (error) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user