Skip to content

Commit

Permalink
Adding bridge PSR-3 compatible loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed Jan 5, 2013
1 parent d3d51b7 commit 74a4ac0
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/node_modules/
/.idea/
/atlassian-ide-plugin.xml
/composer.lock
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"kriswallsmith/buzz": "*",
"guzzle/guzzle": "*",
"monolog/monolog": "*",
"ext-curl": "*"
"ext-curl": "*",
"psr/log": "*"
},
"suggest": {
"zendframework/zend-http": "To use ZF2 Zend\\Http\\Client as HTTP client",
Expand Down
57 changes: 57 additions & 0 deletions src/fXmlRpc/Timing/Psr3TimerBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright (C) 2012-2013
* Lars Strojny, InterNations GmbH <lars.strojny@internations.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace fXmlRpc\Timing;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class Psr3TimerBridge extends AbstractTimerBridge
{
/**
* Create new Zend_Log bridge
*
* Allows passing custom log level and message template (with sprintf() control characters) for log message
* customization
*
* @param LoggerInterface $logger
* @param integer $level
* @param string $messageTemplate
*/
public function __construct(LoggerInterface $logger, $level = null, $messageTemplate = null)
{
$this->logger = $logger;
$this->setLevel($level, LogLevel::DEBUG);
$this->messageTemplate = $messageTemplate ?: $this->messageTemplate;
}

public function recordTiming($callTime, $method, array $arguments)
{
$this->logger->log(
$this->getLevel($callTime),
sprintf($this->messageTemplate, $callTime),
array('xmlrpcMethod' => $method, 'xmlrpcArguments' => $arguments)
);
}
}
133 changes: 133 additions & 0 deletions tests/fXmlRpc/Timing/Psr3TimerBridgeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* Copyright (C) 2012-2013
* Lars Strojny, InterNations GmbH <lars.strojny@internations.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace fXmlRpc\Timing;

use Psr\Log\LogLevel;

class Psr3TimerBridgeTest extends \PHPUnit_Framework_TestCase
{
private $logger;

public function setUp()
{
$this->logger = $this->getMock('Psr\Log\LoggerInterface');
}

public function testDelegatesLogging()
{
$bridge = new Psr3TimerBridge($this->logger);
$this->logger
->expects($this->once())
->method('log')
->with(
LogLevel::DEBUG,
'fXmlRpc call took 1.1000000000s',
array('xmlrpcMethod' => 'method', 'xmlrpcArguments' => array('arg1', 'arg2'))
);

$bridge->recordTiming(1.1, 'method', array('arg1', 'arg2'));
}

public function testSettingCustomLogLevel()
{
$bridge = new Psr3TimerBridge($this->logger, LogLevel::ALERT);
$this->logger
->expects($this->once())
->method('log')
->with(
LogLevel::ALERT,
'fXmlRpc call took 1.1000000000s',
array('xmlrpcMethod' => 'method', 'xmlrpcArguments' => array('arg1', 'arg2'))
);

$bridge->recordTiming(1.1, 'method', array('arg1', 'arg2'));
}

public function testSettingCustomMessageTemplate()
{
$bridge = new Psr3TimerBridge($this->logger, null, 'Custom template %2.1Fs');
$this->logger
->expects($this->once())
->method('log')
->with(
LogLevel::DEBUG,
'Custom template 1.1s',
array('xmlrpcMethod' => 'method', 'xmlrpcArguments' => array('arg1', 'arg2'))
);

$bridge->recordTiming(1.1, 'method', array('arg1', 'arg2'));
}

public function testSpecifyingLoggingThresholds()
{
$bridge = new Psr3TimerBridge($this->logger, array(1 => LogLevel::DEBUG, 2 => LogLevel::WARNING, 3.5 => LogLevel::ALERT));
$this->logger
->expects($this->at(0))
->method('log')
->with(
LogLevel::DEBUG,
'fXmlRpc call took 0.1000000000s',
array('xmlrpcMethod' => 'method', 'xmlrpcArguments' => array('arg1', 'arg2'))
);
$this->logger
->expects($this->at(1))
->method('log')
->with(
LogLevel::DEBUG,
'fXmlRpc call took 1.1000000000s',
array('xmlrpcMethod' => 'method', 'xmlrpcArguments' => array('arg1', 'arg2'))
);
$this->logger
->expects($this->at(2))
->method('log')
->with(
LogLevel::WARNING,
'fXmlRpc call took 2.5000000000s',
array('xmlrpcMethod' => 'method', 'xmlrpcArguments' => array('arg1', 'arg2'))
);
$this->logger
->expects($this->at(3))
->method('log')
->with(
LogLevel::ALERT,
'fXmlRpc call took 3.5000000000s',
array('xmlrpcMethod' => 'method', 'xmlrpcArguments' => array('arg1', 'arg2'))
);
$this->logger
->expects($this->at(4))
->method('log')
->with(
LogLevel::ALERT,
'fXmlRpc call took 5.5000000000s',
array('xmlrpcMethod' => 'method', 'xmlrpcArguments' => array('arg1', 'arg2'))
);

$bridge->recordTiming(0.1, 'method', array('arg1', 'arg2'));
$bridge->recordTiming(1.1, 'method', array('arg1', 'arg2'));
$bridge->recordTiming(2.5, 'method', array('arg1', 'arg2'));
$bridge->recordTiming(3.5, 'method', array('arg1', 'arg2'));
$bridge->recordTiming(5.5, 'method', array('arg1', 'arg2'));
}
}

0 comments on commit 74a4ac0

Please sign in to comment.