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_Error
  20. */
  21. require_once 'Cgiapp2/Error.class.php';
  22.  
  23. /**
  24. * Implements Cgiapp2_Error_Observer_Interface
  25. */
  26. require_once 'Cgiapp2/Error/Observer/Interface.class.php';
  27.  
  28. /**
  29. * Cgiapp2_Error_Observer_Log
  30. *
  31. * {@link Cgiapp2_Error} observer. Writes PHP error information to a log file.
  32. *
  33. * Sample usage:
  34. * <code>
  35. * require_once 'Cgiapp2/Error.class.php';
  36. * require_once 'Cgiapp2/Error/Observer/Log.class.php';
  37. *
  38. * // Set the log file to '/tmp/errors/log'
  39. * Cgiapp2_Error_Observer_Log::setFile('/tmp/errors/log');
  40. *
  41. * // Handle PHP errors
  42. * set_error_handler(array('Cgiapp2_Error', 'handler'));
  43. *
  44. * trigger_error('Log this...', E_USER_WARNING);
  45. * </code>
  46. *
  47. * @package Cgiapp2
  48. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>
  49. * @copyright (c) 2006 - Present, Matthew Weier O'Phinney
  50. * <mweierophinney@gmail.com>
  51. * @version @release-version@
  52. */
  53. class Cgiapp2_Error_Observer_Log implements Cgiapp2_Error_Observer_Interface
  54. {
  55. /**
  56. * Log file. Defaults to '/tmp/cgiapp_error.log'
  57. * @var string
  58. * @access public
  59. */
  60. public $file;
  61.  
  62. /**
  63. * sprintf() style format for log message. Defaults to
  64. * "[%s] [%s:%c] %c: %s\n" ([date] [file:line] code: message)
  65. * @var string
  66. * @access public
  67. */
  68. public $format;
  69.  
  70. /**
  71. * Singleton instance
  72. * @var bool|Cgiapp2_Error_Observer_Log
  73. * @static
  74. * @access private
  75. */
  76. private static $instance = false;
  77.  
  78. /**
  79. * Constructor
  80. *
  81. * @param mixed $file File to which to log; must be writeable
  82. * @param mixed $format Defaults to empty; log format (printf compatible)
  83. * @access public
  84. * @return void
  85. * @throws Exception if log file is not writable
  86. */
  87. private function __construct($file, $format)
  88. {
  89. $this->file = $file;
  90. $this->format = $format;
  91. }
  92.  
  93. /**
  94. * Singleton
  95. *
  96. * @static
  97. * @access public
  98. * @param string $file Location of log
  99. * @param string $format sprintf() format for log
  100. * @return void
  101. * @throws Exception if unable to write to file
  102. */
  103. public static function getInstance($file = null, $format = null)
  104. {
  105. if (self::$instance) {
  106. return self::$instance;
  107. }
  108.  
  109. if (empty($file)) {
  110. $file = '/tmp/cgiapp_exception.log';
  111. }
  112. if (file_exists($file) && !is_writable($file)) {
  113. throw new Exception(__CLASS__ . ' file \'' . $file . '\' is not writable');
  114. } elseif (!file_exists($file) && !is_writable(dirname($file))) {
  115. throw new Exception(__CLASS__ . ' file \'' . $file . '\' can not be created');
  116. }
  117.  
  118. if (empty($format)) {
  119. $format = "[%s] [%s:%c] %c: %s\n";
  120. }
  121.  
  122. self::$instance = new Cgiapp2_Error_Observer_Log($file, $format);
  123.  
  124. return self::$instance;
  125. }
  126.  
  127. /**
  128. * Set the log file
  129. *
  130. * @static
  131. * @access public
  132. * @param string $file
  133. * @return bool
  134. */
  135. public static function setFile($file)
  136. {
  137. if (file_exists($file) && !is_writable($file)) {
  138. return false;
  139. } elseif (!file_exists($file) && !is_writable(dirname($file))) {
  140. return false;
  141. }
  142.  
  143. self::getInstance()->file = $file;
  144. return true;
  145. }
  146.  
  147. /**
  148. * Set the log format
  149. *
  150. * @static
  151. * @access public
  152. * @param mixed $format
  153. * @return void
  154. */
  155. public static function setFormat($format)
  156. {
  157. self::getInstance()->format = $format;
  158. }
  159.  
  160. /**
  161. * Log an error
  162. *
  163. * @static
  164. * @access public
  165. * @param Cgiapp2_Error $e
  166. * @return void
  167. * @throws Exception if unable to append file or obtain lock
  168. */
  169. public static function event(Cgiapp2_Error $e)
  170. {
  171. $handler = self::getInstance();
  172. $log = $handler->file;
  173. $fh = fopen($log, 'a');
  174. if (false === $fh) {
  175. throw new Exception(__CLASS__ . ' unable to append to log file');
  176. }
  177. if (!flock($fh, LOCK_EX)) {
  178. throw new Exception(__CLASS__ . ' unable to lock log file');
  179. }
  180.  
  181. $date = date('Y-m-d H:i:s');
  182. $file = $e->errfile;
  183. $line = $e->errline;
  184. $code = $e->errno;
  185. $msg = $e->errstr;
  186.  
  187. $msg = sprintf($handler->format, $date, $file, $line, $code, $msg);
  188. fwrite($fh, $msg);
  189.  
  190. flock($fh, LOCK_UN);
  191. fclose($fh);
  192. }
  193. }
  194.  
  195. /**
  196. * Observe Cgiapp2_Error
  197. */
  198. Cgiapp2_Error::attach('Cgiapp2_Error_Observer_Log');

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