Permalink
Browse files

Update logger to implement accepted PSR log spec

Add tests!
  • Loading branch information...
bobthecow committed Jan 12, 2013
1 parent 7522e22 commit 1515f5126b35436228f40ee3338c8ede5398d7fb
View
@@ -488,7 +488,7 @@ public function loadPartial($name)
// If the named partial cannot be found, log then return null.
$this->log(
Mustache_Logger::WARNING,
- sprintf('Partial not found: "%s"', $name),
+ 'Partial not found: "{name}"',
array('name' => $name)
);
}
@@ -535,7 +535,7 @@ private function loadSource($source)
if (!is_file($fileName)) {
$this->log(
Mustache_Logger::DEBUG,
- sprintf('Writing "%s" class to template cache: "%s"', $className, $fileName),
+ 'Writing "{className}" class to template cache: "{fileName}"',
array('className' => $className, 'fileName' => $fileName)
);
@@ -546,7 +546,7 @@ private function loadSource($source)
} else {
$this->log(
Mustache_Logger::WARNING,
- sprintf('Template cache disabled, evaluating "%s" class at runtime', $className),
+ 'Template cache disabled, evaluating "{className}" class at runtime',
array('className' => $className)
);
@@ -556,7 +556,7 @@ private function loadSource($source)
$this->log(
Mustache_Logger::DEBUG,
- sprintf('Instantiating template: "%s"', $className),
+ 'Instantiating template: "{className}"',
array('className' => $className)
);
@@ -610,8 +610,8 @@ private function compile($source)
$this->log(
Mustache_Logger::INFO,
- sprintf('Compiling template to "%s" class', $name),
- array('name' => $name)
+ 'Compiling template to "{className}" class',
+ array('className' => $name)
);
return $this->getCompiler()->compile($source, $tree, $name, isset($this->escape), $this->charset);
@@ -647,7 +647,7 @@ private function writeCacheFile($fileName, $source)
if (!is_dir($dirName)) {
$this->log(
Mustache_Logger::INFO,
- sprintf('Creating Mustache template cache directory: "%s"', $dirName),
+ 'Creating Mustache template cache directory: "{dirName}"',
array('dirName' => $dirName)
);
@@ -660,8 +660,8 @@ private function writeCacheFile($fileName, $source)
$this->log(
Mustache_Logger::DEBUG,
- sprintf('Caching compiled template to "%s"', dirname($fileName)),
- array('filename' => $fileName)
+ 'Caching compiled template to "{fileName}"',
+ array('fileName' => $fileName)
);
$tempFile = tempnam($dirName, basename($fileName));
@@ -675,8 +675,8 @@ private function writeCacheFile($fileName, $source)
$this->log(
Mustache_Logger::ERROR,
- sprintf('Unable to rename Mustache temp cache file: "%s" -> "%s"', $tempFile, $fileName),
- array('tempFile' => $tempFile, 'fileName' => $fileName)
+ 'Unable to rename Mustache temp cache file: "{tempName}" -> "{fileName}"',
+ array('tempName' => $tempFile, 'fileName' => $fileName)
);
}
View
@@ -16,7 +16,7 @@
*
* The message MUST be a string or object implementing __toString().
*
- * The message MAY contain placeholders in the form: %foo% where foo
+ * The message MAY contain placeholders in the form: {foo} where foo
* will be replaced by the context data in key "foo".
*
* The context array can contain arbitrary data, the only assumption that
@@ -132,4 +132,4 @@ public function debug($message, array $context = array());
* @return null
*/
public function log($level, $message, array $context = array());
-}
+}
@@ -0,0 +1,121 @@
+<?php
+
+/*
+ * This file is part of Mustache.php.
+ *
+ * (c) 2012 Justin Hileman
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * This is a simple Logger implementation that other Loggers can inherit from.
+ *
+ * This is identical to the Psr\Log\AbstractLogger.
+ *
+ * It simply delegates all log-level-specific methods to the `log` method to
+ * reduce boilerplate code that a simple Logger that does the same thing with
+ * messages regardless of the error level has to implement.
+ */
+abstract class Mustache_Logger_AbstractLogger implements Mustache_Logger
+{
+ /**
+ * System is unusable.
+ *
+ * @param string $message
+ * @param array $context
+ */
+ public function emergency($message, array $context = array())
+ {
+ $this->log(Mustache_Logger::EMERGENCY, $message, $context);
+ }
+
+ /**
+ * Action must be taken immediately.
+ *
+ * Example: Entire website down, database unavailable, etc. This should
+ * trigger the SMS alerts and wake you up.
+ *
+ * @param string $message
+ * @param array $context
+ */
+ public function alert($message, array $context = array())
+ {
+ $this->log(Mustache_Logger::ALERT, $message, $context);
+ }
+
+ /**
+ * Critical conditions.
+ *
+ * Example: Application component unavailable, unexpected exception.
+ *
+ * @param string $message
+ * @param array $context
+ */
+ public function critical($message, array $context = array())
+ {
+ $this->log(Mustache_Logger::CRITICAL, $message, $context);
+ }
+
+ /**
+ * Runtime errors that do not require immediate action but should typically
+ * be logged and monitored.
+ *
+ * @param string $message
+ * @param array $context
+ */
+ public function error($message, array $context = array())
+ {
+ $this->log(Mustache_Logger::ERROR, $message, $context);
+ }
+
+ /**
+ * Exceptional occurrences that are not errors.
+ *
+ * Example: Use of deprecated APIs, poor use of an API, undesirable things
+ * that are not necessarily wrong.
+ *
+ * @param string $message
+ * @param array $context
+ */
+ public function warning($message, array $context = array())
+ {
+ $this->log(Mustache_Logger::WARNING, $message, $context);
+ }
+
+ /**
+ * Normal but significant events.
+ *
+ * @param string $message
+ * @param array $context
+ */
+ public function notice($message, array $context = array())
+ {
+ $this->log(Mustache_Logger::NOTICE, $message, $context);
+ }
+
+ /**
+ * Interesting events.
+ *
+ * Example: User logs in, SQL logs.
+ *
+ * @param string $message
+ * @param array $context
+ */
+ public function info($message, array $context = array())
+ {
+ $this->log(Mustache_Logger::INFO, $message, $context);
+ }
+
+ /**
+ * Detailed debug information.
+ *
+ * @param string $message
+ * @param array $context
+ */
+ public function debug($message, array $context = array())
+ {
+ $this->log(Mustache_Logger::DEBUG, $message, $context);
+ }
+}
Oops, something went wrong.

0 comments on commit 1515f51

Please sign in to comment.