T
T
topuserman2020-02-24 20:38:25
PHP
topuserman, 2020-02-24 20:38:25

How to properly organize the logger factory structure?

For logging in the project, I chose a psr-3 compatible library - Monolog (the library does not matter to the question).

While I plan to write the logs to files.
In the future, I would like to transfer the logs to elasticsearch.

The Monolog library has a lot of configuration so that you don’t have to configure it every time, I would like to create a Factory that will create a default-configured logger (but at the same time be able to change the config, i.e. have access to the monolog logger object itself).

Also, in the future, I need to be able to change the logger config to write logs not to a file, but to elastic.

How can such a factory be created? I would like to see an example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Philipp, 2020-02-25
@zoonman

This is one example of a situation where the use of a singleton can be partially justified.

class LoggerFactory {
  static $instance = null;
  public static create() {
    if (!is_null(static::$instance)) {
      return static::$instance;
    }
     static::$instance = new Logger('name');
     // ну т.д.
     return static::$instance;  
 }
}

// используется вроде бы так
$logger = LoggerFactory::create();

I haven't checked the code, but the idea is something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question