Cgiapp2
[ class tree: Cgiapp2 ] [ index: Cgiapp2 ] [ all elements ]

Source for file Log.class.php

Documentation is available at Log.class.php

  1. <?php
  2. /**
  3. * Cgiapp2 - Framework for building reusable web-applications
  4. *
  5. * A PHP5 port of perl's CGI::Application, a framework for building reusable web
  6. * applications.
  7. *
  8. * @package Cgiapp2
  9. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>; based on
  10. * CGI::Application, by Jesse Erlbaum <jesse@erlbaum.net>, et. al.
  11. * @copyright (c) 2004 - present, Matthew Weier O'Phinney
  12. * @license BSD License (http://www.opensource.org/licenses/bsd-license.php)
  13. * @category Tools and Utilities
  14. * @tutorial Cgiapp2/Cgiapp2.cls
  15. * @version $Id:$
  16. */
  17.  
  18. /**
  19. * Observes Cgiapp2_Exception
  20. */
  21. require_once 'Cgiapp2/Exception.class.php';
  22.  
  23. /**
  24. * Implements Cgiapp2_Exception_Observer_Interface
  25. */
  26. require_once 'Cgiapp2/Exception/Observer/Interface.class.php';
  27.  
  28. /**
  29. * Cgiapp2_Exception_Observer_Log
  30. *
  31. * {@link Cgiapp2_Exception} observer. Writes exception information to a log
  32. * file.
  33. *
  34. * Sample usage:
  35. * <code>
  36. * require_once 'Cgiapp2/Exception/Observer/Log.class.php';
  37. *
  38. * // Set the log file to '/tmp/exceptions/log'
  39. * Cgiapp2_Exception_Observer_Log::setFile('/tmp/exceptions/log');
  40. *
  41. * try {
  42. * throw new Cgiapp2_Exception('Log this...');
  43. * } catch (Cgiapp2_Exception $e) {
  44. * // do something
  45. * }
  46. * </code>
  47. *
  48. * @package Cgiapp2
  49. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>
  50. * @copyright (c) 2006 - Present, Matthew Weier O'Phinney
  51. * <mweierophinney@gmail.com>
  52. * @version @release-version@
  53. */
  54. class Cgiapp2_Exception_Observer_Log implements Cgiapp2_Exception_Observer_Interface
  55. {
  56. /**
  57. * Log file. Defaults to '/tmp/cgiapp_exception.log'
  58. * @var string
  59. * @access public
  60. */
  61. public $file;
  62.  
  63. /**
  64. * sprintf() style format for log message. Defaults to
  65. * "[%s] [%s:%c] %c: %s\n" ([date] [file:line] code: message)
  66. * @var string
  67. * @access public
  68. */
  69. public $format;
  70.  
  71. /**
  72. * Singleton instance
  73. * @var bool|Cgiapp2_Exception_Observer_Log
  74. * @static
  75. * @access private
  76. */
  77. private static $instance = false;
  78.  
  79. /**
  80. * Constructor
  81. *
  82. * @param mixed $file File to which to log; must be writeable
  83. * @param mixed $format Defaults to empty; log format (printf compatible)
  84. * @access public
  85. * @return void
  86. * @throws Exception if log file is not writable
  87. */
  88. private function __construct($file, $format)
  89. {
  90. $this->file = $file;
  91. $this->format = $format;
  92. }
  93.  
  94. /**
  95. * Singleton
  96. *
  97. * @static
  98. * @access public
  99. * @param string $file Location of log
  100. * @param string $format sprintf() format for log
  101. * @return void
  102. * @throws Exception if unable to write to file
  103. */
  104. public static function getInstance($file = null, $format = null)
  105. {
  106. if (self::$instance) {
  107. return self::$instance;
  108. }
  109.  
  110. if (empty($file)) {
  111. $file = '/tmp/cgiapp_exception.log';
  112. }
  113. if (file_exists($file) && !is_writable($file)) {
  114. throw new Exception(__CLASS__ . ' file \'' . $file . '\' is not writable');
  115. } elseif (!file_exists($file) && !is_writable(dirname($file))) {
  116. throw new Exception(__CLASS__ . ' file \'' . $file . '\' can not be created');
  117. }
  118.  
  119. if (empty($format)) {
  120. $format = "[%s] [%s:%c] %c: %s\n";
  121. }
  122.  
  123. self::$instance = new Cgiapp2_Exception_Observer_Log($file, $format);
  124.  
  125. return self::$instance;
  126. }
  127.  
  128. /**
  129. * Set the log file
  130. *
  131. * @static
  132. * @access public
  133. * @param string $file
  134. * @return bool
  135. */
  136. public static function setFile($file)
  137. {
  138. if (file_exists($file) && !is_writable($file)) {
  139. return false;
  140. } elseif (!file_exists($file) && !is_writable(dirname($file))) {
  141. return false;
  142. }
  143.  
  144. self::getInstance()->file = $file;
  145. return true;
  146. }
  147.  
  148. /**
  149. * Set the log format
  150. *
  151. * @static
  152. * @access public
  153. * @param mixed $format
  154. * @return void
  155. */
  156. public static function setFormat($format)
  157. {
  158. self::getInstance()->format = $format;
  159. }
  160.  
  161. /**
  162. * Log an exception
  163. *
  164. * @static
  165. * @access public
  166. * @param Cgiapp2_Exception $e
  167. * @return void
  168. * @throws Exception if unable to append file or obtain lock
  169. */
  170. public static function event(Cgiapp2_Exception $e)
  171. {
  172. $handler = self::getInstance();
  173. $log = $handler->file;
  174. $fh = fopen($log, 'a');
  175. if (false === $fh) {
  176. throw new Exception(__CLASS__ . ' unable to append to log file');
  177. }
  178. if (!flock($fh, LOCK_EX)) {
  179. throw new Exception(__CLASS__ . ' unable to lock log file');
  180. }
  181.  
  182. $date = date('Y-m-d H:i:s');
  183. $file = $e->getFile();
  184. $line = $e->getLine();
  185. $code = $e->getCode();
  186. $msg = $e->getMessage();
  187.  
  188. $msg = sprintf($handler->format, $date, $file, $line, $code, $msg);
  189. fwrite($fh, $msg);
  190. flock($fh, LOCK_UN);
  191. fclose($fh);
  192. }
  193. }
  194.  
  195. /**
  196. * Observe Cgiapp2_Exception
  197. */
  198. Cgiapp2_Exception::attach('Cgiapp2_Exception_Observer_Log');

Documentation generated on Sat, 03 Jun 2006 10:48:49 -0400 by phpDocumentor 1.3.0RC5