UPDATED EXPLANATION:
I'm fairly new to TypeScript, so please bear with me if this question seems basic. I'm working with an existing library (ngx-logger) that I don't want to or can't modify. My goal is to create a service that generates a logger object configured to a specific logging level:
public getLogger(name: string): NGXLogger {
// Check if the logger is defined, then create a new one and update its configuration
if (name in this.logCfg['loggers']) {
// Create a deep copy of the root logger to avoid affecting other loggers
let logger = _.cloneDeep(this.rootLogger);
let level = this.getLevel(this.logCfg['loggers'][name]['level']);
let config = this.rootLogger.getConfigSnapshot();
config.level = level;
// THIS IS WHAT I AM TRYING TO DO
config.name = name;
logger.updateConfig(config);
return logger;
}
// If not found, use the root logger
return this.rootLogger;
}
The property this.logCfg is a JavaScript object containing the names and levels of some crucial components of the application. The structure looks like this:
{
"loggers": {
"DatasetListComponent":{
"level": "INFO"
},
"DspService":{
"level": "WARN"
},
"UserPreferencesLayoutFormComponent":{
"level": "WARN"
},
"RoleListComponent":{
"level": "DEBUG"
},
"SnapFileListComponent":{
"level": "TRACE"
},
"SnapFileService":{
"level": "TRACE"
}
}
Having the ability to set different verbosity levels will help in debugging issues efficiently. Each of the classes mentioned above will utilize the service to get their own logger configured to the appropriate level using the
this.service.getLogger(this.constructor.name);
method.
I want to include the logger's name in the config object, which is an interface. However, extending the interface would result in the line "logger.updateConfig(config)" throwing a type error, correct?
The reason for wanting to store the logger's name is to include that information in the metadata printed every time the debug|info|warn|error methods are called. Hopefully, this explanation clarifies things further.