1

Is it possible to fetch one of configured services (in my case - this is logger service) in non-service class?

I have some model-like classes, which doesn't need to be registered as services. But I would like to use logger inside them - logger with the same config I've setup for whole app (mostly for development purposes).

Is there any way to achieve this, other than registering those classes as services or hacking global $kernel?

4
  • 1
    I would highly discourage this as the models and model-like classes should only be related to managing entities. Now the only way I would imagine this possible is to pass the configured service to a model constructor directly but I could imagine a number of things that could go wrong. If it's in fact just a model-like class that doesn't really manage the entities in the Symfony/Doctrine way, the preferred way is to register it as a service. Commented Jun 19, 2018 at 20:54
  • I had similar thoughts and problem a few months ago. Well, if you need it temporary for debugging purposes, you can inject the service inside rep manually with setter, but it's not recommended. The right way would be to log from service where you receive result of repository function. And maybe you can even do it from some kind of subscriber or listener. Commented Jun 19, 2018 at 21:02
  • @domagoj03 yes, I understand your point (also passing logger service in constructor is no opt for me). But in this particular case I need logger. Just logger, with quite complicated config I've setup. I do not want to configure it in every class I need it. For now the most acceptable way is to register those classes as services, but I just don't like this approach (making something a service just to get access to another service). Commented Jun 19, 2018 at 21:06
  • @revengeance thanks, I suppose logging from service where results are obtained sounds as the best solution for now (and now I wonder how could I not get such simple idea :) ) Commented Jun 19, 2018 at 21:27

1 Answer 1

1

You can use StofDoctrineExtensionsBundle for logging changes of your entities to database. Read more

Also you can define Doctrine Event Listener and saving log into your own format (to database or to log file) Read more

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.