Skip to content

Logger in New Platform should expose a factory method #39695

Closed

Description

In the New platform, LoggerFactory can create Logger, but Logger cannot create a new Logger that extend parents context.
When you want to create several loggers in one context you either have to pass several different Logger instances or pass both Logger and LoggerFactory deeper to a children tree and repeat the whole list of context paths down the tree.

class Service/Plugin {
  constructor(private core: CoreContext){
    this.log = core.logger.get('service-name');
  }
  setup(){
    this.log.info('starting...'); // ['service-name']: starting
    new ServiceA(this.core.logger).setup()
  }

class ServiceA {
  constructor(private logger: LoggerFactory){
    this.log = logger.get('service-name', 'service-a') // knows in what context it was created
  }
  setup(){
    this.log.info('stating') // ['service-name', 'service-a']: starting...
    new ServiceB(logger.get('service-name', 'service-a', 'service-b'))
  }
}

// what if ServiceC wants to use ServiceA as well?
class ServiceA {
  constructor(private log, private logger: LoggerFactory){}
  setup(){
    this.log.info('stating') // ['service-name', 'service-a']: starting...
    new ServiceB(logger.get('service-b')) // now Service B doesn't know in what context it was created
  }
}

To solve the problem we can extend Logger to provide a Factory method that creates new logger bound to a parent context. Something like:

get(...contextPaths) => new Logger(...this.contextPaths,...contextPaths);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

Feature:New PlatformTeam:CoreCore services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etcchore

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions