Add logging full http requests cli arg
This commit is contained in:
parent
e2cb322001
commit
e965303777
@ -8,6 +8,7 @@ import Controller from "./Controller";
|
||||
import MysqlConnectionManager from "./db/MysqlConnectionManager";
|
||||
import Migration from "./db/Migration";
|
||||
import {Type} from "./Utils";
|
||||
import LogRequestsComponent from "./components/LogRequestsComponent";
|
||||
import TemplateError = lib.TemplateError;
|
||||
|
||||
export default abstract class Application {
|
||||
@ -68,15 +69,7 @@ export default abstract class Application {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
let errorID: string;
|
||||
|
||||
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 errorID: string = LogRequestsComponent.logRequest(req, res, err, '500 Internal Error', err instanceof BadRequestError || err instanceof ServiceUnavailableHttpError);
|
||||
|
||||
let httpError: HttpError;
|
||||
|
||||
@ -131,6 +124,9 @@ export default abstract class Application {
|
||||
case '--verbose':
|
||||
Logger.verbose();
|
||||
break;
|
||||
case '--full-http-requests':
|
||||
LogRequestsComponent.logFullHttpRequests();
|
||||
break;
|
||||
case 'migration':
|
||||
await MysqlConnectionManager.migrationCommand(args.slice(i + 1));
|
||||
return true;
|
||||
|
@ -9,6 +9,7 @@ export default class Logger {
|
||||
public static verbose() {
|
||||
this.logLevel = <LogLevelKeys>LogLevel[LogLevel[this.logLevel] + 1] || this.logLevel;
|
||||
this.dbLogLevel = <LogLevelKeys>LogLevel[LogLevel[this.dbLogLevel] + 1] || this.dbLogLevel;
|
||||
Logger.info('Verbose mode');
|
||||
}
|
||||
|
||||
public static silentError(error: Error, ...message: any[]): string {
|
||||
|
@ -1,14 +1,60 @@
|
||||
import ApplicationComponent from "../ApplicationComponent";
|
||||
import onFinished from "on-finished";
|
||||
import Logger from "../Logger";
|
||||
import {Express, Router} from "express";
|
||||
import {Express, Request, Response, Router} from "express";
|
||||
|
||||
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> {
|
||||
router.use((req, res, next) => {
|
||||
onFinished(res, (err) => {
|
||||
if (!err) {
|
||||
Logger.info(`${req.method} ${req.originalUrl} Accept: ${req.accepts()} - ${res.statusCode}`);
|
||||
LogRequestsComponent.logRequest(req, res);
|
||||
}
|
||||
});
|
||||
next();
|
||||
|
Loading…
Reference in New Issue
Block a user