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

/classes/Log.php

http://github.com/joshtronic/pickles
PHP | 152 lines | 61 code | 15 blank | 76 comment | 4 complexity | 2e1cce5c697a3c9126f89d36fc25e6e2 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Logging System for PICKLES
  4. *
  5. * PHP version 5
  6. *
  7. * Licensed under The MIT License
  8. * Redistribution of these files must retain the above copyright notice.
  9. *
  10. * @author Josh Sherman <josh@gravityblvd.com>
  11. * @copyright Copyright 2007-2011, Josh Sherman
  12. * @license http://www.opensource.org/licenses/mit-license.html
  13. * @package PICKLES
  14. * @link http://p.ickl.es
  15. */
  16. /**
  17. * Log Class
  18. *
  19. * Standardized logging methods for ease of reporting.
  20. */
  21. class Log
  22. {
  23. /**
  24. * Log Information
  25. *
  26. * @static
  27. * @param string $message message to log
  28. * @return boolean whether or not the write was successful
  29. */
  30. public static function information($message)
  31. {
  32. return self::write('information', $message);
  33. }
  34. /**
  35. * Log Warning
  36. *
  37. * @static
  38. * @param string $message message to log
  39. * @return boolean whether or not the write was successful
  40. */
  41. public static function warning($message)
  42. {
  43. return self::write('warning', $message);
  44. }
  45. /**
  46. * Log Error
  47. *
  48. * @static
  49. * @param string $message message to log
  50. * @return boolean whether or not the write was successful
  51. */
  52. public static function error($message)
  53. {
  54. return self::write('error', $message);
  55. }
  56. /**
  57. * Log Slow Query
  58. *
  59. * @static
  60. * @param string $message message to log
  61. * @return boolean whether or not the write was successful
  62. */
  63. public static function slowQuery($message)
  64. {
  65. return self::write('slow_query', $message);
  66. }
  67. /**
  68. * Log Credit Card Transaction
  69. *
  70. * @static
  71. * @param string $message message to log
  72. * @return boolean whether or not the write was successful
  73. */
  74. public static function transaction($message)
  75. {
  76. return self::write('transaction', $message);
  77. }
  78. /**
  79. * Log PHP Error
  80. *
  81. * @static
  82. * @param string $message message to log
  83. * @return boolean whether or not the write was successful
  84. */
  85. public static function phpError($message, $time = false)
  86. {
  87. return self::write('php_error', $message, false, $time);
  88. }
  89. /**
  90. * Log SQL Query
  91. *
  92. * @static
  93. * @param string $message message to log
  94. * @return boolean whether or not the write was successful
  95. */
  96. public static function query($message)
  97. {
  98. return self::write('query', $message);
  99. }
  100. /**
  101. * Write Message to Log File
  102. *
  103. * @static
  104. * @access private
  105. * @param string $message message to log
  106. * @return boolean whether or not the write was successful
  107. */
  108. private static function write($log_type, $message, $format = true, $time = false)
  109. {
  110. $log_path = LOG_PATH . date('Y/m/d/', ($time == false ? time() : $time));
  111. try
  112. {
  113. if (!file_exists($log_path))
  114. {
  115. mkdir($log_path, 0755, true);
  116. }
  117. $log_file = $log_path . $log_type . '.log';
  118. $message .= "\n";
  119. if ($format == true)
  120. {
  121. $backtrace = debug_backtrace();
  122. rsort($backtrace);
  123. $frame = $backtrace[strpos($backtrace[0]['file'], 'index.php') === false ? 0 : 1];
  124. return file_put_contents($log_file, date('H:i:s') . ' ' . str_replace(getcwd(), '', $frame['file']) . ':' . $frame['line'] . ' ' . $message, FILE_APPEND);
  125. }
  126. else
  127. {
  128. return file_put_contents($log_file, $message, FILE_APPEND);
  129. }
  130. }
  131. catch (ErrorException $exception)
  132. {
  133. return false;
  134. }
  135. }
  136. }
  137. ?>