2020-04-22 15:52:17 +02:00
|
|
|
import ApplicationComponent from "../ApplicationComponent";
|
|
|
|
import onFinished from "on-finished";
|
2020-11-02 17:48:52 +01:00
|
|
|
import {log} from "../Logger";
|
2020-07-11 11:46:16 +02:00
|
|
|
import {Request, Response, Router} from "express";
|
2020-09-25 23:42:15 +02:00
|
|
|
import {HttpError} from "../HttpError";
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-09-25 22:03:22 +02:00
|
|
|
export default class LogRequestsComponent extends ApplicationComponent {
|
2020-06-14 11:43:00 +02:00
|
|
|
private static fullRequests: boolean = false;
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public static logFullHttpRequests(): void {
|
2020-06-14 11:43:00 +02:00
|
|
|
this.fullRequests = true;
|
2020-11-02 17:48:52 +01:00
|
|
|
log.info('Http requests will be logged with more details.');
|
2020-06-14 11:43:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public static logRequest(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
err?: unknown,
|
|
|
|
additionalStr: string = '',
|
|
|
|
silent: boolean = false,
|
2020-11-02 17:48:52 +01:00
|
|
|
): string | undefined {
|
2020-09-25 23:42:15 +02:00
|
|
|
if (LogRequestsComponent.fullRequests) {
|
2020-06-14 16:04:50 +02:00
|
|
|
const requestObj = JSON.stringify({
|
2020-06-14 11:43:00 +02:00
|
|
|
ip: req.ip,
|
|
|
|
host: req.hostname,
|
|
|
|
method: req.method,
|
|
|
|
url: req.originalUrl,
|
|
|
|
headers: req.headers,
|
|
|
|
query: req.query,
|
|
|
|
params: req.params,
|
|
|
|
body: req.body,
|
2020-06-14 16:04:50 +02:00
|
|
|
files: req.files,
|
2020-06-14 11:43:00 +02:00
|
|
|
cookies: req.cookies,
|
2020-09-25 23:42:15 +02:00
|
|
|
sessionId: req.session?.id,
|
2020-06-14 11:43:00 +02:00
|
|
|
result: {
|
2020-09-25 23:42:15 +02:00
|
|
|
code: res.statusCode,
|
|
|
|
},
|
2020-06-14 16:04:50 +02:00
|
|
|
}, null, 4);
|
2020-06-14 11:43:00 +02:00
|
|
|
if (err) {
|
2020-09-25 23:42:15 +02:00
|
|
|
if (err instanceof Error) {
|
2020-11-02 17:48:52 +01:00
|
|
|
return req.log.error(err, requestObj, err).requestId;
|
2020-09-25 23:42:15 +02:00
|
|
|
} else {
|
2020-11-02 17:48:52 +01:00
|
|
|
return req.log.error(new Error(String(err)), requestObj).requestId;
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|
2020-06-14 11:43:00 +02:00
|
|
|
} else {
|
2020-11-02 17:48:52 +01:00
|
|
|
req.log.info(requestObj);
|
2020-06-14 11:43:00 +02:00
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
} else {
|
|
|
|
let logStr = `${req.method} ${req.originalUrl} - ${res.statusCode}`;
|
|
|
|
if (err) {
|
|
|
|
if (err instanceof Error) {
|
|
|
|
if (silent) {
|
|
|
|
if (err instanceof HttpError) logStr += ` ${err.errorCode}`;
|
|
|
|
logStr += ` ${err.name}`;
|
2020-11-02 17:48:52 +01:00
|
|
|
return req.log.error(err, logStr, additionalStr).requestId;
|
2020-09-25 23:42:15 +02:00
|
|
|
} else {
|
2020-11-02 17:48:52 +01:00
|
|
|
return req.log.error(err, logStr, additionalStr, err).requestId;
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-02 17:48:52 +01:00
|
|
|
return req.log.error(new Error(String(err)), logStr).requestId;
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-02 17:48:52 +01:00
|
|
|
req.log.info(logStr);
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|
2020-06-14 11:43:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-07-11 11:46:16 +02:00
|
|
|
public async init(router: Router): Promise<void> {
|
2020-04-22 15:52:17 +02:00
|
|
|
router.use((req, res, next) => {
|
|
|
|
onFinished(res, (err) => {
|
|
|
|
if (!err) {
|
2020-06-14 11:43:00 +02:00
|
|
|
LogRequestsComponent.logRequest(req, res);
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|