Comments (19)
Unless the exceptions occurs on a route without the auth middleware, I dont think it's something about middleware. Are you using a custom guard?
I understand what you want, I'll think about something!
I'll come back to you asap!
from laravel-mail-log-channel.
Hi,
you can try to use the shouldReport method of the exceptions Handler and test for the connected user.
from laravel-mail-log-channel.
Looks like shouldReport()
has been renamed to reportable()
and is now placed within the register()
method.
Although I can override my stack channel using config(['logging.channels.stack.channels' => 'daily']);
(to turn off the mail
logger), I am unable to get the current user as the session has not yet started and auth()->user()
just returns null
.
from laravel-mail-log-channel.
Hi,
I did the following in \App\Exceptions\Handler
and it works.
class Handler extends ExceptionHandler
{
//...
public function register(): void
{
$this->reportable(function (\Throwable $e) {
return auth()->id() !== 1;
});
}
//...
}
from laravel-mail-log-channel.
Thanks but unfortunately that doesn't work for me as auth()->id()
is null
. I also want to only omit sending an email if I'm logged in but leave logging the error to file as is. (i.e. config(['logging.channels.stack.channels' => 'daily'])
)
What middleware do you have defined?
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
// \App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\App\Http\Middleware\ForceHttps::class,
\App\Http\Middleware\ForceWww::class,
\App\Http\Middleware\SecurityHeaders::class,
\App\Http\Middleware\SeoRedirects::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
from laravel-mail-log-channel.
Hi @u01jmg3,
You can create a middleware that swaps the logging channel for you user.
Make sure the middleware is after the StartSession
middleware.
The middleware can look like this:
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class ChangeLoggingChannelForRootUserMiddleware
{
public function __invoke(Request $request, $next)
{
if ($request->user()?->getKey() == 1) {
Log::setDefaultDriver('daily');
}
return $next($request);
}
}
from laravel-mail-log-channel.
Thanks for providing this code - the middleware successfully grabs the user from the session. The only problem is that Log::setDefaultDriver('daily');
has no effect as I'm still getting emails (although I've checked that the code does run).
Presumably, I'm too high up in the execution stack but I have to place your middleware after StartSession
. Any ideas?
from laravel-mail-log-channel.
Hi,
obviously the error must be thrown after the middleware is callee. What channel do you want to use for you?
from laravel-mail-log-channel.
What about if it's a compilation error rather than manually throwing one?
from laravel-mail-log-channel.
It should work as well for native errors especially if you still receive the error mail.
from laravel-mail-log-channel.
Yes, email works. But running Log::setDefaultDriver('daily')
in the middleware doesn't turn the emails off as it is supposed to when I'm logged in as the root user (id=1
) and a compilation error is caused.
from laravel-mail-log-channel.
What happens if you switch the default channel without the if statement? Does it still send emails?
from laravel-mail-log-channel.
Yep, emails still come through.
The only way it works is by placing a middleware containing just Log::setDefaultDriver('daily')
in Kernel.php::$middleware
rather than Kernel.php::$middlewareGroups['web']
after StartSession
. But then you have no access to the session. Catch 22
from laravel-mail-log-channel.
Is the exception thrown in a middleware before the controller? Looks like its thrown too soon
from laravel-mail-log-channel.
For testing purposes, I just omitted a semicolon in one of my controllers to generate a Whoops page. I had hoped your new middleware would prevent being emailed.
from laravel-mail-log-channel.
Can you try to throw an exception from a controller?
from laravel-mail-log-channel.
I didn't try it, but you can also add a listener on the Authenticated event to swap the default driver.
from laravel-mail-log-channel.
Calling throw new \ErrorException('Error found');
in my controller (rather than omitting a ;
) and the middleware works as intended. 👍🏻
Will try out your listener idea
from laravel-mail-log-channel.
Great!
I think the listener way should be more bullet proof.
from laravel-mail-log-channel.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-mail-log-channel.