Skip to content

Commit

Permalink
Merge branch 'dev-logger' into 'dev'
Browse files Browse the repository at this point in the history
add logger (using colors)

See merge request syarhei-apps/bookmaker-office!12
  • Loading branch information
syarhei committed Apr 24, 2018
2 parents 2ffde31 + 7921c9d commit 5974419
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {Authenticator} from "passport";
import {PassportHandler} from "./src/middlewares/PassportHandler";
import {IKey} from "./types/IKey";
import {DBSession} from "./src/DB/DBSession";
import {logRequestResponse} from "./utils/logger";

@injectable()
export class App {
Expand All @@ -33,6 +34,7 @@ export class App {
}

init(): void {
this.app.use(logRequestResponse);
this.app.use(this.dbSession.middleware);
this.app.use(bodyParser.json({}));
this.app.use(bodyParser.urlencoded({ extended: true }));
Expand Down
34 changes: 34 additions & 0 deletions app/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import "colors";
import {NextFunction, Request, Response} from "express";
import {RequestHandler} from "express-serve-static-core";

export function logRequestResponse(req: Request, res: Response, next: NextFunction): RequestHandler {
console.log(`[${req.method.blue.bold}]: ${req.path}`);
const str: string = "";
res.json = new Proxy(res.json, {
apply: (target, thisArg, argArray) => {
const status: string = res.statusCode.toString();
if (status.startsWith("4")) {
console.log(`${" ERROR:".magenta.bold} "${(argArray[0] as Error).message.magenta.bold}"`);
}
if (status.startsWith("5")) {
console.log(`${thisArg[0]}`.cyan.bold);
}
target.apply(thisArg, argArray);
}
});
res.end = new Proxy(res.end, {
apply: ((target, thisArg, argArray) => {
const status: string = res.statusCode.toString();
if (status.startsWith("2") || status.startsWith("3")) {
console.log(` ${"STATUS:".bold} ${res.statusCode.toString().green.bold}`);
} else if (status.startsWith("4") || status.startsWith("5")) {
console.log(` ${"STATUS:".bold} ${res.statusCode.toString().red.bold}`);
}
target.apply(thisArg, argArray);
})
});

next();
return;
}
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"bluebird": "^3.5.1",
"body-parser": "^1.18.2",
"chai": "^4.1.2",
"colors": "^1.2.1",
"express": "^4.16.3",
"express-async-wrap": "^1.0.0",
"express-mysql-session": "^1.3.0",
Expand All @@ -54,6 +55,7 @@
"@types/bluebird": "^3.5.20",
"@types/body-parser": "^1.16.8",
"@types/chai": "^4.1.2",
"@types/color": "^3.0.0",
"@types/express": "^4.11.1",
"@types/express-mysql-session": "^1.2.2",
"@types/express-session": "^1.15.8",
Expand Down

0 comments on commit 5974419

Please sign in to comment.