Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can SocketHandler be made immune to "Failed connecting to ..." situations? #1822

Closed
LiamFry opened this issue Jul 17, 2023 · 2 comments
Closed
Labels

Comments

@LiamFry
Copy link

LiamFry commented Jul 17, 2023

Monolog version 3

I'm using both a StreamHandler and a SocketHandler.
Is there anyway way for my application to run if the StreamHandler is sucessful yet the SocketHandler fails to connect?
I need my app to run - and log to a file (at a minimum) - even if a connection cannot be made to a remote service via SocketHandler.

Assuming the remote service is unavailable ...
When I instantiate the SocketHandler, I receive no errors/exception. However, the first time a logging method is called, an attempt is made to open the connection to the remote service. The connection fails and UnexpectedValueException is thrown. What if the remote service goes down in the middle of my app running? Sudden my app will throw UnexpectedValueException on every logger call. It's not practical to wrap each and every call to a logging function in a try/catch.

It would be great if SocketHandler had an option to say "ignore failed connection."
Does such a capability exist and I'm overlooking it?

@zerowebcorp
Copy link

zerowebcorp commented Jul 21, 2023

Just encountered same issue. Try...catch isn't working.

I have both GelfHangler sending logs to Graylog and a StreamHandler sending logs to stdout. I took down the graylog server to simulate an outage. When graylog went down, my app also went down throwing RuntimeException error which I tried to catch but for some reason it is not working.

            $this->logger = new \Monolog\Logger('app');

            if ($this->getLogLevel() === "debug") {
                $logLevel = LogLevel::DEBUG;
            } else {
                $logLevel = LogLevel::INFO;
            }
            $this->logger->pushProcessor(new UidProcessor(15));

            if ($this->isDevelopment()) {
                $this->logger->pushHandler(new StreamHandler('php://stdout', $logLevel));
            } else {
                $this->logger->pushProcessor(function ($record) {
                    if (!empty($_SESSION['firstName'])) {
                        $record['extra']['loggedinUser'] = $_SESSION['firstName'];
                    }
                    $date = new \DateTime();
                    $record['extra']['logTime'] = $date->format("Y-m-d H:i:s.u");
                    $record['extra']['token'] = $_SESSION['token'];
                    return $record;
                });
                $this->logger->pushProcessor(new WebProcessor());
            }
            $gelfHandler = new GelfHandler(new Publisher(new UdpTransport($this->graylogDomain, $this->graylogPort)), $logLevel);
            $gelfHandler->setFormatter(new GelfMessageFormatter('app', 'ext_', 'ctx_'));
            $this->logger->pushHandler($gelfHandler);

@Seldaek
Copy link
Owner

Seldaek commented Oct 27, 2023

You should wrap the unreliable handlers into a WhatFailureGroupHandler so that errors are ignored if they fail, and they don't bring the whole app down. This has the downside however that if they are misconfigured you'll never really notice though (except by the fact that nothing gets logged).

@Seldaek Seldaek closed this as completed Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants