@@ -37,10 +37,10 @@ class Logger extends AbstractLogger
3737 private $ formatter ;
3838 private $ handle ;
3939
40- public function __construct ($ minLevel = null , $ output = ' php://stderr ' , callable $ formatter = null )
40+ public function __construct ($ minLevel = null , $ output = null , callable $ formatter = null )
4141 {
4242 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 ;
4444
4545 if (isset ($ _ENV ['SHELL_VERBOSITY ' ]) || isset ($ _SERVER ['SHELL_VERBOSITY ' ])) {
4646 switch ((int ) (isset ($ _ENV ['SHELL_VERBOSITY ' ]) ? $ _ENV ['SHELL_VERBOSITY ' ] : $ _SERVER ['SHELL_VERBOSITY ' ])) {
@@ -58,7 +58,7 @@ public function __construct($minLevel = null, $output = 'php://stderr', callable
5858
5959 $ this ->minLevelIndex = self ::$ levels [$ minLevel ];
6060 $ 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 ' )) {
6262 throw new InvalidArgumentException (sprintf ('Unable to open "%s". ' , $ output ));
6363 }
6464 }
@@ -77,7 +77,11 @@ public function log($level, $message, array $context = [])
7777 }
7878
7979 $ formatter = $ this ->formatter ;
80- @fwrite ($ this ->handle , $ formatter ($ level , $ message , $ context ));
80+ if ($ this ->handle ) {
81+ @fwrite ($ this ->handle , $ formatter ($ level , $ message , $ context ));
82+ } else {
83+ error_log ($ formatter ($ level , $ message , $ context , false ));
84+ }
8185 }
8286
8387 /**
@@ -86,7 +90,7 @@ public function log($level, $message, array $context = [])
8690 *
8791 * @return string
8892 */
89- private function format ($ level , $ message , array $ context )
93+ private function format ($ level , $ message , array $ context, $ prefixDate = true )
9094 {
9195 if (false !== strpos ($ message , '{ ' )) {
9296 $ replacements = [];
@@ -105,6 +109,11 @@ private function format($level, $message, array $context)
105109 $ message = strtr ($ message , $ replacements );
106110 }
107111
108- return sprintf ('%s [%s] %s ' , date (\DateTime::RFC3339 ), $ level , $ message ).PHP_EOL ;
112+ $ log = sprintf ('[%s] %s ' , $ level , $ message ).PHP_EOL ;
113+ if ($ prefixDate ) {
114+ $ log = date (\DateTime::RFC3339 ).' ' .$ log ;
115+ }
116+
117+ return $ log ;
109118 }
110119}
0 commit comments