Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A basic logger middleware for InversifyJS

License

NotificationsYou must be signed in to change notification settings

inversify/inversify-logger-middleware

Repository files navigation

Join the chat at https://gitter.im/inversify/InversifyJSBuild StatusTest Coveragenpm versionDependenciesimgimgKnown Vulnerabilities

NPMNPM

A console logger middleware forInversifyJS.

Installation

You can installinversify-logger-middleware using npm:

npm install inversify inversify-logger-middleware reflect-metadata --save

Theinversify-logger-middleware type definitions are included in the npm module and require TypeScript 2.0.Please refer to theInversifyJS documentation to learn more about the installation process.

Motivation

Lets imagine that we have already configured an InversifyJS Container and the logger middleware using the fillowing bindings:

letmodule=newContainerModule((bind:inversify.interfaces.Bind)=>{bind<Weapon>("Weapon").to(Katana).whenInjectedInto(Samurai);bind<Weapon>("Weapon").to(Shuriken).whenInjectedInto(Ninja);bind<Warrior>("Warrior").to(Samurai).whenTargetTagged("canSneak",false);bind<Warrior>("Warrior").to(Ninja).whenTargetTagged("canSneak",true);});

This middleware will display the InversifyJS resolution plan in console in the following format.

//  container.getTagged<Warrior>("Warrior", "canSneak", true);SUCCESS:0.41ms.└── Request :0└── serviceIdentifier :Warrior└──bindings└──Binding<Warrior> :0                └──type :Instance└── implementationType :Ninja                └──scope :Transient└──target└── serviceIdentifier :Warrior            └──name :undefined└──metadata                └──Metadata :0                    └──key :canSneak                    └──value :true└──childRequests            └──Request :0└── serviceIdentifier :Weapon└──bindings└──Binding<Weapon> :0                        └──type :Instance└── implementationType :Shuriken                        └──scope :Transient└──target└── serviceIdentifier :Weapon                    └──name :shuriken└──metadata                        └──Metadata :0                            └──key :name                            └──value :shuriken                        └──Metadata :1                            └──key :inject                            └──value :Weapon

You can configure which elements of the resolution plan are being displayed.

This kind of information can help you during the development of applications with InersifyJS.

Default settings and renderer

You can create a logger using the default settings as follows:

import{makeLoggerMiddleware}from'inversify-logger-middleware';letlogger=makeLoggerMiddleware();

The default options are the following:

letdeatultOptions:LoggerSettings={request:{bindings:{activated:false,cache:false,constraint:false,dynamicValue:false,factory:false,implementationType:true,onActivation:false,provider:false,scope:false,serviceIdentifier:false,type:false},serviceIdentifier:true,target:{metadata:true,name:false,serviceIdentifier:false}},time:true};

You can use the option to determine which elements of the resolution plan should be logged.

The default renderer look as follows:

functionconsoleRenderer(out:string){console.log(out);}

Custom settings and renderer

The following code snippet uses custom settings and a string renderer instead of the default console renderer.

letoptions:LoggerSettings={request:{serviceIdentifier:true,bindings:{scope:true},result:true}};// Takes object (loggerOutput) instead of primitive (string) to share referenceletmakeStringRenderer=function(loggerOutput:{content:string}){returnfunction(out:string){loggerOutput.content=out;};};letloggerOutput={content :""};letstringRenderer=makeStringRenderer(loggerOutput);letlogger=makeLoggerMiddleware(null,stringRenderer);

Applying the middleware

You can apply the logger middlerare using the InversifyJS API:

letcontainer=newContainer();letlogger=makeLoggerMiddleware();container.applyMiddleware(logger);

Please refere to theInversifyJS documentationto learn more about middleware.

Demo app

A sample application can be found at theinversify-code-samples repository.


[8]ページ先頭

©2009-2025 Movatter.jp