From 92ec203f238ea9176cea0f8f3ac839ff489b7d84 Mon Sep 17 00:00:00 2001 From: Braun Patrik Date: Wed, 21 Jun 2017 11:32:56 +0200 Subject: [PATCH] improving logging --- backend/routes/LoggerRouter.ts | 46 ++++++++++++++++++++++++++++++++++ backend/server.ts | 33 ++---------------------- 2 files changed, 48 insertions(+), 31 deletions(-) create mode 100644 backend/routes/LoggerRouter.ts diff --git a/backend/routes/LoggerRouter.ts b/backend/routes/LoggerRouter.ts new file mode 100644 index 00000000..9a9c52ab --- /dev/null +++ b/backend/routes/LoggerRouter.ts @@ -0,0 +1,46 @@ +import {NextFunction, Request, Response} from "express"; +import {Logger} from "../Logger"; + +export class LoggerRouter { + public static route(app: any) { + + app.get("/api*", (req: Request, res: Response, next: NextFunction) => { + req['_startTime'] = Date.now(); + req['logged'] = true; + const end = res.end; + res.end = (a?: any, b?: any, c?: any) => { + res.end = end; + res.end(a, b, c); + Logger.verbose(req.method, req.url, res.statusCode, (Date.now() - req['_startTime']) + "ms"); + }; + return next(); + }); + + app.get("/node_modules*", (req: Request, res: Response, next: NextFunction) => { + req['_startTime'] = Date.now(); + req['logged'] = true; + const end = res.end; + res.end = (a?: any, b?: any, c?: any) => { + res.end = end; + res.end(a, b, c); + Logger.silly(req.method, req.url, res.statusCode, (Date.now() - req['_startTime']) + "ms"); + }; + return next(); + }); + + app.use((req: Request, res: Response, next: NextFunction) => { + if (req['logged'] == true) { + return next(); + } + req['_startTime'] = Date.now(); + const end = res.end; + res.end = (a?: any, b?: any, c?: any) => { + res.end = end; + res.end(a, b, c); + Logger.debug(req.method, req.url, res.statusCode, (Date.now() - req['_startTime']) + "ms"); + }; + return next(); + }); + + } +} diff --git a/backend/server.ts b/backend/server.ts index 6ae31326..b8107e03 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -2,8 +2,6 @@ import * as _express from "express"; import * as _session from "express-session"; import * as _bodyParser from "body-parser"; import * as _http from "http"; -import * as winston from "winston"; -import * as expressWinston from "express-winston"; import {PublicRouter} from "./routes/PublicRouter"; import {UserRouter} from "./routes/UserRouter"; import {GalleryRouter} from "./routes/GalleryRouter"; @@ -14,6 +12,7 @@ import {ObjectManagerRepository} from "./model/ObjectManagerRepository"; import {Logger} from "./Logger"; import {Config} from "../common/config/private/Config"; import {DatabaseType, ThumbnailProcessingLib} from "../common/config/private/IPrivateConfig"; +import {LoggerRouter} from "./routes/LoggerRouter"; const LOG_TAG = "[server]"; export class Server { @@ -32,35 +31,7 @@ export class Server { this.app = _express(); - this.app.use(expressWinston.logger({ - transports: [ - new winston.transports.Console({ - level: 'silly', - json: false, - colorize: true, - timestamp: function () { - return (new Date()).toLocaleString(); - }, - formatter: (options) => { - // Return string will be passed to logger. - return options.timestamp() + '[' + winston['config']['colorize'](options.level, options.level.toUpperCase()) + '] ' + - (undefined !== options.message ? options.message : '') + - (options.meta && Object.keys(options.meta).length ? '\n\t' + JSON.stringify(options.meta) : '' ); - }, - debugStdout: true - }) - ], - meta: false, // optional: control whether you want to log the meta data about the request (default to true) - msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}" - expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true - colorize: true, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red). - level: (req) => { - if (req.url.indexOf("/api/") !== -1) { - return "verbose"; - } - return req.url.indexOf("node_modules") !== -1 ? "silly" : "debug" - } - })); + LoggerRouter.route(this.app); this.app.set('view engine', 'ejs');