PPLoggingManager.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Simple Logging Manager.
  4. * This does an error_log for now
  5. * Potential frameworks to use are PEAR logger, log4php from Apache
  6. */
  7. class PPLoggingLevel {
  8. // FINE Logging Level
  9. const FINE = 3;
  10. // INFO Logging Level
  11. const INFO = 2;
  12. // WARN Logging Level
  13. const WARN = 1;
  14. // ERROR Logging Level
  15. const ERROR = 0;
  16. }
  17. class PPLoggingManager {
  18. // Default Logging Level
  19. const DEFAULT_LOGGING_LEVEL = 0;
  20. // Logger name
  21. private $loggerName;
  22. // Log enabled
  23. private $isLoggingEnabled;
  24. // Configured logging level
  25. private $loggingLevel;
  26. // Configured logging file
  27. private $loggerFile;
  28. public function __construct($loggerName) {
  29. $this->loggerName = $loggerName;
  30. $config = PPConfigManager::getInstance();
  31. $this->loggerFile = ($config->get('log.FileName')) ? $config->get('log.FileName') : ini_get('error_log');
  32. $loggingEnabled = $config->get('log.LogEnabled');
  33. $this->isLoggingEnabled = (isset($loggingEnabled)) ? $loggingEnabled : false;
  34. $loggingLevel = strtoupper($config->get('log.LogLevel'));
  35. $this->loggingLevel = (isset($loggingLevel) && defined("PPLoggingLevel::$loggingLevel")) ? constant("PPLoggingLevel::$loggingLevel") : PPLoggingManager::DEFAULT_LOGGING_LEVEL;
  36. }
  37. public function log($message, $level=PPLoggingLevel::INFO) {
  38. if($this->isLoggingEnabled && ($level <= $this->loggingLevel)) {
  39. error_log( $this->loggerName . ": $message\n", 3, $this->loggerFile);
  40. }
  41. }
  42. public function error($message) {
  43. $this->log($message, PPLoggingLevel::ERROR);
  44. }
  45. public function warning($message) {
  46. $this->log($message, PPLoggingLevel::WARN);
  47. }
  48. public function info($message) {
  49. $this->log($message, PPLoggingLevel::INFO);
  50. }
  51. public function fine($message) {
  52. $this->log($message, PPLoggingLevel::FINE);
  53. }
  54. }