PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/mashup_workshop/examples/tropo/applications/cloudfiles-poster/KLogger/src/KLogger.php

https://github.com/hack4reno/hack4reno2011
PHP | 395 lines | 177 code | 35 blank | 183 comment | 19 complexity | e37a88d56a67a29ee9ec03eaf55454c0 MD5 | raw file
  1. <?php
  2. /**
  3. * Finally, a light, permissions-checking logging class.
  4. *
  5. * Originally written for use with wpSearch
  6. *
  7. * Usage:
  8. * $log = new KLogger('/var/log/', KLogger::INFO );
  9. * $log->logInfo('Returned a million search results'); //Prints to the log file
  10. * $log->logFatal('Oh dear.'); //Prints to the log file
  11. * $log->logDebug('x = 5'); //Prints nothing due to current severity threshhold
  12. *
  13. * @author Kenny Katzgrau <katzgrau@gmail.com>
  14. * @since July 26, 2008
  15. * @link http://codefury.net
  16. * @version 0.1
  17. */
  18. /**
  19. * Class documentation
  20. */
  21. class KLogger
  22. {
  23. /**
  24. * Error severity, from low to high. From BSD syslog RFC, secion 4.1.1
  25. * @link http://www.faqs.org/rfcs/rfc3164.html
  26. */
  27. const EMERG = 0; // Emergency: system is unusable
  28. const ALERT = 1; // Alert: action must be taken immediately
  29. const CRIT = 2; // Critical: critical conditions
  30. const ERR = 3; // Error: error conditions
  31. const WARN = 4; // Warning: warning conditions
  32. const NOTICE = 5; // Notice: normal but significant condition
  33. const INFO = 6; // Informational: informational messages
  34. const DEBUG = 7; // Debug: debug messages
  35. //custom logging level
  36. /**
  37. * Log nothing at all
  38. */
  39. const OFF = 8;
  40. /**
  41. * Alias for CRIT
  42. * @deprecated
  43. */
  44. const FATAL = 2;
  45. /**
  46. * Internal status codes
  47. */
  48. const STATUS_LOG_OPEN = 1;
  49. const STATUS_OPEN_FAILED = 2;
  50. const STATUS_LOG_CLOSED = 3;
  51. /**
  52. * Current status of the log file
  53. * @var integer
  54. */
  55. private $_logStatus = self::STATUS_LOG_CLOSED;
  56. /**
  57. * Holds messages generated by the class
  58. * @var array
  59. */
  60. private $_messageQueue = array();
  61. /**
  62. * Path to the log file
  63. * @var string
  64. */
  65. private $_logFilePath = null;
  66. /**
  67. * Current minimum logging threshold
  68. * @var integer
  69. */
  70. private $_severityThreshold = self::INFO;
  71. /**
  72. * This holds the file handle for this instance's log file
  73. * @var resource
  74. */
  75. private $_fileHandle = null;
  76. /**
  77. * Standard messages produced by the class. Can be modified for il8n
  78. * @var array
  79. */
  80. private $_messages = array(
  81. //'writefail' => 'The file exists, but could not be opened for writing. Check that appropriate permissions have been set.',
  82. 'writefail' => 'The file could not be written to. Check that appropriate permissions have been set.',
  83. 'opensuccess' => 'The log file was opened successfully.',
  84. 'openfail' => 'The file could not be opened. Check permissions.',
  85. );
  86. /**
  87. * Default severity of log messages, if not specified
  88. * @var integer
  89. */
  90. private static $_defaultSeverity = self::DEBUG;
  91. /**
  92. * Valid PHP date() format string for log timestamps
  93. * @var string
  94. */
  95. private static $_dateFormat = 'Y-m-d G:i:s';
  96. /**
  97. * Octal notation for default permissions of the log file
  98. * @var integer
  99. */
  100. private static $_defaultPermissions = 0777;
  101. /**
  102. * Array of KLogger instances, part of Singleton pattern
  103. * @var array
  104. */
  105. private static $instances = array();
  106. /**
  107. * Partially implements the Singleton pattern. Each $logDirectory gets one
  108. * instance.
  109. *
  110. * @param string $logDirectory File path to the logging directory
  111. * @param integer $severity One of the pre-defined severity constants
  112. * @return KLogger
  113. */
  114. public static function instance($logDirectory = false, $severity = false)
  115. {
  116. if ($severity === false) {
  117. $severity = self::$_defaultSeverity;
  118. }
  119. if ($logDirectory === false) {
  120. if (count(self::$instances) > 0) {
  121. return current(self::$instances);
  122. } else {
  123. $logDirectory = dirname(__FILE__);
  124. }
  125. }
  126. if (in_array($logDirectory, self::$instances)) {
  127. return self::$instances[$logDirectory];
  128. }
  129. self::$instances[$logDirectory] = new self($logDirectory, $severity);
  130. return self::$instances[$logDirectory];
  131. }
  132. /**
  133. * Class constructor
  134. *
  135. * @param string $logDirectory File path to the logging directory
  136. * @param integer $severity One of the pre-defined severity constants
  137. * @return void
  138. */
  139. public function __construct($logDirectory, $severity)
  140. {
  141. $logDirectory = rtrim($logDirectory, '\\/');
  142. if ($severity === self::OFF) {
  143. return;
  144. }
  145. $this->_logFilePath = $logDirectory
  146. . DIRECTORY_SEPARATOR
  147. . 'log_'
  148. . date('Y-m-d')
  149. . '.txt';
  150. $this->_severityThreshold = $severity;
  151. if (!file_exists($logDirectory)) {
  152. mkdir($logDirectory, self::$_defaultPermissions, true);
  153. }
  154. if (file_exists($this->_logFilePath) && !is_writable($this->_logFilePath)) {
  155. $this->_logStatus = self::STATUS_OPEN_FAILED;
  156. $this->_messageQueue[] = $this->_messages['writefail'];
  157. return;
  158. }
  159. if (($this->_fileHandle = fopen($this->_logFilePath, 'a'))) {
  160. $this->_logStatus = self::STATUS_LOG_OPEN;
  161. $this->_messageQueue[] = $this->_messages['opensuccess'];
  162. } else {
  163. $this->_logStatus = self::STATUS_OPEN_FAILED;
  164. $this->_messageQueue[] = $this->_messages['openfail'];
  165. }
  166. }
  167. /**
  168. * Class destructor
  169. */
  170. public function __destruct()
  171. {
  172. if ($this->_fileHandle) {
  173. fclose($this->_fileHandle);
  174. }
  175. }
  176. /**
  177. * Writes a $line to the log with a severity level of DEBUG
  178. *
  179. * @param string $line Information to log
  180. * @return void
  181. */
  182. public function logDebug($line)
  183. {
  184. $this->log($line, self::DEBUG);
  185. }
  186. /**
  187. * Returns (and removes) the last message from the queue.
  188. * @return string
  189. */
  190. public function getMessage()
  191. {
  192. return array_pop($this->_messageQueue);
  193. }
  194. /**
  195. * Returns the entire message queue (leaving it intact)
  196. * @return array
  197. */
  198. public function getMessages()
  199. {
  200. return $this->_messageQueue;
  201. }
  202. /**
  203. * Empties the message queue
  204. * @return void
  205. */
  206. public function clearMessages()
  207. {
  208. $this->_messageQueue = array();
  209. }
  210. /**
  211. * Sets the date format used by all instances of KLogger
  212. *
  213. * @param string $dateFormat Valid format string for date()
  214. */
  215. public static function setDateFormat($dateFormat)
  216. {
  217. self::$_dateFormat = $dateFormat;
  218. }
  219. /**
  220. * Writes a $line to the log with a severity level of INFO. Any information
  221. * can be used here, or it could be used with E_STRICT errors
  222. *
  223. * @param string $line Information to log
  224. * @return void
  225. */
  226. public function logInfo($line)
  227. {
  228. $this->log($line, self::INFO);
  229. }
  230. /**
  231. * Writes a $line to the log with a severity level of NOTICE. Generally
  232. * corresponds to E_STRICT, E_NOTICE, or E_USER_NOTICE errors
  233. *
  234. * @param string $line Information to log
  235. * @return void
  236. */
  237. public function logNotice($line)
  238. {
  239. $this->log($line, self::NOTICE);
  240. }
  241. /**
  242. * Writes a $line to the log with a severity level of WARN. Generally
  243. * corresponds to E_WARNING, E_USER_WARNING, E_CORE_WARNING, or
  244. * E_COMPILE_WARNING
  245. *
  246. * @param string $line Information to log
  247. * @return void
  248. */
  249. public function logWarn($line)
  250. {
  251. $this->log($line, self::WARN);
  252. }
  253. /**
  254. * Writes a $line to the log with a severity level of ERR. Most likely used
  255. * with E_RECOVERABLE_ERROR
  256. *
  257. * @param string $line Information to log
  258. * @return void
  259. */
  260. public function logError($line)
  261. {
  262. $this->log($line, self::ERR);
  263. }
  264. /**
  265. * Writes a $line to the log with a severity level of FATAL. Generally
  266. * corresponds to E_ERROR, E_USER_ERROR, E_CORE_ERROR, or E_COMPILE_ERROR
  267. *
  268. * @param string $line Information to log
  269. * @return void
  270. * @deprecated Use logCrit
  271. */
  272. public function logFatal($line)
  273. {
  274. $this->log($line, self::FATAL);
  275. }
  276. /**
  277. * Writes a $line to the log with a severity level of ALERT.
  278. *
  279. * @param string $line Information to log
  280. * @return void
  281. */
  282. public function logAlert($line)
  283. {
  284. $this->log($line, self::ALERT);
  285. }
  286. /**
  287. * Writes a $line to the log with a severity level of CRIT.
  288. *
  289. * @param string $line Information to log
  290. * @return void
  291. */
  292. public function logCrit($line)
  293. {
  294. $this->log($line, self::CRIT);
  295. }
  296. /**
  297. * Writes a $line to the log with a severity level of EMERG.
  298. *
  299. * @param string $line Information to log
  300. * @return void
  301. */
  302. public function logEmerg($line)
  303. {
  304. $this->log($line, self::EMERG);
  305. }
  306. /**
  307. * Writes a $line to the log with the given severity
  308. *
  309. * @param string $line Text to add to the log
  310. * @param integer $severity Severity level of log message (use constants)
  311. */
  312. public function log($line, $severity)
  313. {
  314. if ($this->_severityThreshold >= $severity) {
  315. $status = $this->_getTimeLine($severity);
  316. $this->writeFreeFormLine("$status $line \n");
  317. }
  318. }
  319. /**
  320. * Writes a line to the log without prepending a status or timestamp
  321. *
  322. * @param string $line Line to write to the log
  323. * @return void
  324. */
  325. public function writeFreeFormLine($line)
  326. {
  327. if ($this->_logStatus == self::STATUS_LOG_OPEN
  328. && $this->_severityThreshold != self::OFF) {
  329. if (fwrite($this->_fileHandle, $line) === false) {
  330. $this->_messageQueue[] = $this->_messages['writefail'];
  331. }
  332. }
  333. }
  334. private function _getTimeLine($level)
  335. {
  336. $time = date(self::$_dateFormat);
  337. switch ($level) {
  338. case self::EMERG:
  339. return "$time - EMERG -->";
  340. case self::ALERT:
  341. return "$time - ALERT -->";
  342. case self::CRIT:
  343. return "$time - CRIT -->";
  344. case self::FATAL: # FATAL is an alias of CRIT
  345. return "$time - FATAL -->";
  346. case self::NOTICE:
  347. return "$time - NOTICE -->";
  348. case self::INFO:
  349. return "$time - INFO -->";
  350. case self::WARN:
  351. return "$time - WARN -->";
  352. case self::DEBUG:
  353. return "$time - DEBUG -->";
  354. case self::ERR:
  355. return "$time - ERROR -->";
  356. default:
  357. return "$time - LOG -->";
  358. }
  359. }
  360. }