michaelmoussa / zend-psr-log
PSR3-compliant, backwards-compatible, drop-in replacement for Zend\Log\Logger.
Installs: 6 482
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ^7.0
- psr/log: ~1.0
- zendframework/zend-log: ~2.1
- zendframework/zend-modulemanager: ~2.1
- zendframework/zend-mvc: ~2.1
Requires (Dev)
- mockery/mockery: ~0.9
- phpdocumentor/phpdocumentor: ~2.7
- phpunit/phpunit: ~4.3
- squizlabs/php_codesniffer: ~1.5
- zendframework/zendframework: ~2.1
This package is not auto-updated.
Last update: 2023-12-06 09:54:01 UTC
README
zend-psr-log
Overview
A number of PHP libraries accept a Psr\Log\LoggerInterface
for logging messages. Unfortunately, applications using
Zend Framework 2 cannot provide their existing Zend\Log\Logger
loggers, as
they don't comply with PSR-3.
In fact, the two interfaces are not compatible, as they declare some common methods with different signatures. For example: Zend alert vs. Psr alert.
This library serves as a backwards-compatible replacement for Zend\Log\Logger
instances that provides a
Psr\Log-compliant interface to the underlying Zend\Log\Logger
instance. The
result is that you can provide either logger depending on the interface required by a given context, while still
having all messages go through Zend\Log\Logger
and benefiting from its robustness.
Installation
The only supported method of installation is Composer.
composer require "michaelmoussa/zend-psr-log"
Configuration
To begin, you must add the ZendPsrLog
module to your application.config.php
module list as with any ZF2 application.
return [ 'modules' => [ ..., 'ZendPsrLog', ... ], ... ];
Further configuration depends on how you are presently getting your instances of a Zend\Log\Logger
.
"I define a log
entry in my configuration with Zend\Log\Logger
as the key, and then let Zend\Log\LoggerAbstractServiceFactory
do the work."
Then you're done! ZendPsrLog
adds its own factory for creating Zend\Log\Logger
instances, which will be invoked
before the Abstract Factory.
"I define a log
entry in my configuration with a different key, then let Zend\Log\LoggerAbstractServiceFactory
do the work."
For each key you use, you'll need to add a 'My\Key\Here' => new \ZendPsrLog\LoggerFactory('My\Key\Here')
entry to
your service manager config.
"I define a log key in my configuration with no key, and then have my own 'Zend\Log\Logger' => '...'
factory definition in the service manager config"
You need to replace your 'Zend\Log\Logger' => '...'
entry with
'Zend\Log\Logger' => new \ZendPsrLog\LoggerFactory(null)
, as your definition will override the one done by the
ZendPsrLog\LoggerFactory
.
If your custom factory is doing some special logic, you will have to extend ZendPsrLog\LoggerFactory
on your own to
ensure it persists.
"I use new \Zend\Log\Logger(...)
."
- Please don't. :) You should be using the
ServiceManager
. - Replace
new \Zend\Log\Logger
withnew \ZendPsrLog\Logger
.
Usage
As a Zend\Log\LoggerInterface
The ZendPsrLog\Logger
is an extension of Zend\Log\Logger
, so any class you have in your application that is
presently using a Zend\Log\Logger
can use the ZendPsrLog\Logger
without any additional configuration.
As a Psr\Log\LoggerInterface
Suppose you have a class that requires a Psr\Log\LoggerInterface
instance:
use Psr\Log\LoggerInterface; class Foo { public function __construct(LoggerInterface $logger) { ... } }
And you obtain an instance of the ZendPsrLog\Logger
:
/** @var \ZendPsrLog\Logger $logger */ $logger = $serviceManager->get('Zend\Log\Logger');
Just use the ->getPsrLogger()
method to obtain a Psr\Log\LoggerInterface
to your existing Zend\Log\Logger
:
$foo = new Foo($logger->getPsrLogger());