Skip to content

Commit 1b0dcb5

Browse files
author
Zoltan Tarcsay
committed
makes JsRestfulRegistry configurable with a custom logger
1 parent 191e9b7 commit 1b0dcb5

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/express/registry.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@ import * as winston from 'winston';
66
import * as namings from './namings';
77
import { ExpressContextType, ISecurityContextFactory } from './descriptions';
88

9+
export interface JsRestfulRegistryConfig {
10+
logger?: winston.Winston | winston.LoggerInstance;
11+
}
12+
913
export class JsRestfulRegistry {
1014

1115
private registeredServices:any[] = [];
1216
private securityContextFactory:ISecurityContextFactory = null;
1317

14-
constructor(private app:express.Application){}
18+
constructor(private app:express.Application, private config: JsRestfulRegistryConfig = {}){
19+
if (!config.logger) {
20+
config.logger = winston;
21+
}
22+
}
1523

1624
registerSecurityContextFactory(factory:ISecurityContextFactory){
1725
this.securityContextFactory = factory;
@@ -35,7 +43,7 @@ export class JsRestfulRegistry {
3543
}
3644
}
3745

38-
winston.log('info', `${service.constructor.name} will be registered`);
46+
this.config.logger.info(`${service.constructor.name} will be registered`);
3947
// store the service at the app
4048
this.registeredServices.push(service.constructor.name);
4149

@@ -48,7 +56,7 @@ export class JsRestfulRegistry {
4856

4957
let path = method.path ? method.path : '/';
5058

51-
winston.log('info', `register method ${method.methodName} for path ${path}`);
59+
this.config.logger.info(`register method ${method.methodName} for path ${path}`);
5260

5361
router[httpMethodName](path, (req: express.Request, res: express.Response, next: express.NextFunction) => {
5462
try{
@@ -106,7 +114,7 @@ export class JsRestfulRegistry {
106114

107115
let basePath = pathUtil.getPathFromString(descriptions.basePath);
108116
this.app.use(basePath, router);
109-
winston.log('info', `${service.constructor.name} published at ${basePath}`);
117+
this.config.logger.info(`${service.constructor.name} published at ${basePath}`);
110118
}
111119

112120
collectAndConvertArgs(req:express.Request, res: express.Response, next:express.NextFunction, service:Object, method:MethodDescription): any[]{

src/express/service-registry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as express from "express";
2-
import { JsRestfulRegistry } from './registry';
2+
import { JsRestfulRegistry, JsRestfulRegistryConfig } from './registry';
33
import { ISecurityContext } from 'js-restful';
44
import { ISecurityContextFactory } from './descriptions';
55

66
export class ExpressServiceRegistry {
77

8-
private static initJsResutfulRegistry(app: express.Application){
9-
app.locals.jsResutfulRegistry = app.locals.jsResutfulRegistry || new JsRestfulRegistry(app);
8+
private static initJsResutfulRegistry(app: express.Application, config?: JsRestfulRegistryConfig){
9+
app.locals.jsResutfulRegistry = app.locals.jsResutfulRegistry || new JsRestfulRegistry(app, config);
1010
}
1111

1212
static registerSecurityContextFactory(app: express.Application, factory:ISecurityContextFactory){

0 commit comments

Comments
 (0)