PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/log.php

https://bitbucket.org/sriedel/iccrm-wip
PHP | 192 lines | 95 code | 30 blank | 67 comment | 12 complexity | 0a6e7b9a376010e7859d7c537bdc960f MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2012 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Log Class
  15. *
  16. * @package Fuel
  17. * @category Logging
  18. * @author Phil Sturgeon
  19. * @link http://docs.fuelphp.com/classes/log.html
  20. */
  21. class Log
  22. {
  23. public static function _init()
  24. {
  25. \Config::load('file', true);
  26. // make sure the configured chmod values are octal
  27. $chmod = \Config::get('file.chmod.folders', 0777);
  28. is_string($chmod) and \Config::set('file.chmod.folders', octdec($chmod));
  29. $chmod = \Config::get('file.chmod.files', 0666);
  30. is_string($chmod) and \Config::set('file.chmod.files', octdec($chmod));
  31. }
  32. /**
  33. * Logs a message with the Info Log Level
  34. *
  35. * @param string $msg The log message
  36. * @param string $method The method that logged
  37. * @return bool If it was successfully logged
  38. */
  39. public static function info($msg, $method = null)
  40. {
  41. return static::write(\Fuel::L_INFO, $msg, $method);
  42. }
  43. /**
  44. * Logs a message with the Debug Log Level
  45. *
  46. * @param string $msg The log message
  47. * @param string $method The method that logged
  48. * @return bool If it was successfully logged
  49. */
  50. public static function debug($msg, $method = null)
  51. {
  52. return static::write(\Fuel::L_DEBUG, $msg, $method);
  53. }
  54. /**
  55. * Logs a message with the Warning Log Level
  56. *
  57. * @param string $msg The log message
  58. * @param string $method The method that logged
  59. * @return bool If it was successfully logged
  60. */
  61. public static function warning($msg, $method = null)
  62. {
  63. return static::write(\Fuel::L_WARNING, $msg, $method);
  64. }
  65. /**
  66. * Logs a message with the Error Log Level
  67. *
  68. * @param string $msg The log message
  69. * @param string $method The method that logged
  70. * @return bool If it was successfully logged
  71. */
  72. public static function error($msg, $method = null)
  73. {
  74. return static::write(\Fuel::L_ERROR, $msg, $method);
  75. }
  76. /**
  77. * Write Log File
  78. *
  79. * Generally this function will be called using the global log_message() function
  80. *
  81. * @access public
  82. * @param int|string the error level
  83. * @param string the error message
  84. * @param string information about the method
  85. * @return bool
  86. */
  87. public static function write($level, $msg, $method = null)
  88. {
  89. // defined default error labels
  90. static $labels = array(
  91. 1 => 'Error',
  92. 2 => 'Warning',
  93. 3 => 'Debug',
  94. 4 => 'Info',
  95. );
  96. // get the levels defined to be logged
  97. $loglabels = \Config::get('log_threshold');
  98. // bail out if we don't need logging at all
  99. if ($loglabels == \Fuel::L_NONE)
  100. {
  101. return false;
  102. }
  103. // if it's not an array, assume it's an "up to" level
  104. if ( ! is_array($loglabels))
  105. {
  106. $loglabels = array_keys(array_slice($labels, 0, $loglabels, true));
  107. }
  108. // if $level is string, it is custom level.
  109. if (is_int($level))
  110. {
  111. // do we need to log the message with this level?
  112. if ( ! in_array($level, $loglabels))
  113. {
  114. return false;
  115. }
  116. // store the label for this level for future use
  117. $level = $labels[$level];
  118. }
  119. // if profiling is active log the message to the profile
  120. if (Config::get('profiling'))
  121. {
  122. \Console::log($method.' - '.$msg);
  123. }
  124. // and write it to the logfile
  125. $filepath = \Config::get('log_path').date('Y/m').'/';
  126. if ( ! is_dir($filepath))
  127. {
  128. $old = umask(0);
  129. mkdir($filepath, \Config::get('file.chmod.folders', 0777), true);
  130. umask($old);
  131. }
  132. $filename = $filepath.date('d').'.php';
  133. $message = '';
  134. if ( ! $exists = file_exists($filename))
  135. {
  136. $message .= "<"."?php defined('COREPATH') or exit('No direct script access allowed'); ?".">".PHP_EOL.PHP_EOL;
  137. }
  138. if ( ! $fp = @fopen($filename, 'a'))
  139. {
  140. return false;
  141. }
  142. $call = '';
  143. if ( ! empty($method))
  144. {
  145. $call .= $method;
  146. }
  147. $message .= $level.' '.(($level == 'info') ? ' -' : '-').' ';
  148. $message .= date(\Config::get('log_date_format'));
  149. $message .= ' --> '.(empty($call) ? '' : $call.' - ').$msg.PHP_EOL;
  150. flock($fp, LOCK_EX);
  151. fwrite($fp, $message);
  152. flock($fp, LOCK_UN);
  153. fclose($fp);
  154. if ( ! $exists)
  155. {
  156. $old = umask(0);
  157. @chmod($filename, \Config::get('file.chmod.files', 0666));
  158. umask($old);
  159. }
  160. return true;
  161. }
  162. }