-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
enhancementNew feature or requestNew feature or request
Description
I would like to implement context based logger service, this logger need context name. Logger constructo may take context name from a class which will use this logger. Tipically loggers are initialized in constructor of a class. With DIService it may be initialized before class instance created.
To be implemented
SERVICE_REQUIRING - symbol, should be substituted by the class-object (not an instance) which caused current class initialization by requirement, other words a class-object which declare current class as a dependency by SERVICE_REQUIRE. See also example below.
NOTE: SERVICE_MULTIPLE must be required if SERVICE_REQUIRING appears in dependency list
Examples
Custom service which requiring for the logger service
class CustomService {
static [SERVICE_REQUIRE] = [ServiceLogger];
static [LOGGER_CONTEXT] = 'CUSTOM';
#logger;
constructor(logger) {
this.#logger = logger;
}
action() {
this.#logger.log('Action performed'); // => 'CUSTOM Action performed' in console
}
}Logger service
const LOGGER_CONTEXT = Symbol();
class ServiceLogger {
static [SERVICE_REQUIRE] = [SERVICE_REQUIRING];
static [SERVICE_MULTIPLE] = true;
#contextName;
constructor(requiringClz) {
this.#contextName = requiringClz[LOGGER_CONTEXT] ?? requiringClz.name;
console.log(requiringClz === CustomService) // => true
}
log(message) {
console.log(this.#contextName, message);
}
}Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request