Merge pull request #375 from TheArcaneBrony/request-logging
Add request logging (with env var: log-requests)
This commit is contained in:
commit
fa9f512c90
BIN
api/package-lock.json
generated
BIN
api/package-lock.json
generated
Binary file not shown.
@ -62,6 +62,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@fosscord/util": "file:../util",
|
||||
"@types/morgan": "^1.9.3",
|
||||
"ajv": "8.6.2",
|
||||
"ajv-formats": "^2.1.1",
|
||||
"amqplib": "^0.8.0",
|
||||
@ -85,6 +86,7 @@
|
||||
"mongoose": "^5.12.3",
|
||||
"mongoose-autopopulate": "^0.12.3",
|
||||
"mongoose-long": "^0.3.2",
|
||||
"morgan": "^1.10.0",
|
||||
"multer": "^1.4.2",
|
||||
"node-fetch": "^2.6.1",
|
||||
"patch-package": "^6.4.7",
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import { OptionsJson } from 'body-parser';
|
||||
import "missing-native-js-functions";
|
||||
import { Connection } from "mongoose";
|
||||
import { Server, ServerOptions } from "lambert-server";
|
||||
@ -11,6 +12,7 @@ import path from "path";
|
||||
import { initRateLimits } from "./middlewares/RateLimit";
|
||||
import TestClient from "./middlewares/TestClient";
|
||||
import { initTranslation } from "./middlewares/Translation";
|
||||
import morgan from "morgan";
|
||||
|
||||
export interface FosscordServerOptions extends ServerOptions {}
|
||||
|
||||
@ -36,6 +38,29 @@ export class FosscordServer extends Server {
|
||||
await Config.init();
|
||||
await initEvent();
|
||||
|
||||
|
||||
/*
|
||||
DOCUMENTATION: uses log-requests environment variable
|
||||
|
||||
# only log 200 and 204
|
||||
log-requests=200 204
|
||||
# log everything except 200 and 204
|
||||
log-requests=-200 204
|
||||
# log all requests
|
||||
log-requests=-
|
||||
*/
|
||||
|
||||
let logRequests = process.env["log-requests"] != undefined;
|
||||
if(logRequests) {
|
||||
this.app.use(morgan("combined", {
|
||||
skip: (req, res) => {
|
||||
var skip = !(process.env["log-requests"]?.includes(res.statusCode.toString()) ?? false);
|
||||
if(process.env["log-requests"]?.charAt(0) == '-') skip = !skip;
|
||||
return skip;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
this.app.use(CORS);
|
||||
this.app.use(BodyParser({ inflate: true, limit: "10mb" }));
|
||||
|
||||
@ -65,6 +90,9 @@ export class FosscordServer extends Server {
|
||||
this.app.use(ErrorHandler);
|
||||
TestClient(this.app);
|
||||
|
||||
if(logRequests){
|
||||
console.log("Warning: Request logging is enabled! This will spam your console!\nTo disable this, unset the 'log-requests' environment variable!");
|
||||
}
|
||||
return super.start();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user