PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Log.php

https://gitlab.com/betanurlaila/UI_onlineshop
PHP | 247 lines | 99 code | 33 blank | 115 comment | 17 complexity | 6fb149d77d95a7979d051324def910b7 MD5 | raw file
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Logging Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Logging
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/general/errors.html
  47. */
  48. class CI_Log {
  49. /**
  50. * Path to save log files
  51. *
  52. * @var string
  53. */
  54. protected $_log_path;
  55. /**
  56. * File permissions
  57. *
  58. * @var int
  59. */
  60. protected $_file_permissions = 0644;
  61. /**
  62. * Level of logging
  63. *
  64. * @var int
  65. */
  66. protected $_threshold = 1;
  67. /**
  68. * Array of threshold levels to log
  69. *
  70. * @var array
  71. */
  72. protected $_threshold_array = array();
  73. /**
  74. * Format of timestamp for log files
  75. *
  76. * @var string
  77. */
  78. protected $_date_fmt = 'Y-m-d H:i:s';
  79. /**
  80. * Filename extension
  81. *
  82. * @var string
  83. */
  84. protected $_file_ext;
  85. /**
  86. * Whether or not the logger can write to the log files
  87. *
  88. * @var bool
  89. */
  90. protected $_enabled = TRUE;
  91. /**
  92. * Predefined logging levels
  93. *
  94. * @var array
  95. */
  96. protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4);
  97. // --------------------------------------------------------------------
  98. /**
  99. * Class constructor
  100. *
  101. * @return void
  102. */
  103. public function __construct()
  104. {
  105. $config =& get_config();
  106. $this->_log_path = ($config['log_path'] !== '') ? $config['log_path'] : APPPATH.'logs/';
  107. $this->_file_ext = (isset($config['log_file_extension']) && $config['log_file_extension'] !== '')
  108. ? ltrim($config['log_file_extension'], '.') : 'php';
  109. file_exists($this->_log_path) OR mkdir($this->_log_path, 0755, TRUE);
  110. if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
  111. {
  112. $this->_enabled = FALSE;
  113. }
  114. if (is_numeric($config['log_threshold']))
  115. {
  116. $this->_threshold = (int) $config['log_threshold'];
  117. }
  118. elseif (is_array($config['log_threshold']))
  119. {
  120. $this->_threshold = 0;
  121. $this->_threshold_array = array_flip($config['log_threshold']);
  122. }
  123. if ( ! empty($config['log_date_format']))
  124. {
  125. $this->_date_fmt = $config['log_date_format'];
  126. }
  127. if ( ! empty($config['log_file_permissions']) && is_int($config['log_file_permissions']))
  128. {
  129. $this->_file_permissions = $config['log_file_permissions'];
  130. }
  131. }
  132. // --------------------------------------------------------------------
  133. /**
  134. * Write Log File
  135. *
  136. * Generally this function will be called using the global log_message() function
  137. *
  138. * @param string $level The error level: 'error', 'debug' or 'info'
  139. * @param string $msg The error message
  140. * @return bool
  141. */
  142. public function write_log($level, $msg)
  143. {
  144. if ($this->_enabled === FALSE)
  145. {
  146. return FALSE;
  147. }
  148. $level = strtoupper($level);
  149. if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
  150. && ! isset($this->_threshold_array[$this->_levels[$level]]))
  151. {
  152. return FALSE;
  153. }
  154. $filepath = $this->_log_path.'log-'.date('Y-m-d').'.'.$this->_file_ext;
  155. $message = '';
  156. if ( ! file_exists($filepath))
  157. {
  158. $newfile = TRUE;
  159. // Only add protection to php files
  160. if ($this->_file_ext === 'php')
  161. {
  162. $message .= "<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>\n\n";
  163. }
  164. }
  165. if ( ! $fp = @fopen($filepath, 'ab'))
  166. {
  167. return FALSE;
  168. }
  169. flock($fp, LOCK_EX);
  170. // Instantiating DateTime with microseconds appended to initial date is needed for proper support of this format
  171. if (strpos($this->_date_fmt, 'u') !== FALSE)
  172. {
  173. $microtime_full = microtime(TRUE);
  174. $microtime_short = sprintf("%06d", ($microtime_full - floor($microtime_full)) * 1000000);
  175. $date = new DateTime(date('Y-m-d H:i:s.'.$microtime_short, $microtime_full));
  176. $date = $date->format($this->_date_fmt);
  177. }
  178. else
  179. {
  180. $date = date($this->_date_fmt);
  181. }
  182. $message .= $this->_format_line($level, $date, $msg);
  183. for ($written = 0, $length = strlen($message); $written < $length; $written += $result)
  184. {
  185. if (($result = fwrite($fp, substr($message, $written))) === FALSE)
  186. {
  187. break;
  188. }
  189. }
  190. flock($fp, LOCK_UN);
  191. fclose($fp);
  192. if (isset($newfile) && $newfile === TRUE)
  193. {
  194. chmod($filepath, $this->_file_permissions);
  195. }
  196. return is_int($result);
  197. }
  198. // --------------------------------------------------------------------
  199. /**
  200. * Format the log line.
  201. *
  202. * This is for extensibility of log formatting
  203. * If you want to change the log format, extend the CI_Log class and override this method
  204. *
  205. * @param string $level The error level
  206. * @param string $date Formatted date string
  207. * @param string $msg The log message
  208. * @return string Formatted log line with a new line character '\n' at the end
  209. */
  210. protected function _format_line($level, $date, $message)
  211. {
  212. return $level.' - '.$date.' --> '.$message."\n";
  213. }
  214. }