PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Log/Writer/Stream.php

https://bitbucket.org/ksekar/campus
PHP | 138 lines | 61 code | 15 blank | 62 comment | 12 complexity | cfb4fbd319b3d79b2790d7ebea122084 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Log
  17. * @subpackage Writer
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Stream.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. /** Zend_Log_Writer_Abstract */
  23. require_once 'Zend/Log/Writer/Abstract.php';
  24. /** Zend_Log_Formatter_Simple */
  25. require_once 'Zend/Log/Formatter/Simple.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Log
  29. * @subpackage Writer
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @version $Id: Stream.php 24594 2012-01-05 21:27:01Z matthew $
  33. */
  34. class Zend_Log_Writer_Stream extends Zend_Log_Writer_Abstract
  35. {
  36. /**
  37. * Holds the PHP stream to log to.
  38. *
  39. * @var null|stream
  40. */
  41. protected $_stream = null;
  42. /**
  43. * Class Constructor
  44. *
  45. * @param array|string|resource $streamOrUrl Stream or URL to open as a stream
  46. * @param string|null $mode Mode, only applicable if a URL is given
  47. * @return void
  48. * @throws Zend_Log_Exception
  49. */
  50. public function __construct($streamOrUrl, $mode = null)
  51. {
  52. // Setting the default
  53. if (null === $mode) {
  54. $mode = 'a';
  55. }
  56. if (is_resource($streamOrUrl)) {
  57. if (get_resource_type($streamOrUrl) != 'stream') {
  58. require_once 'Zend/Log/Exception.php';
  59. throw new Zend_Log_Exception('Resource is not a stream');
  60. }
  61. if ($mode != 'a') {
  62. require_once 'Zend/Log/Exception.php';
  63. throw new Zend_Log_Exception('Mode cannot be changed on existing streams');
  64. }
  65. $this->_stream = $streamOrUrl;
  66. } else {
  67. if (is_array($streamOrUrl) && isset($streamOrUrl['stream'])) {
  68. $streamOrUrl = $streamOrUrl['stream'];
  69. }
  70. if (! $this->_stream = @fopen($streamOrUrl, $mode, false)) {
  71. require_once 'Zend/Log/Exception.php';
  72. $msg = "\"$streamOrUrl\" cannot be opened with mode \"$mode\"";
  73. throw new Zend_Log_Exception($msg);
  74. }
  75. }
  76. $this->_formatter = new Zend_Log_Formatter_Simple();
  77. }
  78. /**
  79. * Create a new instance of Zend_Log_Writer_Stream
  80. *
  81. * @param array|Zend_Config $config
  82. * @return Zend_Log_Writer_Stream
  83. */
  84. static public function factory($config)
  85. {
  86. $config = self::_parseConfig($config);
  87. $config = array_merge(array(
  88. 'stream' => null,
  89. 'mode' => null,
  90. ), $config);
  91. $streamOrUrl = isset($config['url']) ? $config['url'] : $config['stream'];
  92. return new self(
  93. $streamOrUrl,
  94. $config['mode']
  95. );
  96. }
  97. /**
  98. * Close the stream resource.
  99. *
  100. * @return void
  101. */
  102. public function shutdown()
  103. {
  104. if (is_resource($this->_stream)) {
  105. fclose($this->_stream);
  106. }
  107. }
  108. /**
  109. * Write a message to the log.
  110. *
  111. * @param array $event event data
  112. * @return void
  113. * @throws Zend_Log_Exception
  114. */
  115. protected function _write($event)
  116. {
  117. $line = $this->_formatter->format($event);
  118. if (false === @fwrite($this->_stream, $line)) {
  119. require_once 'Zend/Log/Exception.php';
  120. throw new Zend_Log_Exception("Unable to write to stream");
  121. }
  122. }
  123. }