PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Log/Writer/Stream.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 135 lines | 55 code | 15 blank | 65 comment | 12 complexity | b3afe3d49aa7ae4bc4f5497049507d31 MD5 | raw file
  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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
  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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
  33. */
  34. class Zend_Log_Writer_Stream extends Zend_Log_Writer_Abstract
  35. {
  36. /**
  37. * Holds the PHP stream to log to.
  38. * @var null|stream
  39. */
  40. protected $_stream = null;
  41. /**
  42. * Class Constructor
  43. *
  44. * @param streamOrUrl Stream or URL to open as a stream
  45. * @param mode Mode, only applicable if a URL is given
  46. */
  47. public function __construct($streamOrUrl, $mode = NULL)
  48. {
  49. // Setting the default
  50. if ($mode === NULL) {
  51. $mode = 'a';
  52. }
  53. if (is_resource($streamOrUrl)) {
  54. if (get_resource_type($streamOrUrl) != 'stream') {
  55. #require_once 'Zend/Log/Exception.php';
  56. throw new Zend_Log_Exception('Resource is not a stream');
  57. }
  58. if ($mode != 'a') {
  59. #require_once 'Zend/Log/Exception.php';
  60. throw new Zend_Log_Exception('Mode cannot be changed on existing streams');
  61. }
  62. $this->_stream = $streamOrUrl;
  63. } else {
  64. if (is_array($streamOrUrl) && isset($streamOrUrl['stream'])) {
  65. $streamOrUrl = $streamOrUrl['stream'];
  66. }
  67. if (! $this->_stream = @fopen($streamOrUrl, $mode, false)) {
  68. #require_once 'Zend/Log/Exception.php';
  69. $msg = "\"$streamOrUrl\" cannot be opened with mode \"$mode\"";
  70. throw new Zend_Log_Exception($msg);
  71. }
  72. }
  73. $this->_formatter = new Zend_Log_Formatter_Simple();
  74. }
  75. /**
  76. * Create a new instance of Zend_Log_Writer_Mock
  77. *
  78. * @param array|Zend_Config $config
  79. * @return Zend_Log_Writer_Mock
  80. * @throws Zend_Log_Exception
  81. */
  82. static public function factory($config)
  83. {
  84. $config = self::_parseConfig($config);
  85. $config = array_merge(array(
  86. 'stream' => null,
  87. 'mode' => null,
  88. ), $config);
  89. $streamOrUrl = isset($config['url']) ? $config['url'] : $config['stream'];
  90. return new self(
  91. $streamOrUrl,
  92. $config['mode']
  93. );
  94. }
  95. /**
  96. * Close the stream resource.
  97. *
  98. * @return void
  99. */
  100. public function shutdown()
  101. {
  102. if (is_resource($this->_stream)) {
  103. fclose($this->_stream);
  104. }
  105. }
  106. /**
  107. * Write a message to the log.
  108. *
  109. * @param array $event event data
  110. * @return void
  111. */
  112. protected function _write($event)
  113. {
  114. $line = $this->_formatter->format($event);
  115. if (false === @fwrite($this->_stream, $line)) {
  116. #require_once 'Zend/Log/Exception.php';
  117. throw new Zend_Log_Exception("Unable to write to stream");
  118. }
  119. }
  120. }