2

I am currently developing a Symfony 2 Application and I want to log when some setters are called in my Models.

As I see, there is no way to inject the logger by default or access it via a static registry in Symfony 2, so my approach is as follows:

I added a static method and property to the base class of my models and set the Logger there. I also added a getter, that is then available in my models.

I set the logger in there via the request kernel event, so the logger is only available after this event.

This solution works but it seems rather hacky to me. Anyone got a better idea how to approach this? A setup method that does not rely on the request kernel event would be nice. A method that does not rely on static properties would be even nicer!

Attention! Adding the logger to the base document may lead to issues with serialization.

Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164
Paul Weber
  • 6,518
  • 3
  • 43
  • 52

2 Answers2

0

I just put it as a second argument to the setter:

Supposing that you have an Entity so a solution ti the following:

//Namespace Declaration above
class SomeEntity 
{
 ....
 private $somefield;

 public function setSomefield($somefield,LoggerService $l)
 {
    $this->somefield=$someField;
    $l->log(""Somefield Has Been Set"");  
 }
}

Note: Instead of LoggerService replace it with the logger class you may have developed and call the appropriate methods the LoggerService is an assumptio I make in order to show the way.

In case you are using Monolog and not some custom LoggingClass:

//Namespace Declaration above
use Symfony\Bridge\Monolog\Logger;

class SomeEntity 
{
 ....
 private $somefield;

 public function setSomefield($somefield,Logger $l)
 {
    $this->somefield=$someField;
    $l->info("Somefield Has Been Set");  
 }
}

I hope it fits your needs. This will help you better to Log the setters without probs with serialization.

Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164
-1

You can use Monolog(official documentation) for logging the activities.Register the logger as a service and that service will be available in the service container.

In app/config.yml, under services, register the logger service and its handler. When registering the handler, specify the log file against arguments. In the example given below, the file name will be either dev.api.log or prod.api.log depending on the environment.

bt.api.logger:
  class: Symfony\Bridge\Monolog\Logger
  arguments: [app]
  calls:
    - [pushHandler, [@bt.api.logger_handler]]

bt.api.logger_handler:
  class: Monolog\Handler\StreamHandler       
  arguments: [%kernel.logs_dir%/%kernel.environment%.api.log, 200]

Once you have registerd the logger service, it can be called in controllers as shown below

$logger = $this->get('bt.api.logger');
$msg = "The setter was called";
$logger->info(msg);
Praveesh
  • 1,257
  • 1
  • 10
  • 23
  • Sorry, but this does not solve my problem. I already have a configured logger, the problem is how to inject the logger into my Database Domain Classes, that are not injectable but newable. AFAIK I cannot access the service container from my domain classes. – Paul Weber Apr 07 '14 at 12:14
  • The only Solution is to define the loger as Service somehow. Only Services get inject3ed – Dimitrios Desyllas Apr 08 '17 at 14:31