scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Monolog\Formatter;
13  
14 /**
15 * formats the record to be used in the FlowdockHandler
16 *
17 * @author Dominik Liebler <liebler.dominik@gmail.com>
18 */
19 class FlowdockFormatter implements FormatterInterface
20 {
21 /**
22 * @var string
23 */
24 private $source;
25  
26 /**
27 * @var string
28 */
29 private $sourceEmail;
30  
31 /**
32 * @param string $source
33 * @param string $sourceEmail
34 */
35 public function __construct($source, $sourceEmail)
36 {
37 $this->source = $source;
38 $this->sourceEmail = $sourceEmail;
39 }
40  
41 /**
42 * {@inheritdoc}
43 */
44 public function format(array $record)
45 {
46 $tags = array(
47 '#logs',
48 '#' . strtolower($record['level_name']),
49 '#' . $record['channel'],
50 );
51  
52 foreach ($record['extra'] as $value) {
53 $tags[] = '#' . $value;
54 }
55  
56 $subject = sprintf(
57 'in %s: %s - %s',
58 $this->source,
59 $record['level_name'],
60 $this->getShortMessage($record['message'])
61 );
62  
63 $record['flowdock'] = array(
64 'source' => $this->source,
65 'from_address' => $this->sourceEmail,
66 'subject' => $subject,
67 'content' => $record['message'],
68 'tags' => $tags,
69 'project' => $this->source,
70 );
71  
72 return $record;
73 }
74  
75 /**
76 * {@inheritdoc}
77 */
78 public function formatBatch(array $records)
79 {
80 $formatted = array();
81  
82 foreach ($records as $record) {
83 $formatted[] = $this->format($record);
84 }
85  
86 return $formatted;
87 }
88  
89 /**
90 * @param string $message
91 *
92 * @return string
93 */
94 public function getShortMessage($message)
95 {
96 static $hasMbString;
97  
98 if (null === $hasMbString) {
99 $hasMbString = function_exists('mb_strlen');
100 }
101  
102 $maxLength = 45;
103  
104 if ($hasMbString) {
105 if (mb_strlen($message, 'UTF-8') > $maxLength) {
106 $message = mb_substr($message, 0, $maxLength - 4, 'UTF-8') . ' ...';
107 }
108 } else {
109 if (strlen($message) > $maxLength) {
110 $message = substr($message, 0, $maxLength - 4) . ' ...';
111 }
112 }
113  
114 return $message;
115 }
116 }