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?
If you suggest using a logger, it is up to the developer using your package to make use of that!
I'd suggest you depend on the PSR-3 logger package to allow for easy integration, and let the developer do the rest. No magically using a logger that you think is installed! That
Psr\Log\LoggerAwareInterface
is there for a reason.