0

I am trying to write my own messages to the log in Symfony 2.3, from anywhere, and not just the Controller (which I realize you can just do a "$this->get('logger')".

I've seen that in Symfony 1 you can use sfContext, but that class no longer seems to be a viable choice in 2.3.

Any help is appreciated.

1
  • You should register your service with Monolog as a dependency. Commented Aug 13, 2013 at 11:15

1 Answer 1

3

Symfony2 has Service-oriented architecture (http://en.wikipedia.org/wiki/Service-oriented_architecture) and logger is one of service (by default Monolog). In controller you have access to service via $this->get('service_name'). Here is more info about service container: http://symfony.com/doc/current/book/service_container.html#what-is-a-service-container. If you wanna use logger in another service you have to define service and inject logger service. Example:

# section with defined service in your config.yml file (by default in config.yml)
services:
    # your service name
    my_service:
        # your class name
        class: Fully\Qualified\Loader\Class\Name
        # arguments passed to service constructor. In this case @logger
        arguments: ["@logger"]
        # tags, info: http://symfony.com/doc/current/components/dependency_injection/tags.html
        tags:
            - { name: monolog.logger, channel: acme }

Additionally you should familiarize with dependency injection docs: http://symfony.com/doc/current/components/dependency_injection/index.html

I hope that helped. If not, please let me know where exactly you want to use logger.

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.