inversify-logger-middleware
A console logger middleware for InversifyJS.
Installation
You can install inversify-logger-middleware
using npm:
npm install inversify inversify-logger-middleware reflect-metadata --save
The inversify-logger-middleware
type definitions are included in the npm module and require TypeScript 2.0.
Please refer to the InversifyJS 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:
;
This middleware will display the InversifyJS resolution plan in console in the following format.
// container.getTagged<Warrior>("Warrior", "canSneak", true); SUCCESS: 041 ms. └── 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 desplayed.
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:
;;
The default options are the following:
;
You can use the option to determine which elements of the resolution plan should be logged.
The default renderer look as follows:
Custom settings and renderer
The following code snippet uses custom settings and a string renderer instead of the default console renderer.
; // Takes object (loggerOutput) instead of primitive (string) to share reference; ;;;
Applying the middleware
You can apply the logger middlerare using the InversifyJS API:
;;container.applyMiddlewarelogger;
Please refere to the InversifyJS documentation to learn more about middleware.
Demo app
A sample application can be found at the inversify-code-samples repository.