Swergas
Swergas

Reputation: 372

Where should I place my Monolog custom Handler in my Symfony2 project?

I have created a handler class that derives from AbstractProcessingHandler. I've seen that I can put it in src/MyNamespace/MyBundle/Monolog/, but it worries me a bit because this handler is used in several others bundles where I log data. So the other bundles will need MyBundle to work properly, only because of this handler.

I tried to put my handler class in lib/ but it does not seem to work (maybe I have to do something special with Autoload?).

Or should I create a new bundle specifically for this handler?

Edit: I can't really place my custom handler class in vendor/monolog/monolog/src/Monolog/Handler because then I would not be able to add it to my git repository: there is a conflict because this folder is managed by another git repository (created by Composer)

Upvotes: 0

Views: 752

Answers (3)

Jacob Thomason
Jacob Thomason

Reputation: 3411

I think the common approach here is to use a "Bridge" dir in your Bundle with a clear dependency. If you have other bundles that rely on this, what we've done is create a ServiceBundle which is basically for all shared services across all bundles within the application. This might not work well for you if you have plans of distributing this bundle, but may otherwise.

Upvotes: 0

Seldaek
Seldaek

Reputation: 42036

On Monolog's end there is really no restriction on where to put it or how you call it. The key is only that it implements monolog's HandlerInterface or extends from one of the existing handlers.

Now it depends what your handler is, if it's generic stuff that other people could use you could submit it as a pull request to monolog.
If not, you can either create an own composer package for it, or put it in src/Acme/Monolog/FooHandler or something like that, so it stays in your application but is clearly out of a bundle. The downside is that you need to configure it as a service in one of your bundles, so you still have some sort of dependency on a bundle there.
Maybe having it as its own bundle would make sense then. But it's quite a lot of boilerplate for just one class.
If all your bundles are application specific and very unlikely to be extracted out of it, having cross-bundles dependencies is fine though IMO.
The dependency is anyway not very strong since one bundle could contain the handler and configure it. The other bundles can still log to monolog, even if the handler isn't present, they can log. It just won't go to that specific handler. Nothing should break.

As you see, it's just a lot of trade-offs, and it's hard to say which solution is the most fitting without knowing more about your project.

Upvotes: 2

Juan Sosa
Juan Sosa

Reputation: 5280

If you want to have your handler class in lib/ you will need to add the lib/ folder to your composer.json autoload section. For example:

"autoload": {
    "psr-0": { "": ["src/", "lib/"] }
}

Take a look at the Composer documentation:

Basic Usage

Autoload

Upvotes: 0

Related Questions