Skip to content

Commit

Permalink
new psr logger adapter for symfony
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Nov 26, 2014
1 parent da1be38 commit 6e27390
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 1 deletion.
31 changes: 31 additions & 0 deletions WHATS_NEW.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,34 @@ The `project:optimize` task has been fixed.
```bash
php test/bin/coverage.php sfWebRequest
```

Logger
------

You can now integrate a [psr compliant log](https://github.com/php-fig/log). For this you need to set the psr_logger
setting to true and configure a service with the id logger.psr.

```YAML
#In settings.yml
all:
.settings:
psr_logger: true
```
This is a example of service configuration using [monolog](https://github.com/Seldaek/monolog)
```YAML
#In services.yml

all:
services:
logger.psr:
class: Monolog\Logger
arguments: [default]
calls:
- [pushHandler, [@monolog.handler.file]]

monolog.handler.file:
class: Monolog\Handler\StreamHandler
arguments: [/the/file/path/of/your/file.log]
```
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
"require": {
"swiftmailer/swiftmailer": "~5.2.1"
},
"require-dev": {
"psr/log": "*"
},
"type": "library",
"autoload": {
"files": ["autoload.php"]
},
"suggest": {
"lexpress/doctrine1": "Doctrine plugin",
"propel/sf-propel-o-r-m-plugin": "Propel plugin"
"propel/sf-propel-o-r-m-plugin": "Propel plugin",
"psr/log": "Psr logger"
},
"extra": {
"branch-alias": {
Expand Down
3 changes: 3 additions & 0 deletions lib/autoload/sfCoreAutoload.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,13 @@ static public function make()
'sfloggerinterface' => 'log/sfLoggerInterface.class.php',
'sfloggerwrapper' => 'log/sfLoggerWrapper.class.php',
'sfnologger' => 'log/sfNoLogger.class.php',
'sfpsrloggeradapter' => 'log/sfPsrLoggerAdapter.class.php',
'sfstreamlogger' => 'log/sfStreamLogger.class.php',
'sfvarlogger' => 'log/sfVarLogger.class.php',
'sfwebdebuglogger' => 'log/sfWebDebugLogger.class.php',
'sfmailer' => 'mailer/sfMailer.class.php',
'sfmailermessageloggerplugin' => 'mailer/sfMailerMessageLoggerPlugin.class.php',
'sfnomailer' => 'mailer/sfNoMailer.class.php',
'sfpearconfig' => 'plugin/sfPearConfig.class.php',
'sfpeardownloader' => 'plugin/sfPearDownloader.class.php',
'sfpearenvironment' => 'plugin/sfPearEnvironment.class.php',
Expand Down Expand Up @@ -433,6 +435,7 @@ static public function make()
'sftestbasetask' => 'task/test/sfTestBaseTask.class.php',
'sftestcoveragetask' => 'task/test/sfTestCoverageTask.class.php',
'sftestfunctionaltask' => 'task/test/sfTestFunctionalTask.class.php',
'sftestplugintask' => 'task/test/sfTestPluginTask.class.php',
'sftestunittask' => 'task/test/sfTestUnitTask.class.php',
'sftestbrowser' => 'test/sfTestBrowser.class.php',
'sftestfunctional' => 'test/sfTestFunctional.class.php',
Expand Down
6 changes: 6 additions & 0 deletions lib/config/config/factories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ default:
param:
level: debug
file: %SF_LOG_DIR%/%SF_APP%_%SF_ENVIRONMENT%.log
sf_psr_logger:
class: sfPsrLoggerAdapter
param:
logger_service_id: logger.psr
auto_connect: true
condition: %SF_PSR_LOGGER%

mailer:
class: sfMailer
Expand Down
1 change: 1 addition & 0 deletions lib/config/config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ default:

# Logging
logging_enabled: true
psr_logger: false

# i18n
default_culture: en # Default user culture
161 changes: 161 additions & 0 deletions lib/log/sfPsrLoggerAdapter.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
/*
* This file is part of the symfony package.
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* sfPsrLoggerAdapter is meant to be able to use a Prs compliant logger in symfony 1.
*
* @see https://github.com/php-fig/log
*
* @package symfony
* @subpackage service
* @author Martin Poirier Theoret <[email protected]>
*/
class sfPsrLoggerAdapter extends sfLogger
{
/**
* Buffer to keep all the log before the psr logger is registered
*
* @var array
*/
private $buffer = array();

/**
* The logger that will the log will be forward to
*
* @var \Psr\Log\LoggerInterface
*/
private $logger;

/**
* The service id that will be use as the psr logger
*
* @var string
*/
private $loggerServiceId = 'logger.psr';

/**
* Initializes this logger.
*
* Available options:
*
* - logger_service_id: The service id to use as the logger. Default: logger.psr
* - auto_connect: If we must connect automatically to the context.load_factories to set the logger. Default: true
*
* @param sfEventDispatcher $dispatcher
* @param array $options
*
* @return void
*/
public function initialize(sfEventDispatcher $dispatcher, $options = array())
{
if (isset($options['logger_service_id']))
{
$this->loggerServiceId = $options['logger_service_id'];
}

if (!isset($options['auto_connect']) || $options['auto_connect'])
{
$dispatcher->connect('context.load_factories', array($this, 'listenContextLoadFactoriesEvent'));
}

parent::initialize($dispatcher, $options);
}

/**
* Listen the context load factories to get the configure service after the service container is available
*
* @param sfEvent $event
*
* @return void
*/
public function listenContextLoadFactoriesEvent(sfEvent $event)
{
$context = $event->getSubject();
/* @var $context sfContext */
$this->setLogger($context->getService($this->loggerServiceId));
$this->dispatcher->disconnect('context.load_factories', array($this, 'listenContextLoadFactoriesEvent'));
}

/**
* Set the logger
*
* @param \Psr\Log\LoggerInterface $logger
*/
public function setLogger(\Psr\Log\LoggerInterface $logger)
{
$this->logger = $logger;
$this->flushBuffer();
}

/**
* Flush the current buffer to the register logger
*
* @return void
*/
public function flushBuffer()
{
if (!$this->logger)
{
$this->buffer = array();
return;
}

foreach ($this->buffer as $log)
{
$this->log($log['message'], $log['priority']);
}

$this->buffer = array();
}

/**
* Logs a message.
*
* @param string $message Message
* @param string $priority Message priority
*
* @return void
*/
public function doLog($message, $priority)
{
if (!$this->logger)
{
$this->buffer[] = compact('message', 'priority');
return;
}

switch ($priority)
{
case sfLogger::EMERG:
$this->logger->emergency($message);
break;
case sfLogger::ALERT:
$this->logger->alert($message);
break;
case sfLogger::CRIT:
$this->logger->critical($message);
break;
case sfLogger::ERR:
$this->logger->error($message);
break;
case sfLogger::WARNING:
$this->logger->warning($message);
break;
case sfLogger::NOTICE:
$this->logger->notice($message);
break;
case sfLogger::INFO:
$this->logger->info($message);
break;
case sfLogger::DEBUG:
$this->logger->debug($message);
break;
}
}
}

0 comments on commit 6e27390

Please sign in to comment.