PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Log/Formatter/Simple.php

https://bitbucket.org/philkershaw/zend-framework-1.11-acl-implementation
PHP | 108 lines | 44 code | 13 blank | 51 comment | 8 complexity | 4e016dbdccfa0f248867785680857a09 MD5 | raw file
Possible License(s): LGPL-3.0
  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 Formatter
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Simple.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /** Zend_Log_Formatter_Abstract */
  23. require_once 'Zend/Log/Formatter/Abstract.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Log
  27. * @subpackage Formatter
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @version $Id: Simple.php 23775 2011-03-01 17:25:24Z ralph $
  31. */
  32. class Zend_Log_Formatter_Simple extends Zend_Log_Formatter_Abstract
  33. {
  34. /**
  35. * @var string
  36. */
  37. protected $_format;
  38. const DEFAULT_FORMAT = '%timestamp% %priorityName% (%priority%): %message%';
  39. /**
  40. * Class constructor
  41. *
  42. * @param null|string $format Format specifier for log messages
  43. * @return void
  44. * @throws Zend_Log_Exception
  45. */
  46. public function __construct($format = null)
  47. {
  48. if ($format === null) {
  49. $format = self::DEFAULT_FORMAT . PHP_EOL;
  50. }
  51. if (!is_string($format)) {
  52. require_once 'Zend/Log/Exception.php';
  53. throw new Zend_Log_Exception('Format must be a string');
  54. }
  55. $this->_format = $format;
  56. }
  57. /**
  58. * Factory for Zend_Log_Formatter_Simple classe
  59. *
  60. * @param array|Zend_Config $options
  61. * @return Zend_Log_Formatter_Simple
  62. */
  63. public static function factory($options)
  64. {
  65. $format = null;
  66. if (null !== $options) {
  67. if ($options instanceof Zend_Config) {
  68. $options = $options->toArray();
  69. }
  70. if (array_key_exists('format', $options)) {
  71. $format = $options['format'];
  72. }
  73. }
  74. return new self($format);
  75. }
  76. /**
  77. * Formats data into a single line to be written by the writer.
  78. *
  79. * @param array $event event data
  80. * @return string formatted line to write to the log
  81. */
  82. public function format($event)
  83. {
  84. $output = $this->_format;
  85. foreach ($event as $name => $value) {
  86. if ((is_object($value) && !method_exists($value,'__toString'))
  87. || is_array($value)
  88. ) {
  89. $value = gettype($value);
  90. }
  91. $output = str_replace("%$name%", $value, $output);
  92. }
  93. return $output;
  94. }
  95. }