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

/system/core/Log.php

https://github.com/lmorchard/friendfeedarchiver
PHP | 102 lines | 51 code | 14 blank | 37 comment | 6 complexity | e29f473a6148ac6df48478b9cedc686b MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Message file logging class.
  4. *
  5. * $Id: Log.php 1930 2008-02-05 22:35:57Z armen $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. final class Log {
  13. private static $log_directory;
  14. private static $types = array(1 => 'error', 2 => 'debug', 3 => 'info');
  15. private static $messages = array();
  16. /**
  17. * Set the the log directory. The log directory is determined by Kohana::setup.
  18. *
  19. * @param string full log directory path
  20. * @return void
  21. */
  22. public static function directory($directory)
  23. {
  24. if (self::$log_directory === NULL)
  25. {
  26. // Set the log directory if it has not already been set
  27. self::$log_directory = rtrim($directory, '/').'/';
  28. }
  29. }
  30. /**
  31. * Add a log message.
  32. *
  33. * @param string info, debug, or error
  34. * @param string message to be logged
  35. * @return void
  36. */
  37. public static function add($type, $message)
  38. {
  39. self::$messages[strtolower($type)][] = array
  40. (
  41. date(Config::item('log.format')),
  42. strip_tags($message)
  43. );
  44. }
  45. /**
  46. * Write the current log to a file.
  47. *
  48. * @return void
  49. */
  50. public static function write()
  51. {
  52. // Set the log threshold
  53. $threshold = Config::item('log.threshold');
  54. // Don't log if there is nothing to log to
  55. if ($threshold < 1 OR count(self::$messages) === 0) return;
  56. // Set the log filename
  57. $filename = self::$log_directory.date('Y-m-d').'.log'.EXT;
  58. // Compile the messages
  59. $messages = '';
  60. foreach(self::$messages as $type => $data)
  61. {
  62. if (array_search($type, self::$types) > $threshold)
  63. continue;
  64. foreach($data as $date => $text)
  65. {
  66. list($date, $message) = $text;
  67. $messages .= $date.' -- '.$type.': '.$message."\r\n";
  68. }
  69. }
  70. // No point in logging nothing
  71. if ($messages == '')
  72. return;
  73. // Create the log file if it doesn't exist yet
  74. if ( ! file_exists($filename))
  75. {
  76. touch($filename);
  77. chmod($filename, 0644);
  78. // Add our PHP header to the log file to prevent URL access
  79. $messages = "<?php defined('SYSPATH') or die('No direct script access.'); ?>\r\n\r\n".$messages;
  80. }
  81. // Append the messages to the log
  82. file_put_contents($filename, $messages, FILE_APPEND) or trigger_error
  83. (
  84. 'The log file could not be written to. Please correct the permissions and refresh the page.',
  85. E_USER_ERROR
  86. );
  87. }
  88. } // End Log