You've already forked pigallery2
mirror of
https://github.com/bpatrik/pigallery2.git
synced 2025-12-07 23:23:49 +02:00
improving logging
This commit is contained in:
46
backend/routes/LoggerRouter.ts
Normal file
46
backend/routes/LoggerRouter.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import {NextFunction, Request, Response} from "express";
|
||||
import {Logger} from "../Logger";
|
||||
|
||||
export class LoggerRouter {
|
||||
public static route(app: any) {
|
||||
|
||||
app.get("/api*", (req: Request, res: Response, next: NextFunction) => {
|
||||
req['_startTime'] = Date.now();
|
||||
req['logged'] = true;
|
||||
const end = res.end;
|
||||
res.end = (a?: any, b?: any, c?: any) => {
|
||||
res.end = end;
|
||||
res.end(a, b, c);
|
||||
Logger.verbose(req.method, req.url, res.statusCode, (Date.now() - req['_startTime']) + "ms");
|
||||
};
|
||||
return next();
|
||||
});
|
||||
|
||||
app.get("/node_modules*", (req: Request, res: Response, next: NextFunction) => {
|
||||
req['_startTime'] = Date.now();
|
||||
req['logged'] = true;
|
||||
const end = res.end;
|
||||
res.end = (a?: any, b?: any, c?: any) => {
|
||||
res.end = end;
|
||||
res.end(a, b, c);
|
||||
Logger.silly(req.method, req.url, res.statusCode, (Date.now() - req['_startTime']) + "ms");
|
||||
};
|
||||
return next();
|
||||
});
|
||||
|
||||
app.use((req: Request, res: Response, next: NextFunction) => {
|
||||
if (req['logged'] == true) {
|
||||
return next();
|
||||
}
|
||||
req['_startTime'] = Date.now();
|
||||
const end = res.end;
|
||||
res.end = (a?: any, b?: any, c?: any) => {
|
||||
res.end = end;
|
||||
res.end(a, b, c);
|
||||
Logger.debug(req.method, req.url, res.statusCode, (Date.now() - req['_startTime']) + "ms");
|
||||
};
|
||||
return next();
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user