@@ -37,10 +37,10 @@ class Logger extends AbstractLogger
37
37
private $ formatter ;
38
38
private $ handle ;
39
39
40
- public function __construct (string $ minLevel = null , $ output = ' php://stderr ' , callable $ formatter = null )
40
+ public function __construct (string $ minLevel = null , $ output = null , callable $ formatter = null )
41
41
{
42
42
if (null === $ minLevel ) {
43
- $ minLevel = 'php://stdout ' === $ output || 'php://stderr ' === $ output ? LogLevel::CRITICAL : LogLevel::WARNING ;
43
+ $ minLevel = null === $ output || 'php://stdout ' === $ output || 'php://stderr ' === $ output ? LogLevel::ERROR : LogLevel::WARNING ;
44
44
45
45
if (isset ($ _ENV ['SHELL_VERBOSITY ' ]) || isset ($ _SERVER ['SHELL_VERBOSITY ' ])) {
46
46
switch ((int ) (isset ($ _ENV ['SHELL_VERBOSITY ' ]) ? $ _ENV ['SHELL_VERBOSITY ' ] : $ _SERVER ['SHELL_VERBOSITY ' ])) {
@@ -58,7 +58,7 @@ public function __construct(string $minLevel = null, $output = 'php://stderr', c
58
58
59
59
$ this ->minLevelIndex = self ::$ levels [$ minLevel ];
60
60
$ this ->formatter = $ formatter ?: [$ this , 'format ' ];
61
- if (false === $ this ->handle = \is_resource ($ output ) ? $ output : @fopen ($ output , 'a ' )) {
61
+ if ($ output && false === $ this ->handle = \is_resource ($ output ) ? $ output : @fopen ($ output , 'a ' )) {
62
62
throw new InvalidArgumentException (sprintf ('Unable to open "%s". ' , $ output ));
63
63
}
64
64
}
@@ -79,10 +79,14 @@ public function log($level, $message, array $context = [])
79
79
}
80
80
81
81
$ formatter = $ this ->formatter ;
82
- @fwrite ($ this ->handle , $ formatter ($ level , $ message , $ context ));
82
+ if ($ this ->handle ) {
83
+ @fwrite ($ this ->handle , $ formatter ($ level , $ message , $ context ));
84
+ } else {
85
+ error_log ($ formatter ($ level , $ message , $ context , false ));
86
+ }
83
87
}
84
88
85
- private function format (string $ level , string $ message , array $ context ): string
89
+ private function format (string $ level , string $ message , array $ context, bool $ prefixDate = true ): string
86
90
{
87
91
if (false !== strpos ($ message , '{ ' )) {
88
92
$ replacements = [];
@@ -101,6 +105,11 @@ private function format(string $level, string $message, array $context): string
101
105
$ message = strtr ($ message , $ replacements );
102
106
}
103
107
104
- return sprintf ('%s [%s] %s ' , date (\DateTime::RFC3339 ), $ level , $ message ).PHP_EOL ;
108
+ $ log = sprintf ('[%s] %s ' , $ level , $ message ).PHP_EOL ;
109
+ if ($ prefixDate ) {
110
+ $ log = date (\DateTime::RFC3339 ).' ' .$ log ;
111
+ }
112
+
113
+ return $ log ;
105
114
}
106
115
}
0 commit comments