json-logger-service
Advanced tools
Comparing version 1.0.14 to 1.0.15
@@ -22,3 +22,3 @@ "use strict"; | ||
beforeAll(() => __awaiter(this, void 0, void 0, function* () { | ||
requestLogger = new index_1.RequestLogger(); | ||
requestLogger = index_1.RequestLogger.buildExpressRequestLogger(); | ||
nextCalled = false; | ||
@@ -28,3 +28,3 @@ })); | ||
beforeEach(() => { | ||
requestLogger.logExpressRequest(undefined, undefined, nextFunction); | ||
requestLogger(undefined, undefined, nextFunction); | ||
}); | ||
@@ -37,3 +37,3 @@ it('Should return without failing', () => { | ||
beforeEach(() => { | ||
requestLogger.logExpressRequest({ path: undefined }, undefined, nextFunction); | ||
requestLogger({ path: undefined }, undefined, nextFunction); | ||
}); | ||
@@ -46,3 +46,3 @@ it('Should return without failing', () => { | ||
beforeEach(() => { | ||
requestLogger.logExpressRequest({ path: '/mypath' }, responseMock, nextFunction); | ||
requestLogger({ path: '/mypath' }, responseMock, nextFunction); | ||
}); | ||
@@ -55,3 +55,3 @@ it('Should return without failing', () => { | ||
beforeEach(() => { | ||
requestLogger.logExpressRequest({ path: '/mypath', method: 'GET' }, responseMock, nextFunction); | ||
requestLogger({ path: '/mypath', method: 'GET' }, responseMock, nextFunction); | ||
}); | ||
@@ -64,4 +64,4 @@ it('Should return without failing', () => { | ||
beforeEach(() => { | ||
requestLogger = new index_1.RequestLogger(['/mypath/do-not-log']); | ||
requestLogger.logExpressRequest({ path: '/mypath/do-not-log/customerEmail@gmail.com', method: 'GET' }, responseMock, nextFunction); | ||
requestLogger = index_1.RequestLogger.buildExpressRequestLogger(['/mypath/do-not-log']); | ||
requestLogger({ path: '/mypath/do-not-log/customerEmail@gmail.com', method: 'GET' }, responseMock, nextFunction); | ||
}); | ||
@@ -68,0 +68,0 @@ it('Should return without failing', () => { |
export declare class RequestLogger { | ||
private readonly omitBasePaths?; | ||
private static readonly LOGGER; | ||
constructor(omitBasePaths?: string[]); | ||
logExpressRequest(req: any, res: any, next: any): void; | ||
private isPathOmitted; | ||
static buildExpressRequestLogger(omitBasePaths?: string[]): any; | ||
private static isPathOmitted; | ||
} |
@@ -7,25 +7,24 @@ "use strict"; | ||
class RequestLogger { | ||
constructor(omitBasePaths) { | ||
this.omitBasePaths = omitBasePaths; | ||
if (!this.omitBasePaths) { | ||
this.omitBasePaths = []; | ||
static buildExpressRequestLogger(omitBasePaths) { | ||
if (!omitBasePaths) { | ||
omitBasePaths = []; | ||
} | ||
} | ||
logExpressRequest(req, res, next) { | ||
if (!(req && req.path)) { | ||
RequestLogger.LOGGER.warn('No request path defined.'); | ||
return (req, res, next) => { | ||
if (!(req && req.path)) { | ||
RequestLogger.LOGGER.warn('No request path defined.'); | ||
next(); | ||
return; | ||
} | ||
const requestPath = RequestLogger.isPathOmitted(req.path, omitBasePaths) | ||
? PATH_OMITTED : req.path; | ||
const method = req.method ? req.method : ''; | ||
RequestLogger.LOGGER.info(`Before request ${method} '${requestPath}'`); | ||
onFinished(res, () => { | ||
RequestLogger.LOGGER.info(`After request ${method} '${requestPath}'`); | ||
}); | ||
next(); | ||
return; | ||
} | ||
const requestPath = this.isPathOmitted(req.path) ? PATH_OMITTED : req.path; | ||
const method = req.method ? req.method : ''; | ||
RequestLogger.LOGGER.info(`Before request ${method} '${requestPath}'`); | ||
onFinished(res, () => { | ||
RequestLogger.LOGGER.info(`After request ${method} '${requestPath}'`); | ||
}); | ||
next(); | ||
return; | ||
}; | ||
} | ||
isPathOmitted(requestPath) { | ||
for (const blackListedPath of this.omitBasePaths) { | ||
static isPathOmitted(requestPath, omitBasePaths) { | ||
for (const blackListedPath of omitBasePaths) { | ||
if (requestPath.startsWith(blackListedPath)) { | ||
@@ -32,0 +31,0 @@ return true; |
{ | ||
"name": "json-logger-service", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"description": "Nest Json LoggerService implementation.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index", |
@@ -58,3 +58,3 @@ # json-logger-service | ||
... | ||
app.use(new RequestLogger().logExpressRequest); | ||
app.use(RequestLogger.buildExpressRequestLogger()); | ||
... | ||
@@ -76,3 +76,3 @@ await app.listen(3000); | ||
```typescript | ||
app.use(new RequestLogger(['/my-path-with-sensible-information']).logExpressRequest); | ||
app.use(RequestLogger.buildExpressRequestLogger(['/my-path-with-sensible-information'])); | ||
``` | ||
@@ -79,0 +79,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30354
355