Skip to content
Browse files

Moved code to lib/ folder, added optional PSR-0 support, added PSR-0 …

…example and updated readme
  • Loading branch information...
1 parent 70aa86e commit 618b9399a4c22114a918eeff97bbf38bd5769e27 @jbroadway committed Jan 11, 2012
View
32 README.md
@@ -1,10 +1,10 @@
-## Analog - PHP 5.3+ micro logging class
+## Analog - PHP 5.3+ micro logging package
* Copyright: (c) 2012 Johnny Broadway
* License: http://www.opensource.org/licenses/mit-license.php
-A short and simple logging class based on the idea of using closures for
-configurability and extensibility. It functions as a static class, but you can
+A [MicroPHP](http://microphp.org/) logging package based on the idea of using closures
+for configurability and extensibility. It functions as a static class, but you can
completely control the writing of log messages through a closure function
(aka [anonymous functions](http://ca3.php.net/manual/en/functions.anonymous.php)).
@@ -28,8 +28,7 @@ with examples for each in the examples folder. These include:
* Syslog - Send messages to syslog
* Variable - Buffer messages to a variable reference.
-So while it's a micro class (the core is 51 lines of code, plus the handler of your choice),
-it's highly extensible and very capable out of the box too.
+So while it's a micro class, it's highly extensible and very capable out of the box too.
### Rationale
@@ -51,6 +50,8 @@ flexibility.
### Usage
+Standard usage:
+
```php
<?php
@@ -77,4 +78,25 @@ Analog::log ('Debugging info', Analog::DEBUG);
?>
```
+PSR-0 usage:
+
+```php
+<?php
+
+require_once ('SplClassLoader.php');
+
+$loader = new SplClassLoader ('Analog', 'lib');
+$loader->register ();
+
+use \Analog\Analog;
+
+Analog::log ('Log this error');
+
+Analog::handler (\Analog\Handler\FirePHP::init ());
+
+Analog::log ('Take me to your browser');
+
+?>
+```
+
For more examples, see the [examples](https://github.com/jbroadway/analog/tree/master/examples) folder.
View
7 composer.json
@@ -13,6 +13,11 @@
}
],
"require": {
- "php": ">=5.3.0"
+ "php": ">=5.3.2"
+ },
+ "autoload": {
+ "psr-0": {
+ "Analog": "lib/"
+ }
}
}
View
136 examples/SplClassLoader.php
@@ -0,0 +1,136 @@
+<?php
+
+/**
+ * SplClassLoader implementation that implements the technical interoperability
+ * standards for PHP 5.3 namespaces and class names.
+ *
+ * http://groups.google.com/group/php-standards/web/final-proposal
+ *
+ * // Example which loads classes for the Doctrine Common package in the
+ * // Doctrine\Common namespace.
+ * $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
+ * $classLoader->register();
+ *
+ * @author Jonathan H. Wage <jonwage@gmail.com>
+ * @author Roman S. Borschel <roman@code-factory.org>
+ * @author Matthew Weier O'Phinney <matthew@zend.com>
+ * @author Kris Wallsmith <kris.wallsmith@gmail.com>
+ * @author Fabien Potencier <fabien.potencier@symfony-project.org>
+ */
+class SplClassLoader
+{
+ private $_fileExtension = '.php';
+ private $_namespace;
+ private $_includePath;
+ private $_namespaceSeparator = '\\';
+
+ /**
+ * Creates a new <tt>SplClassLoader</tt> that loads classes of the
+ * specified namespace.
+ *
+ * @param string $ns The namespace to use.
+ */
+ public function __construct($ns = null, $includePath = null)
+ {
+ $this->_namespace = $ns;
+ $this->_includePath = $includePath;
+ }
+
+ /**
+ * Sets the namespace separator used by classes in the namespace of this class loader.
+ *
+ * @param string $sep The separator to use.
+ */
+ public function setNamespaceSeparator($sep)
+ {
+ $this->_namespaceSeparator = $sep;
+ }
+
+ /**
+ * Gets the namespace seperator used by classes in the namespace of this class loader.
+ *
+ * @return void
+ */
+ public function getNamespaceSeparator()
+ {
+ return $this->_namespaceSeparator;
+ }
+
+ /**
+ * Sets the base include path for all class files in the namespace of this class loader.
+ *
+ * @param string $includePath
+ */
+ public function setIncludePath($includePath)
+ {
+ $this->_includePath = $includePath;
+ }
+
+ /**
+ * Gets the base include path for all class files in the namespace of this class loader.
+ *
+ * @return string $includePath
+ */
+ public function getIncludePath()
+ {
+ return $this->_includePath;
+ }
+
+ /**
+ * Sets the file extension of class files in the namespace of this class loader.
+ *
+ * @param string $fileExtension
+ */
+ public function setFileExtension($fileExtension)
+ {
+ $this->_fileExtension = $fileExtension;
+ }
+
+ /**
+ * Gets the file extension of class files in the namespace of this class loader.
+ *
+ * @return string $fileExtension
+ */
+ public function getFileExtension()
+ {
+ return $this->_fileExtension;
+ }
+
+ /**
+ * Installs this class loader on the SPL autoload stack.
+ */
+ public function register()
+ {
+ spl_autoload_register(array($this, 'loadClass'));
+ }
+
+ /**
+ * Uninstalls this class loader from the SPL autoloader stack.
+ */
+ public function unregister()
+ {
+ spl_autoload_unregister(array($this, 'loadClass'));
+ }
+
+ /**
+ * Loads the given class or interface.
+ *
+ * @param string $className The name of the class to load.
+ * @return void
+ */
+ public function loadClass($className)
+ {
+ if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
+ $fileName = '';
+ $namespace = '';
+ if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
+ $namespace = substr($className, 0, $lastNsPos);
+ $className = substr($className, $lastNsPos + 1);
+ $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
+ }
+ $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
+
+ require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
+ }
+ }
+}
View
2 examples/buffer.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
Analog::handler (Analog\Handler\Buffer::init (
Analog\Handler\Mail::init (
View
2 examples/default.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
Analog::log ('foo');
Analog::log ('bar');
View
2 examples/file.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
$log_file = 'log.txt';
View
2 examples/firephp.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
Analog::handler (Analog\Handler\FirePHP::init ());
View
2 examples/levelbuffer.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
Analog::handler (Analog\Handler\LevelBuffer::init (
Analog\Handler\Mail::init (
View
2 examples/mail.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
Analog::handler (Analog\Handler\Mail::init (
'you@example.com',
View
2 examples/mongo.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
Analog::handler (Analog\Handler\Mongo::init (
'localhost:27017',
View
2 examples/multi.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
$errors = "Errors:\n";
$warnings = "Warnings:\n";
View
2 examples/null.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
Analog::handler (Analog\Handler\Null::init ());
View
2 examples/post.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
$log = '';
View
19 examples/psr-0.php
@@ -0,0 +1,19 @@
+<?php
+
+require 'SplClassLoader.php';
+
+$loader = new SplClassLoader ('Analog', '../lib');
+$loader->register ();
+
+use \Analog\Analog;
+
+$log = '';
+
+Analog::handler (\Analog\Handler\Variable::init ($log));
+
+Analog::log ('Test one');
+Analog::log ('Test two');
+
+echo $log;
+
+?>
View
2 examples/stderr.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
Analog::handler (Analog\Handler\Stderr::init ());
View
2 examples/syslog.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
Analog::handler (Analog\Handler\Syslog::init ('analog', 'user'));
View
2 examples/variable.php
@@ -1,6 +1,6 @@
<?php
-require '../Analog.php';
+require '../lib/Analog.php';
$log = '';
View
34 lib/Analog.php
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * Register a very simple autoloader for the pre-built handlers
+ * based on the current working directory.
+ */
+spl_autoload_register (function ($class) {
+ $file = str_replace ('\\', DIRECTORY_SEPARATOR, ltrim ($class, '\\')) . '.php';
+ if (file_exists (__DIR__ . DIRECTORY_SEPARATOR . $file)) {
+ require_once $file;
+ return true;
+ }
+ return false;
+});
+
+/**
+ * We simply extend the main class so that Analog is
+ * available as a global class. This saves us adding
+ * `use \Analog\Analog` at the top of every file,
+ * or worse, typeing `\Analog\Analog::log()` everywhere.
+ */
+class Analog extends \Analog\Analog {
+ /**
+ * We need to override format() to always write to
+ * the parent, since the pre-built handlers have to
+ * assume they're using the PSR-0 class.
+ */
+ public static function format ($format = false) {
+ if ($format) {
+ \Analog\Analog::$format = $format;
+ }
+ return \Analog\Analog::$format;
+ }
+}
View
29 Analog.php → lib/Analog/Analog.php
@@ -1,5 +1,7 @@
<?php
+namespace Analog;
+
/**
* Analog - PHP 5.3+ logging class
*
@@ -131,6 +133,16 @@ public static function handler ($handler = false) {
}
/**
+ * Format getter/setter.
+ */
+ public static function format ($format = false) {
+ if ($format) {
+ self::$format = $format;
+ }
+ return self::$format;
+ }
+
+ /**
* Get the log info as an associative array.
*/
private static function get_struct ($message, $level) {
@@ -154,7 +166,7 @@ private static function write ($struct) {
$handler = self::handler ();
if (! $handler instanceof \Closure) {
- $handler = Analog\Handler\File::init ($handler);
+ $handler = \Analog\Handler\File::init ($handler);
}
return $handler ($struct);
}
@@ -169,17 +181,4 @@ private static function write ($struct) {
public static function log ($message, $level = 3) {
return self::write (self::get_struct ($message, $level));
}
-}
-
-/**
- * Register a very simple autoloader for the pre-built handlers
- * based on the current working directory.
- */
-spl_autoload_register (function ($class) {
- $file = str_replace ('\\', DIRECTORY_SEPARATOR, ltrim ($class, '\\')) . '.php';
- if (file_exists (__DIR__ . DIRECTORY_SEPARATOR . $file)) {
- require_once $file;
- return true;
- }
- return false;
-});
+}
View
2 Analog/Handler/Buffer.php → lib/Analog/Handler/Buffer.php
@@ -45,7 +45,7 @@ public static function init ($handler) {
self::$destructor = new \Analog\Handler\Buffer\Destructor ();
return function ($info) {
- Buffer::$buffer .= vsprintf (\Analog::$format, $info);
+ Buffer::$buffer .= vsprintf (\Analog\Analog::format (), $info);
};
}
View
0 Analog/Handler/Buffer/Destructor.php → lib/Analog/Handler/Buffer/Destructor.php
File renamed without changes.
View
2 Analog/Handler/File.php → lib/Analog/Handler/File.php
@@ -27,7 +27,7 @@ public static function init ($file) {
throw new \RuntimeException ('Could not lock file');
}
- fwrite ($f, vsprintf (\Analog::$format, $info));
+ fwrite ($f, vsprintf (\Analog\Analog::format (), $info));
flock ($f, LOCK_UN);
fclose ($f);
};
View
16 Analog/Handler/FirePHP.php → lib/Analog/Handler/FirePHP.php
@@ -21,14 +21,14 @@ class FirePHP {
* Translation list for log levels.
*/
private static $log_levels = array (
- \Analog::DEBUG => 'LOG',
- \Analog::INFO => 'INFO',
- \Analog::NOTICE => 'INFO',
- \Analog::WARNING => 'WARN',
- \Analog::ERROR => 'ERROR',
- \Analog::CRITICAL => 'ERROR',
- \Analog::ALERT => 'ERROR',
- \Analog::URGENT => 'ERROR'
+ \Analog\Analog::DEBUG => 'LOG',
+ \Analog\Analog::INFO => 'INFO',
+ \Analog\Analog::NOTICE => 'INFO',
+ \Analog\Analog::WARNING => 'WARN',
+ \Analog\Analog::ERROR => 'ERROR',
+ \Analog\Analog::CRITICAL => 'ERROR',
+ \Analog\Analog::ALERT => 'ERROR',
+ \Analog\Analog::URGENT => 'ERROR'
);
/**
View
2 Analog/Handler/LevelBuffer.php → lib/Analog/Handler/LevelBuffer.php
@@ -44,7 +44,7 @@ public static function init ($handler, $until_level = 2) {
self::$handler = $handler;
return function ($info) use ($until_level) {
- LevelBuffer::$buffer .= vsprintf (\Analog::$format, $info);
+ LevelBuffer::$buffer .= vsprintf (\Analog\Analog::format (), $info);
if ($info['level'] <= $until_level) {
// flush and reset the buffer
LevelBuffer::flush ();
View
0 Analog/Handler/Mail.php → lib/Analog/Handler/Mail.php
File renamed without changes.
View
0 Analog/Handler/Mongo.php → lib/Analog/Handler/Mongo.php
File renamed without changes.
View
0 Analog/Handler/Multi.php → lib/Analog/Handler/Multi.php
File renamed without changes.
View
0 Analog/Handler/Null.php → lib/Analog/Handler/Null.php
File renamed without changes.
View
0 Analog/Handler/Post.php → lib/Analog/Handler/Post.php
File renamed without changes.
View
2 Analog/Handler/Stderr.php → lib/Analog/Handler/Stderr.php
@@ -16,7 +16,7 @@
class Stderr {
public static function init () {
return function ($info) {
- file_put_contents ('php://stderr', vsprintf (\Analog::$format, $info));
+ file_put_contents ('php://stderr', vsprintf (\Analog\Analog::format (), $info));
};
}
}
View
12 Analog/Handler/Syslog.php → lib/Analog/Handler/Syslog.php
@@ -12,12 +12,12 @@
*/
class Syslog {
public static $levels = array (
- \Analog::DEBUG => LOG_DEBUG,
- \Analog::INFO => LOG_INFO,
- \Analog::WARNING => LOG_WARNING,
- \Analog::ERROR => LOG_ERR,
- \Analog::CRITICAL => LOG_CRIT,
- \Analog::ALERT => LOG_ALERT
+ \Analog\Analog::DEBUG => LOG_DEBUG,
+ \Analog\Analog::INFO => LOG_INFO,
+ \Analog\Analog::WARNING => LOG_WARNING,
+ \Analog\Analog::ERROR => LOG_ERR,
+ \Analog\Analog::CRITICAL => LOG_CRIT,
+ \Analog\Analog::ALERT => LOG_ALERT
);
public static $facilities = array (
View
2 Analog/Handler/Variable.php → lib/Analog/Handler/Variable.php
@@ -18,7 +18,7 @@
class Variable {
public static function init (&$log) {
return function ($info) use (&$log) {
- $log .= vsprintf (\Analog::$format, $info);
+ $log .= vsprintf (\Analog\Analog::format (), $info);
};
}
}
View
12 tests/Analog.php
@@ -1,31 +1,35 @@
<?php
-require_once ('Analog.php');
+require_once ('lib/Analog.php');
class AnalogTest extends PHPUnit_Framework_TestCase {
public static $log = '';
- function test_analog () {
+ function test_default () {
// Check it wrote correctly to temp file
Analog::log ('Foo');
$this->assertEquals (
sprintf ("%s - %s - %d - %s\n", 'localhost', gmdate ('Y-m-d H:i:s'), 3, 'Foo'),
file_get_contents (Analog::handler ())
);
unlink (Analog::handler ());
+ }
+ function test_format () {
// Test changing the format string and write again
- Analog::$format = "%s, %s, %d, %s\n";
+ Analog::format ("%s, %s, %d, %s\n");
Analog::log ('Foo');
$this->assertEquals (
sprintf ("%s, %s, %d, %s\n", 'localhost', gmdate ('Y-m-d H:i:s'), 3, 'Foo'),
file_get_contents (Analog::handler ())
);
unlink (Analog::handler ());
+ }
+ function test_handler () {
// Test logging using a closure
Analog::handler (function ($msg) {
- AnalogTest::$log .= vsprintf (Analog::$format, $msg);
+ AnalogTest::$log .= vsprintf (Analog::format (), $msg);
});
Analog::log ('Testing');

0 comments on commit 618b939

Please sign in to comment.
Something went wrong with that request. Please try again.