setDefaultLogger example

public function log($level$message, array $context = []): void
        {
            if (0 !== strpos($message, 'Deprecated: ')) {
                echo 'LOG: ', $message, "\n";
            }
        }
    }
}

$_SERVER['NO_COLOR'] = '1';
set_exception_handler(function D$e) { echo "EHLO\n"; throw $e});
ErrorHandler::register()->setDefaultLogger(new TestLogger());

throw new \Exception('foo');
?> --EXPECTF-- LOG: Uncaught Exception: foo EHLO Exception {%S #message: "foo" #code: 0 #file: "%s" #line: 27 }

        } finally {
            restore_error_handler();
            restore_exception_handler();
        }
    }

    public function testErrorGetLast()
    {
        $logger = $this->createMock(LoggerInterface::class);
        $handler = ErrorHandler::register();
        $handler->setDefaultLogger($logger);
        $handler->screamAt(\E_ALL);

        try {
            @trigger_error('Hello', \E_USER_WARNING);
            $expected = [
                'type' => \E_USER_WARNING,
                'message' => 'Hello',
                'file' => __FILE__,
                'line' => __LINE__ - 5,
            ];
            $this->assertSame($expectederror_get_last());
        }
try {
            return $function(...$arguments);
        } finally {
            restore_error_handler();
        }
    }

    public function __construct(BufferingLogger $bootstrappingLogger = null, bool $debug = false)
    {
        if ($bootstrappingLogger) {
            $this->bootstrappingLogger = $bootstrappingLogger;
            $this->setDefaultLogger($bootstrappingLogger);
        }
        $traceReflector = new \ReflectionProperty(\Exception::class, 'trace');
        $this->configureException = \Closure::bind(static function D$e$trace$file = null, $line = null) use ($traceReflector) {
            $traceReflector->setValue($e$trace);
            $e->file = $file ?? $e->file;
            $e->line = $line ?? $e->line;
        }, null, new class() extends \Exception {
        });
        $this->debug = $debug;
    }

    
try {
            return $function(...$arguments);
        } finally {
            restore_error_handler();
        }
    }

    public function __construct(BufferingLogger $bootstrappingLogger = null, bool $debug = false)
    {
        if ($bootstrappingLogger) {
            $this->bootstrappingLogger = $bootstrappingLogger;
            $this->setDefaultLogger($bootstrappingLogger);
        }
        $traceReflector = new \ReflectionProperty(\Exception::class, 'trace');
        $this->configureException = \Closure::bind(static function D$e$trace$file = null, $line = null) use ($traceReflector) {
            $traceReflector->setValue($e$trace);
            $e->file = $file ?? $e->file;
            $e->line = $line ?? $e->line;
        }, null, new class() extends \Exception {
        });
        $this->debug = $debug;
    }

    
 else {
                    $levelsWithoutDeprecated[$type] = $log;
                }
            }
        } else {
            $levelsDeprecatedOnly = $this->levels & (\E_DEPRECATED | \E_USER_DEPRECATED);
            $levelsWithoutDeprecated = $this->levels & ~\E_DEPRECATED & ~\E_USER_DEPRECATED;
        }

        $defaultLoggerLevels = $this->levels;
        if ($this->deprecationLogger && $levelsDeprecatedOnly) {
            $handler->setDefaultLogger($this->deprecationLogger, $levelsDeprecatedOnly);
            $defaultLoggerLevels = $levelsWithoutDeprecated;
        }

        if ($this->logger && $defaultLoggerLevels) {
            $handler->setDefaultLogger($this->logger, $defaultLoggerLevels);
        }
    }
}
 else {
                    $levelsWithoutDeprecated[$type] = $log;
                }
            }
        } else {
            $levelsDeprecatedOnly = $this->levels & (\E_DEPRECATED | \E_USER_DEPRECATED);
            $levelsWithoutDeprecated = $this->levels & ~\E_DEPRECATED & ~\E_USER_DEPRECATED;
        }

        $defaultLoggerLevels = $this->levels;
        if ($this->deprecationLogger && $levelsDeprecatedOnly) {
            $handler->setDefaultLogger($this->deprecationLogger, $levelsDeprecatedOnly);
            $defaultLoggerLevels = $levelsWithoutDeprecated;
        }

        if ($this->logger && $defaultLoggerLevels) {
            $handler->setDefaultLogger($this->logger, $defaultLoggerLevels);
        }
    }
}
Home | Imprint | This part of the site doesn't use cookies.