-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BC-8559 implement request logging in nestjs
- Loading branch information
Showing
12 changed files
with
105 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { Logger } from '@src/core/logger'; | ||
import { RequestLoggable } from '@src/apps/helpers/request-loggable'; | ||
import { Configuration } from '@hpi-schul-cloud/commons/lib'; | ||
|
||
export const createAppLoggerMiddleware = ( | ||
logger: Logger | ||
): ((request: Request, response: Response, next: NextFunction) => void) => { | ||
logger.setContext('AppLoggerMiddleware'); | ||
const enabled = Configuration.get('REQUEST_LOGGING_ENABLED') as boolean; | ||
|
||
return (request: Request, response: Response, next: NextFunction): void => { | ||
if (enabled) { | ||
const startAt = process.hrtime(); | ||
const { method, originalUrl } = request; | ||
|
||
response.on('finish', () => { | ||
const { statusCode } = response; | ||
const contentLength = response.get('content-length') || 'unknown'; | ||
const diff = process.hrtime(startAt); | ||
const responseTime = diff[0] * 1e3 + diff[1] * 1e-6; | ||
logger.info(new RequestLoggable({ method, originalUrl, statusCode, responseTime, contentLength })); | ||
}); | ||
} | ||
|
||
next(); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { Injectable, NestMiddleware, Logger } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class LoggerMiddleware implements NestMiddleware { | ||
private logger = new Logger('HTTP'); | ||
|
||
public use(request: Request, response: Response, next: NextFunction): void { | ||
const startAt = process.hrtime(); | ||
const { method, originalUrl } = request; | ||
|
||
response.on('finish', () => { | ||
const { statusCode } = response; | ||
const contentLength = response.get('content-length') || 'unknown'; | ||
const diff = process.hrtime(startAt); | ||
const responseTime = diff[0] * 1e3 + diff[1] * 1e-6; | ||
this.logger.log(`${method} ${originalUrl} ${statusCode} ${responseTime}ms ${contentLength}`); | ||
}); | ||
|
||
next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Loggable, LogMessage } from '@src/core/logger'; | ||
|
||
interface RequestLogInfo { | ||
method: string; | ||
originalUrl: string; | ||
statusCode: number; | ||
responseTime: number; | ||
contentLength: string; | ||
} | ||
|
||
export class RequestLoggable implements Loggable { | ||
constructor(private readonly requestLog: RequestLogInfo) {} | ||
|
||
public getLogMessage(): LogMessage { | ||
return { | ||
message: `${this.requestLog.method} ${this.requestLog.originalUrl} ${this.requestLog.statusCode} ${this.requestLog.responseTime}ms ${this.requestLog.contentLength}`, | ||
}; | ||
// return { | ||
// message: 'loggables rock', | ||
// data: { | ||
// method: this.requestLog.method, | ||
// originalUrl: this.requestLog.originalUrl, | ||
// statusCode: this.requestLog.statusCode, | ||
// responseTime: `${this.requestLog.responseTime}ms`, | ||
// contentLength: this.requestLog.contentLength, | ||
// }, | ||
// }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters