Simple.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Log_Formatter_Interface */
  23. require_once 'Zend/Log/Formatter/Interface.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Log
  27. * @subpackage Formatter
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @version $Id$
  31. */
  32. class Zend_Log_Formatter_Simple implements Zend_Log_Formatter_Interface
  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. * Formats data into a single line to be written by the writer.
  59. *
  60. * @param array $event event data
  61. * @return string formatted line to write to the log
  62. */
  63. public function format($event)
  64. {
  65. $output = $this->_format;
  66. foreach ($event as $name => $value) {
  67. if ((is_object($value) && !method_exists($value,'__toString'))
  68. || is_array($value)) {
  69. $value = gettype($value);
  70. }
  71. $output = str_replace("%$name%", $value, $output);
  72. }
  73. return $output;
  74. }
  75. }