From 3da160c9e3698556b0d160d421da26f1287529e9 Mon Sep 17 00:00:00 2001 From: vitalypanait Date: Fri, 6 May 2016 21:26:55 +0300 Subject: [PATCH 1/6] Udp adapter for logger --- Library/Phalcon/Logger/Adapter/Udplogger.php | 183 +++++++++++++++++++ Library/Phalcon/Logger/README.md | 18 ++ README.md | 1 + 3 files changed, 202 insertions(+) create mode 100644 Library/Phalcon/Logger/Adapter/Udplogger.php diff --git a/Library/Phalcon/Logger/Adapter/Udplogger.php b/Library/Phalcon/Logger/Adapter/Udplogger.php new file mode 100644 index 000000000..4c80cb2aa --- /dev/null +++ b/Library/Phalcon/Logger/Adapter/Udplogger.php @@ -0,0 +1,183 @@ + | + +------------------------------------------------------------------------+ +*/ + +namespace Phalcon\Logger\Adapter; + +use Phalcon\Logger\Exception; +use Phalcon\Logger\Formatter\Line as LineFormatter; +use Phalcon\Logger\Adapter as LoggerAdapter; +use Phalcon\Logger\AdapterInterface; + +/** + * Phalcon\Logger\Adapter\Udplogger + * Sends messages using UDP protocol to external server + * + * @version 0.1 + * @author Vitaliy Panait + */ +class Udplogger extends LoggerAdapter implements AdapterInterface +{ + /** + * Name + * + * @var string + */ + protected $name = 'phalcon'; + + /** + * Adapter options + * + * @var array + */ + protected $options = []; + + /** + * @var resource + */ + protected $socket; + + /** + * Storage for holding all messages until they are ready to be sent to server. + * + * @var array + */ + protected $logs = []; + + /** + * Flag for the transaction + * + * @var boolean + */ + protected $isTransaction = false; + + /** + * Class constructor. + * + * @param string $name + * @param array $options + * @throws \Phalcon\Logger\Exception + */ + public function __construct($name = 'phalcon', array $options = []) + { + if (!isset($options['url'])) { + throw new Exception("Parameter 'url' is required"); + } + + if (!isset($options['port'])) { + throw new Exception("Parameter 'port' is required"); + } + + if ($name) { + $this->name = $name; + } + + $this->options = $options; + + register_shutdown_function([$this, 'commit']); + register_shutdown_function([$this, 'close']); + } + + /** + * {@inheritdoc} + * + * @return \Phalcon\Logger\FormatterInterface + */ + public function getFormatter() + { + if (!$this->_formatter) { + $this->_formatter = new LineFormatter(); + } + + return $this->_formatter; + } + + /** + * Writes the log. + * + * @param string $message + * @param integer $type + * @param integer $time + * @param array $context + */ + public function logInternal($message, $type, $time, $context = []) + { + $this->logs[] = compact('message', 'type', 'time', 'context'); + + if (!$this->isTransaction) { + $this->send(); + } + } + + /** + * {@inheritdoc} + * + * @return boolean + */ + public function close() + { + if ($this->socket !== null) { + socket_close($this->socket); + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function begin() + { + $this->commit(); + $this->isTransaction = true; + } + + /** + * {@inheritdoc} + */ + public function commit() + { + if (!$this->isTransaction || empty($this->logs)) { + $this->isTransaction = false; + + return; + } + + $this->send(); + $this->isTransaction = false; + } + + /** + * {@inheritdoc} + */ + protected function send() + { + if (empty($this->logs)) { + return; + } + + $message = json_encode($this->logs); + + if ($this->socket === null) { + $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); + } + + socket_sendto($this->socket, $message, strlen($message), 0, $this->options['url'], $this->options['port']); + + $this->logs = []; + } +} \ No newline at end of file diff --git a/Library/Phalcon/Logger/README.md b/Library/Phalcon/Logger/README.md index 7a041ffb3..3e751c5e9 100644 --- a/Library/Phalcon/Logger/README.md +++ b/Library/Phalcon/Logger/README.md @@ -56,6 +56,24 @@ $logger->info('Info Message'); // etc ``` +## UDP logger + +Adapter to send messages by UDP protocol to external server + +```php +use Phalcon\Logger\Adapter\Udplogger as UdpLogger; + +$di->set('logger', function() { + + $logger = new UdpLogger('errors', [ + 'url' => $url, + 'port' => $port + ]); + + return $logger; +}); +``` + ## Multiple file logger Adapter `Phalcon\Logger\Adapter\File\Multiple` can be used to log messages to multiple files. This is similar to the core diff --git a/README.md b/README.md index 6f3b529bd..8487be9fa 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ See [CONTRIBUTING.md](docs/CONTRIBUTING.md) ### Logger * [Phalcon\Logger\Adapter\Database](Library/Phalcon/Logger) - Adapter to store logs in a database table (!phalcon) * [Phalcon\Logger\Adapter\Firelogger](Library/Phalcon/Logger) - Adapter to log messages in the Firelogger console in Firebug (@phalcon) +* [Phalcon\Logger\Adapter\Udplogger](Library/Phalcon/Logger) - Adapter to log messages using UDP protocol to external server (@vitalypanait) * [Phalcon\Logger\Adapter\File\Multiple](Library/Phalcon/Logger) - Adapter to log to multiple files (@rlaffers) ### Mailer From b806a867f5c4bd43e79840c6cf84ee2c6dbc34ed Mon Sep 17 00:00:00 2001 From: vitalypanait Date: Fri, 6 May 2016 22:38:11 +0300 Subject: [PATCH 2/6] fix warning --- Library/Phalcon/Logger/Adapter/Udplogger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Phalcon/Logger/Adapter/Udplogger.php b/Library/Phalcon/Logger/Adapter/Udplogger.php index 4c80cb2aa..117324716 100644 --- a/Library/Phalcon/Logger/Adapter/Udplogger.php +++ b/Library/Phalcon/Logger/Adapter/Udplogger.php @@ -180,4 +180,4 @@ protected function send() $this->logs = []; } -} \ No newline at end of file +} From bfba859dcbd158fc18129c6ec3548b0e530f6ab7 Mon Sep 17 00:00:00 2001 From: Studentsov Date: Sat, 7 May 2016 16:35:11 +0300 Subject: [PATCH 3/6] Update NestedSet.php Bug fixed: the rootAttribute wasn't defined --- Library/Phalcon/Mvc/Model/Behavior/NestedSet.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Library/Phalcon/Mvc/Model/Behavior/NestedSet.php b/Library/Phalcon/Mvc/Model/Behavior/NestedSet.php index 59a22abe3..6c7d83413 100644 --- a/Library/Phalcon/Mvc/Model/Behavior/NestedSet.php +++ b/Library/Phalcon/Mvc/Model/Behavior/NestedSet.php @@ -869,6 +869,8 @@ private function addNode(ModelInterface $target, $key, $levelUp, array $attribut private function makeRoot($attributes, $whiteList) { $owner = $this->getOwner(); + + $owner->{$this->rootAttribute} = 0; $owner->{$this->leftAttribute} = 1; $owner->{$this->rightAttribute} = 2; $owner->{$this->levelAttribute} = 1; From a778676bc697a3ec723f8b3d7e59863e7f4c1513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hallet?= Date: Tue, 10 May 2016 19:34:09 +0200 Subject: [PATCH 4/6] add mysql INTERVAL units basic units have been added, see http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-add, the more complex ones remain to be added (like 'SECOND_MICROSECOND) --- Library/Phalcon/Db/Dialect/MysqlExtended.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Library/Phalcon/Db/Dialect/MysqlExtended.php b/Library/Phalcon/Db/Dialect/MysqlExtended.php index 7d92ce84c..1057e3743 100644 --- a/Library/Phalcon/Db/Dialect/MysqlExtended.php +++ b/Library/Phalcon/Db/Dialect/MysqlExtended.php @@ -46,12 +46,27 @@ public function getSqlExpression(array $expression, $escapeChar = null, $bindCou } switch ($expression["arguments"][1]['value']) { + case "'MICROSECOND'": + return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' MICROSECOND'; + case "'SECOND'": + return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' SECOND'; + case "'MINUTE'": + return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' MINUTE'; + case "'HOUR'": + return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' HOUR'; case "'DAY'": return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' DAY'; - case "'MONTH'": + case "'WEEK'": + return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' WEEK'; + case "'MONTH'": return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' MONTH'; + case "'QUARTER'": + return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' QUARTER'; case "'YEAR'": return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' YEAR'; + default: + throw new \Exception('DATE_INTERVAL unit is not supported'); + } break; From 3201b3295b0ee745dac885969b77d1763df9b69e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hallet?= Date: Tue, 10 May 2016 21:21:33 +0200 Subject: [PATCH 5/6] Update MysqlExtended.php fix whitespaces --- Library/Phalcon/Db/Dialect/MysqlExtended.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Library/Phalcon/Db/Dialect/MysqlExtended.php b/Library/Phalcon/Db/Dialect/MysqlExtended.php index 1057e3743..13f83d0f2 100644 --- a/Library/Phalcon/Db/Dialect/MysqlExtended.php +++ b/Library/Phalcon/Db/Dialect/MysqlExtended.php @@ -58,7 +58,7 @@ public function getSqlExpression(array $expression, $escapeChar = null, $bindCou return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' DAY'; case "'WEEK'": return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' WEEK'; - case "'MONTH'": + case "'MONTH'": return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' MONTH'; case "'QUARTER'": return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' QUARTER'; @@ -66,7 +66,6 @@ public function getSqlExpression(array $expression, $escapeChar = null, $bindCou return 'INTERVAL ' . $this->getSqlExpression($expression["arguments"][0]) . ' YEAR'; default: throw new \Exception('DATE_INTERVAL unit is not supported'); - } break; From f916e37ab041c844849e2ed276e79a4af6a3c2f1 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Tue, 17 May 2016 09:21:56 +0300 Subject: [PATCH 6/6] Enabled tests for Phalcon 2.0.12 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 11aa5f2ff..4075261f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,6 +29,7 @@ env: - TEST_DB_NAME="incubator_tests" - TEST_DB_CHARSET="utf8" matrix: + - PHALCON_VERSION="2.0.12" - PHALCON_VERSION="2.0.11" - PHALCON_VERSION="2.0.10" - PHALCON_VERSION="2.0.9"