PageRenderTime 63ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/packages/log/src/Monolog/Formatter/LineFormatter.php

https://bitbucket.org/codeyash/bootstrap
PHP | 91 lines | 53 code | 15 blank | 23 comment | 6 complexity | 3b89c3684c0ce02625f24f2320622173 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Formatter;
  11. /**
  12. * Formats incoming records into a one-line string
  13. *
  14. * This is especially useful for logging to files
  15. *
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. * @author Christophe Coevoet <stof@notk.org>
  18. */
  19. class LineFormatter extends NormalizerFormatter
  20. {
  21. const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
  22. protected $format;
  23. /**
  24. * @param string $format The format of the message
  25. * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
  26. */
  27. public function __construct($format = null, $dateFormat = null)
  28. {
  29. $this->format = $format ?: static::SIMPLE_FORMAT;
  30. parent::__construct($dateFormat);
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function format(array $record)
  36. {
  37. $vars = parent::format($record);
  38. $output = $this->format;
  39. foreach ($vars['extra'] as $var => $val) {
  40. if (false !== strpos($output, '%extra.'.$var.'%')) {
  41. $output = str_replace('%extra.'.$var.'%', $this->convertToString($val), $output);
  42. unset($vars['extra'][$var]);
  43. }
  44. }
  45. foreach ($vars as $var => $val) {
  46. $output = str_replace('%'.$var.'%', $this->convertToString($val), $output);
  47. }
  48. return $output;
  49. }
  50. public function formatBatch(array $records)
  51. {
  52. $message = '';
  53. foreach ($records as $record) {
  54. $message .= $this->format($record);
  55. }
  56. return $message;
  57. }
  58. protected function normalize($data)
  59. {
  60. if (is_bool($data) || is_null($data)) {
  61. return var_export($data, true);
  62. }
  63. return parent::normalize($data);
  64. }
  65. protected function convertToString($data)
  66. {
  67. if (null === $data || is_scalar($data)) {
  68. return (string) $data;
  69. }
  70. $data = $this->normalize($data);
  71. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  72. return $this->toJson($data);
  73. }
  74. return str_replace('\\/', '/', json_encode($data));
  75. }
  76. }