sfFileLogger.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfFileLogger logs messages in a file.
  11. *
  12. * @package symfony
  13. * @subpackage log
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfFileLogger.class.php 10964 2008-08-19 18:33:50Z fabien $
  16. */
  17. class sfFileLogger extends sfLogger
  18. {
  19. protected
  20. $type = 'symfony',
  21. $format = '%time% %type% [%priority%] %message%%EOL%',
  22. $timeFormat = '%b %d %H:%M:%S',
  23. $fp = null;
  24. /**
  25. * Initializes this logger.
  26. *
  27. * Available options:
  28. *
  29. * - file: The file path or a php wrapper to log messages
  30. * You can use any support php wrapper. To write logs to the Apache error log, use php://stderr
  31. * - format: The log line format (default to %time% %type% [%priority%] %message%%EOL%)
  32. * - time_format: The log time strftime format (default to %b %d %H:%M:%S)
  33. * - dir_mode: The mode to use when creating a directory (default to 0777)
  34. * - file_mode: The mode to use when creating a file (default to 0666)
  35. *
  36. * @param sfEventDispatcher $dispatcher A sfEventDispatcher instance
  37. * @param array $options An array of options.
  38. *
  39. * @return Boolean true, if initialization completes successfully, otherwise false.
  40. */
  41. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  42. {
  43. if (!isset($options['file']))
  44. {
  45. throw new sfConfigurationException('You must provide a "file" parameter for this logger.');
  46. }
  47. if (isset($options['format']))
  48. {
  49. $this->format = $options['format'];
  50. }
  51. if (isset($options['time_format']))
  52. {
  53. $this->timeFormat = $options['time_format'];
  54. }
  55. if (isset($options['type']))
  56. {
  57. $this->type = $options['type'];
  58. }
  59. $dir = dirname($options['file']);
  60. if (!is_dir($dir))
  61. {
  62. mkdir($dir, isset($options['dir_mode']) ? $options['dir_mode'] : 0777, true);
  63. }
  64. $fileExists = file_exists($options['file']);
  65. if (!is_writable($dir) || ($fileExists && !is_writable($options['file'])))
  66. {
  67. throw new sfFileException(sprintf('Unable to open the log file "%s" for writing.', $options['file']));
  68. }
  69. $this->fp = fopen($options['file'], 'a');
  70. if (!$fileExists)
  71. {
  72. chmod($options['file'], isset($options['file_mode']) ? $options['file_mode'] : 0666);
  73. }
  74. return parent::initialize($dispatcher, $options);
  75. }
  76. /**
  77. * Logs a message.
  78. *
  79. * @param string $message Message
  80. * @param string $priority Message priority
  81. */
  82. protected function doLog($message, $priority)
  83. {
  84. flock($this->fp, LOCK_EX);
  85. fwrite($this->fp, strtr($this->format, array(
  86. '%type%' => $this->type,
  87. '%message%' => $message,
  88. '%time%' => strftime($this->timeFormat),
  89. '%priority%' => $this->getPriority($priority),
  90. '%EOL%' => PHP_EOL,
  91. )));
  92. flock($this->fp, LOCK_UN);
  93. }
  94. /**
  95. * Returns the priority string to use in log messages.
  96. *
  97. * @param string $priority The priority constant
  98. *
  99. * @return string The priority to use in log messages
  100. */
  101. protected function getPriority($priority)
  102. {
  103. return sfLogger::getPriorityName($priority);
  104. }
  105. /**
  106. * Executes the shutdown method.
  107. */
  108. public function shutdown()
  109. {
  110. if (is_resource($this->fp))
  111. {
  112. fclose($this->fp);
  113. }
  114. }
  115. }