Permalink
Browse files

Merge branch 'logging' into stable

  • Loading branch information...
2 parents 3d06acf + c3ec422 commit da8adc0f9a493131bae4326f6062b455bdbb9095 @nateabele nateabele committed Dec 9, 2009
@@ -0,0 +1,56 @@
+<?php
+
+namespace lithium\tests\cases\util\audit\logger\adapters;
+
+use \lithium\util\audit\Logger;
+use \lithium\util\audit\logger\adapters\Syslog;
+
+/**
+ * Syslog adapter test.
+ */
+class SyslogTest extends \lithium\test\Unit {
+
+ public function setUp() {
+ $this->syslog = new Syslog();
+ Logger::config(array('syslog' => array('adapter' => $this->syslog)));
+ }
+
+ public function testConfiguration() {
+ $loggers = Logger::config();
+ $result = isset($loggers['syslog']);
+ $this->assertTrue($result);
+ }
+
+ public function testConstruct() {
+ $expected = array(
+ 'identity' => false,
+ 'options' => LOG_ODELAY,
+ 'facility' => LOG_USER,
+ 'priority' => LOG_INFO,
+ 'init' => true
+ );
+ $result = $this->syslog->_config;
+ $this->assertEqual($expected, $result);
+
+ $syslog = new Syslog(array(
+ 'identity' => 'SyslogTest',
+ 'priority' => LOG_DEBUG
+ ));
+ $expected = array(
+ 'identity' => 'SyslogTest',
+ 'options' => LOG_ODELAY,
+ 'facility' => LOG_USER,
+ 'priority' => LOG_DEBUG,
+ 'init' => true
+ );
+ $result = $syslog->_config;
+ $this->assertEqual($expected, $result);
+ }
+
+ public function testWrite() {
+ $result = Logger::write('syslog', 'SyslogTest message...');
+ $this->assertTrue($result);
+ }
+}
+
+?>
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Lithium: the most rad php framework
+ *
+ * @copyright Copyright 2009, Union of RAD (http://union-of-rad.org)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ */
+
+namespace lithium\util\audit\logger\adapters;
+
+use \lithium\util\String;
+use \lithium\storage\Cache as Writer;
+
+class Cache extends \lithium\core\Object {
+
+ /**
+ * Class constructor
+ *
+ * @return void
+ */
+ public function __construct($config = array()) {
+ $defaults = array(
+ 'config' => null,
+ 'expiry' => '+999 days',
+ 'key' => 'log_{:type}_{:timestamp}'
+ );
+ parent::__construct($config + $defaults);
+ }
+
+ /**
+ * Appends `$data` to file `$type`.
+ *
+ * @param string $type
+ * @param string $message
+ * @return boolean True on successful write, false otherwise
+ */
+ public function write($type, $message) {
+ $config = $this->_config;
+
+ return function($self, $params, $chain) use ($config) {
+ $params += array('timestamp' => strtotime('now'));
+ $key = $config['key'];
+ $key = is_callable($key) ? $key($params) : String::insert($key, $params);
+ Writer::write($config['config'], $key, $params['message'], $config['expiry']);
+ };
+ }
+}
+
+?>
@@ -0,0 +1,62 @@
+<?php
+/**
+ * Lithium: the most rad php framework
+ *
+ * @copyright Copyright 2009, Union of RAD (http://union-of-rad.org)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ */
+
+namespace lithium\util\audit\logger\adapters;
+
+/**
+ * The Syslog adapter facilitates logging messages to a syslogd backend.
+ */
+class Syslog extends \lithium\core\Object {
+
+ /**
+ * The last connection to have opened syslog. This will determine whether or
+ * not the log needs to be closed and reopened.
+ *
+ * @var string
+ */
+ protected static $_lastOpenedBy;
+
+ /**
+ * Class constructor
+ *
+ * @return void
+ */
+ public function __construct($config = array()) {
+ $defaults = array(
+ 'identity' => false,
+ 'options' => LOG_ODELAY,
+ 'facility' => LOG_USER,
+ 'priority' => LOG_INFO
+ );
+ parent::__construct($config + $defaults);
+ }
+
+ /**
+ * Appends `$data` to file `$type`.
+ *
+ * @param string $type
+ * @param string $message
+ * @return boolean True on successful write, false otherwise
+ */
+ public function write($type, $message) {
+ if (static::$_lastOpenedBy != $type) {
+ closelog();
+ openlog(
+ $this->_config['identity'], $this->_config['options'], $this->_config['facility']
+ );
+ static::$_lastOpenedBy = $type;
+ }
+ $priority = $this->_config['priority'];
+
+ return function($self, $params, $chain) use ($priority) {
+ return syslog($priority, $params['message']);
+ };
+ }
+}
+
+?>

0 comments on commit da8adc0

Please sign in to comment.