Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4487f99

Browse files
committed
[Bridge/Monolog] Enhanced the Console Handler
Basically, the formatter now uses the VarDumper & uses more significantcolors and has a more compact / readable format (IMHO).
1 parente7c12d3 commit4487f99

File tree

4 files changed

+184
-21
lines changed

4 files changed

+184
-21
lines changed

‎src/Symfony/Bridge/Monolog/Formatter/ConsoleFormatter.php‎

Lines changed: 166 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,194 @@
1111

1212
namespaceSymfony\Bridge\Monolog\Formatter;
1313

14-
useMonolog\Formatter\LineFormatter;
14+
useMonolog\Formatter\FormatterInterface;
1515
useMonolog\Logger;
16+
useSymfony\Component\VarDumper\Cloner\Data;
17+
useSymfony\Component\VarDumper\Cloner\Stub;
18+
useSymfony\Component\VarDumper\Cloner\VarCloner;
19+
useSymfony\Component\VarDumper\Dumper\CliDumper;
1620

1721
/**
1822
* Formats incoming records for console output by coloring them depending on log level.
1923
*
2024
* @author Tobias Schultze <http://tobion.de>
25+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
2126
*/
22-
class ConsoleFormatterextends LineFormatter
27+
class ConsoleFormatterimplements FormatterInterface
2328
{
24-
constSIMPLE_FORMAT ="%start_tag%[%datetime%] %channel%.%level_name%:%end_tag% %message% %context% %extra%\n";
29+
constSIMPLE_FORMAT ="%datetime% %start_tag%%level_name%%end_tag% <comment>[%channel%]</> %message%%context%%extra%\n";
30+
constSIMPLE_DATE ='H:i:s';
31+
32+
privatestatic$levelColorMap =array(
33+
Logger::DEBUG =>'fg=white',
34+
Logger::INFO =>'fg=green',
35+
Logger::NOTICE =>'fg=blue',
36+
Logger::WARNING =>'fg=cyan',
37+
Logger::ERROR =>'fg=yellow',
38+
Logger::CRITICAL =>'fg=red',
39+
Logger::ALERT =>'fg=red',
40+
Logger::EMERGENCY =>'fg=white;bg=red',
41+
);
42+
43+
private$options;
44+
private$cloner;
45+
private$outputBuffer;
46+
private$dumper;
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
publicfunction__construct($options =array())
52+
{
53+
// BC Layer
54+
if (!is_array($options)) {
55+
@trigger_error(sprintf('The constructor arguments $format, $dateFormat, $allowInlineLineBreaks, $ignoreEmptyContextAndExtra of "%s" are deprecated since 3.3 and will be removed in 4.0. Use $options instead.',self::class),E_USER_DEPRECATED);
56+
$args =func_get_args();
57+
$options =array();
58+
if (isset($args[0])) {
59+
$options['format'] =$args[0];
60+
}
61+
if (isset($args[1])) {
62+
$options['date_format'] =$args[1];
63+
}
64+
}
65+
66+
$this->options =array_replace(array(
67+
'format' =>self::SIMPLE_FORMAT,
68+
'date_format' =>self::SIMPLE_DATE,
69+
'colors' =>true,
70+
'multiline' =>false,
71+
),$options);
72+
73+
if (class_exists(VarCloner::class)) {
74+
$this->cloner =newVarCloner();
75+
$this->cloner->addCasters(array(
76+
'*' =>array($this,'castObject'),
77+
));
78+
79+
$this->outputBuffer =fopen('php://memory','r+b');
80+
if ($this->options['multiline']) {
81+
$output =$this->outputBuffer;
82+
}else {
83+
$output =array($this,'echoLine');
84+
}
85+
86+
$this->dumper =newCliDumper($output,null, CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR);
87+
}
88+
}
2589

2690
/**
2791
* {@inheritdoc}
2892
*/
29-
publicfunction__construct($format =null,$dateFormat =null,$allowInlineLineBreaks =false,$ignoreEmptyContextAndExtra =true)
93+
publicfunctionformatBatch(array$records)
3094
{
31-
parent::__construct($format,$dateFormat,$allowInlineLineBreaks,$ignoreEmptyContextAndExtra);
95+
foreach ($recordsas$key =>$record) {
96+
$records[$key] =$this->format($record);
97+
}
98+
99+
return$records;
32100
}
33101

34102
/**
35103
* {@inheritdoc}
36104
*/
37105
publicfunctionformat(array$record)
38106
{
39-
if ($record['level'] >= Logger::ERROR) {
40-
$record['start_tag'] ='<error>';
41-
$record['end_tag'] ='</error>';
42-
}elseif ($record['level'] >= Logger::NOTICE) {
43-
$record['start_tag'] ='<comment>';
44-
$record['end_tag'] ='</comment>';
45-
}elseif ($record['level'] >= Logger::INFO) {
46-
$record['start_tag'] ='<info>';
47-
$record['end_tag'] ='</info>';
107+
$record =$this->replacePlaceHolder($record);
108+
109+
$levelColor =self::$levelColorMap[$record['level']];
110+
111+
if ($this->options['multiline']) {
112+
$context =$extra ="\n";
48113
}else {
49-
$record['start_tag'] ='';
50-
$record['end_tag'] ='';
114+
$context =$extra ='';
115+
}
116+
$context .=$this->dumpData($record['context']);
117+
$extra .=$this->dumpData($record['extra']);
118+
119+
$formatted =strtr($this->options['format'],array(
120+
'%datetime%' =>$record['datetime']->format($this->options['date_format']),
121+
'%start_tag%' =>sprintf('<%s>',$levelColor),
122+
'%level_name%' =>sprintf('%-9s',$record['level_name']),
123+
'%end_tag%' =>'</>',
124+
'%channel%' =>$record['channel'],
125+
'%message%' =>$this->replacePlaceHolder($record)['message'],
126+
'%context%' =>$context,
127+
'%extra%' =>$extra,
128+
));
129+
130+
return$formatted;
131+
}
132+
133+
/**
134+
* @internal
135+
*/
136+
publicfunctionechoLine($line,$depth,$indentPad)
137+
{
138+
if (-1 !==$depth) {
139+
fwrite($this->outputBuffer,$line);
51140
}
141+
}
142+
143+
/**
144+
* @internal
145+
*/
146+
publicfunctioncastObject($v,array$a,Stub$s,$isNested)
147+
{
148+
if ($this->options['multiline']) {
149+
return$a;
150+
}
151+
152+
if ($isNested && !$vinstanceof \DateTimeInterface) {
153+
$s->cut = -1;
154+
$a =array();
155+
}
156+
157+
return$a;
158+
}
159+
160+
privatefunctionreplacePlaceHolder(array$record)
161+
{
162+
$message =$record['message'];
163+
164+
if (false ===strpos($message,'{')) {
165+
return$record;
166+
}
167+
168+
$context =$record['context'];
169+
170+
$replacements =array();
171+
foreach ($contextas$k =>$v) {
172+
$replacements['{'.$k.'}'] =sprintf('<comment>%s</>',$this->dumpData($v,false));
173+
}
174+
175+
$record['message'] =strtr($message,$replacements);
176+
177+
return$record;
178+
}
179+
180+
privatefunctiondumpData($data,$colors =null)
181+
{
182+
if (null ===$this->dumper) {
183+
return'';
184+
}
185+
186+
if (null ===$colors) {
187+
$this->dumper->setColors($this->options['colors']);
188+
}else {
189+
$this->dumper->setColors($colors);
190+
}
191+
192+
if (!$datainstanceof Data) {
193+
$data =$this->cloner->cloneVar($data);
194+
}
195+
$data =$data->withRefHandles(false);
196+
$this->dumper->dump($data);
197+
198+
$dump =stream_get_contents($this->outputBuffer, -1,0);
199+
rewind($this->outputBuffer);
200+
ftruncate($this->outputBuffer,0);
52201

53-
returnparent::format($record);
202+
returnrtrim($dump);
54203
}
55204
}

‎src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,14 @@ protected function write(array $record)
164164
*/
165165
protectedfunctiongetDefaultFormatter()
166166
{
167-
returnnewConsoleFormatter();
167+
if (!$this->output) {
168+
returnnewConsoleFormatter();
169+
}
170+
171+
returnnewConsoleFormatter(array(
172+
'colors' =>$this->output->isDecorated(),
173+
'multiline' => OutputInterface::VERBOSITY_DEBUG <=$this->output->getVerbosity(),
174+
));
168175
}
169176

170177
/**

‎src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,19 @@ public function testVerbosityMapping($verbosity, $level, $isHandling, array $map
5959

6060
// check that the handler actually outputs the record if it handles it
6161
$levelName = Logger::getLevelName($level);
62+
$levelName =sprintf('%-9s',$levelName);
6263

6364
$realOutput =$this->getMockBuilder('Symfony\Component\Console\Output\Output')->setMethods(array('doWrite'))->getMock();
6465
$realOutput->setVerbosity($verbosity);
66+
if ($realOutput->isDebug()) {
67+
$log ="16:21:54$levelName [app] My info message\n[]\n[]\n";
68+
}else {
69+
$log ="16:21:54$levelName [app] My info message [] []\n";
70+
}
6571
$realOutput
6672
->expects($isHandling ?$this->once() :$this->never())
6773
->method('doWrite')
68-
->with("[2013-05-29 16:21:54] app.$levelName: My info message\n",false);
74+
->with($log,false);
6975
$handler =newConsoleHandler($realOutput,true,$map);
7076

7177
$infoRecord =array(
@@ -143,7 +149,7 @@ public function testWritingAndFormatting()
143149
$output
144150
->expects($this->once())
145151
->method('write')
146-
->with('<info>[2013-05-2916:21:54] app.INFO:</info>My info message'."\n")
152+
->with("16:21:54 <fg=green>INFO </> <comment>[app]</>My info message\n[]\n[]\n")
147153
;
148154

149155
$handler =newConsoleHandler(null,false);

‎src/Symfony/Bridge/Monolog/composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
},
2323
"require-dev": {
2424
"symfony/console":"~2.8|~3.0",
25-
"symfony/event-dispatcher":"~2.8|~3.0"
25+
"symfony/event-dispatcher":"~2.8|~3.0",
26+
"symfony/var-dumper":"~3.3"
2627
},
2728
"suggest": {
2829
"symfony/http-kernel":"For using the debugging handlers together with the response life cycle of the HTTP kernel.",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp