Add logging full http requests cli arg

This commit is contained in:
Alice Gaudon 2020-06-14 11:43:00 +02:00
parent e2cb322001
commit e965303777
3 changed files with 54 additions and 11 deletions

View File

@ -8,6 +8,7 @@ import Controller from "./Controller";
import MysqlConnectionManager from "./db/MysqlConnectionManager"; import MysqlConnectionManager from "./db/MysqlConnectionManager";
import Migration from "./db/Migration"; import Migration from "./db/Migration";
import {Type} from "./Utils"; import {Type} from "./Utils";
import LogRequestsComponent from "./components/LogRequestsComponent";
import TemplateError = lib.TemplateError; import TemplateError = lib.TemplateError;
export default abstract class Application { export default abstract class Application {
@ -68,15 +69,7 @@ export default abstract class Application {
return next(err); return next(err);
} }
let errorID: string; let errorID: string = LogRequestsComponent.logRequest(req, res, err, '500 Internal Error', err instanceof BadRequestError || err instanceof ServiceUnavailableHttpError);
let logStr = `${req.method} ${req.originalUrl} Accept: ${req.accepts()} - `;
if (err instanceof BadRequestError || err instanceof ServiceUnavailableHttpError) {
logStr += `${err.errorCode} ${err.name}`;
errorID = Logger.silentError(err, logStr);
} else {
errorID = Logger.error(err, logStr + `500 Internal Error`, err);
}
let httpError: HttpError; let httpError: HttpError;
@ -131,6 +124,9 @@ export default abstract class Application {
case '--verbose': case '--verbose':
Logger.verbose(); Logger.verbose();
break; break;
case '--full-http-requests':
LogRequestsComponent.logFullHttpRequests();
break;
case 'migration': case 'migration':
await MysqlConnectionManager.migrationCommand(args.slice(i + 1)); await MysqlConnectionManager.migrationCommand(args.slice(i + 1));
return true; return true;

View File

@ -9,6 +9,7 @@ export default class Logger {
public static verbose() { public static verbose() {
this.logLevel = <LogLevelKeys>LogLevel[LogLevel[this.logLevel] + 1] || this.logLevel; this.logLevel = <LogLevelKeys>LogLevel[LogLevel[this.logLevel] + 1] || this.logLevel;
this.dbLogLevel = <LogLevelKeys>LogLevel[LogLevel[this.dbLogLevel] + 1] || this.dbLogLevel; this.dbLogLevel = <LogLevelKeys>LogLevel[LogLevel[this.dbLogLevel] + 1] || this.dbLogLevel;
Logger.info('Verbose mode');
} }
public static silentError(error: Error, ...message: any[]): string { public static silentError(error: Error, ...message: any[]): string {

View File

@ -1,14 +1,60 @@
import ApplicationComponent from "../ApplicationComponent"; import ApplicationComponent from "../ApplicationComponent";
import onFinished from "on-finished"; import onFinished from "on-finished";
import Logger from "../Logger"; import Logger from "../Logger";
import {Express, Router} from "express"; import {Express, Request, Response, Router} from "express";
export default class LogRequestsComponent extends ApplicationComponent<void> { export default class LogRequestsComponent extends ApplicationComponent<void> {
private static fullRequests: boolean = false;
public static logFullHttpRequests() {
this.fullRequests = true;
Logger.info('Http requests will be logged with more details.');
}
public static logRequest(req: Request, res: Response, err: any = null, additionalStr: string = '', silent: boolean = false): string {
if (!LogRequestsComponent.fullRequests) {
let logStr = `${req.method} ${req.originalUrl} - ${res.statusCode}`;
if (err) {
if (silent) {
logStr += `${err.errorCode} ${err.name}`;
return Logger.silentError(err, logStr);
} else {
return Logger.error(err, logStr, additionalStr, err);
}
} else {
Logger.info(logStr);
}
} else {
const requestObj = {
ip: req.ip,
host: req.hostname,
method: req.method,
url: req.originalUrl,
headers: req.headers,
query: req.query,
params: req.params,
body: req.body,
cookies: req.cookies,
sessionId: req.sessionID,
result: {
code: res.statusCode
}
};
if (err) {
return Logger.error(err, requestObj, err);
} else {
Logger.info(requestObj);
}
}
return '';
}
public async start(app: Express, router: Router): Promise<void> { public async start(app: Express, router: Router): Promise<void> {
router.use((req, res, next) => { router.use((req, res, next) => {
onFinished(res, (err) => { onFinished(res, (err) => {
if (!err) { if (!err) {
Logger.info(`${req.method} ${req.originalUrl} Accept: ${req.accepts()} - ${res.statusCode}`); LogRequestsComponent.logRequest(req, res);
} }
}); });
next(); next();