I'm developing a PHP package (distributed on Composer) that could benefit from logging, but doesn't require it. I've seen many sample composer.json files that include suggested packages like so:
{
"suggest": {
"monolog/monolog": "Allows more advanced logging of the application flow"
}
}
How would I go about detecting if the library is available at runtime? I want to instantiate a default instance of the \Monolog\Logger if it is available, allow the consumer of the package to pass in their own Psr\Log\LoggerInterface interface implementation if they desire. Are there any best practices around this?
I would advise to create custom package that would include all monolog-related logic. If someone would like to use it, he can include it and it works.
Optional dependency is code smell and magic, that is easy to hide. For deeper explanation, checkout There no such thing as optional dependency article.