Documentation
Logging dependency injection messages
Logging dependency injection messages
Use your own logger implementation to control DI warnings and diagnostics.
By default, we are using console to output some information messages like deprecations.
You can override our default logger and use your desired logger service.
import winston from 'winston'
let container = new ContainerBuilder()
container.logger = winston
Example made with WinstonJS but feel free to use your desired logger library or your own logger service
Your logger instance MUST implement warn method
The info and debug methods are optional. If your logger does not implement them, those messages will be silently skipped.
interface Logger {
warn(message?: any, ...optionalParams: any[]): void;
info?(message?: any, ...optionalParams: any[]): void;
debug?(message?: any, ...optionalParams: any[]): void;
}
Verbosity Levels
You can control how much information the container outputs by setting the verbosity property:
import {ContainerBuilder} from 'node-dependency-injection'
let container = new ContainerBuilder()
container.verbosity = 'debug' // 'silent' | 'warn' | 'info' | 'debug'
| Level | Description |
|---|---|
silent | No output at all |
warn | Only warnings: deprecations, unresolved dependencies (default) |
info | Compilation phases, file loading, autowire scan start/end |
debug | Full detail: every service instantiated, every file skipped, every compiler pass executed |
Each level includes all messages from levels below it (e.g. debug includes info + warn).
Default behavior
The default verbosity is warn, which preserves the same behavior as previous versions. No changes are needed for existing code.
Debugging Autowire issues
When autowiring is not resolving a dependency as expected, set verbosity = 'debug' to see exactly what happens during the scan:
let container = new ContainerBuilder(false, '/path/to/src')
container.verbosity = 'debug'
container.logger = console // or your preferred logger
const autowire = new Autowire(container)
await autowire.process()
await container.compile()
This will output messages such as:
Autowire: skipping non-TypeScript file: /path/to/file.jsAutowire: no default export class found in: /path/to/SomeFile.tsAutowire: could not resolve dependency "SomeInterface" for service MyService. Ensure the import exists and points to a valid file.Autowire: failed to create definition for MyService: Cannot read properties...Autowire: could not resolve tsconfig path alias for "Foo" (import: "@app/foo")
Silent mode
If you want to suppress all logging (e.g. in production):
container.verbosity = 'silent'
