PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Log.php

https://github.com/nisheeth-barthwal/maveric
PHP | 142 lines | 84 code | 31 blank | 27 comment | 13 complexity | 7987225dd45b261b58cef644c2146926 MD5 | raw file
  1. <?php
  2. /**
  3. * Author : Nisheeth Barthwal
  4. * Date : 18 Jul 2013
  5. * Project: maveric
  6. *
  7. */
  8. namespace sys\core;
  9. class Log
  10. {
  11. /**
  12. * Singleton instance
  13. * @var null
  14. */
  15. protected static $_instance = null;
  16. /**
  17. * Returns the singleton instance for the class
  18. * @return Log
  19. */
  20. public static function instance()
  21. {
  22. if (self::$_instance === null)
  23. {
  24. self::$_instance = new self();
  25. }
  26. return self::$_instance;
  27. }
  28. protected $_enabled = true;
  29. protected $_log_path;
  30. protected $_threshold = 1;
  31. protected $_threshold_max = 0;
  32. protected $_threshold_array = array();
  33. protected $_date_fmt = 'Y-m-d H:i:s';
  34. protected $_file_ext;
  35. protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4);
  36. // --------------------------------------------------------------------
  37. public function __construct()
  38. {
  39. $config = Config::instance();
  40. // Check if logging is enabled. Default: enabled
  41. $this->_enabled = $config->item('log', 'enabled') !== null? (bool)$config->item('log', 'enabled'): true;
  42. if ($this->_enabled)
  43. {
  44. // Assign a default log path if not specified in config
  45. $this->_log_path = ($config->item('log', 'path') !== '') ? $config->item('log', 'path') : APPPATH.'logs/';
  46. // Assign a default log extension if not specified in config
  47. $this->_file_ext = ($config->item('log', 'file_extension') && $config->item('log', 'file_extension') !== '')
  48. ? ltrim($config->item('log', 'file_extension'), '.') : 'php';
  49. // Create the log directory if it doesn't exist
  50. file_exists($this->_log_path) OR mkdir($this->_log_path, DIR_WRITE_MODE, true);
  51. // Wait what? We failed to create the directory. Abort!
  52. if ( ! is_dir($this->_log_path))
  53. {
  54. Exception::trace('Could not create the logging directory');
  55. }
  56. if (is_numeric($config->item('log', 'threshold')))
  57. {
  58. $this->_threshold = (int) $config->item('log', 'threshold');
  59. }
  60. elseif (is_array($config->item('log', 'threshold')))
  61. {
  62. $this->_threshold = $this->_threshold_max;
  63. $this->_threshold_array = array_flip($config->item('log', 'threshold'));
  64. }
  65. if ($config->item('log', 'date_format') !== '')
  66. {
  67. $this->_date_fmt = $config->item('log', 'date_format');
  68. }
  69. }
  70. }
  71. // --------------------------------------------------------------------
  72. /**
  73. * Write the message to the log file
  74. * @param string $level
  75. * @param string $msg
  76. * @return bool
  77. */
  78. public function write($level = 'error', $msg)
  79. {
  80. if ($this->_enabled === false)
  81. {
  82. return false;
  83. }
  84. $level = strtoupper($level);
  85. if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
  86. && ! isset($this->_threshold_array[$this->_levels[$level]]))
  87. {
  88. return false;
  89. }
  90. $filepath = $this->_log_path.'log-'.date('Y-m-d').'.'.$this->_file_ext;
  91. $message = '';
  92. if ( ! file_exists($filepath))
  93. {
  94. $newfile = true;
  95. }
  96. if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
  97. {
  98. return false;
  99. }
  100. $message .= $level.' '.($level === 'INFO' ? ' -' : '-').' '.date($this->_date_fmt).' --> '.$msg."\n";
  101. flock($fp, LOCK_EX);
  102. fwrite($fp, $message);
  103. flock($fp, LOCK_UN);
  104. fclose($fp);
  105. if (isset($newfile) && $newfile === true)
  106. {
  107. @chmod($filepath, FILE_WRITE_MODE);
  108. }
  109. return true;
  110. }
  111. }