0

It seems that the issue I am facing is straightforward but somehow the resolutions suggested here, here, and here are not working

I have got the following piece of code:

namespace App\Traits;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

...

try {

  $response = Http::timeout(3)->get('google.cim');

} catch (\Illuminate\Http\Client\ConnectionException $e) {

  Log::info('Connection refused #6 ');

}

I have already tried

  1. Using \Illuminate\Http\Client\ConnectionException $e
  2. Using \Exception $e
  3. Clearing cache php artisan optimize:clear

But I just see the exception in the logs while my custom message never appears in the log.

Thank you.

burf
  • 153
  • 2
  • 8
  • 1
    Have you reviewed the stack trace from the error log to see if the error is being generated/logged somewhere you're not expecting? The above code is working as expected in isolation (i.e. your custom message is logged). – Peppermintology Aug 17 '22 at 11:08
  • @Peppermintology thank you for trying to help me out... if it works for you then my suspicion of `cache` being the culprit becomes stronger but alas even clearing them don't work on my end. To answer your question, I am looking at the same log where all my other custom logging is being done which is `storage/logs/laravel.log` – burf Aug 17 '22 at 11:24
  • Sorry, when I say generated/logged somewhere you're not expecting, I mean in your code. So the or a similar exception is being thrown somewhere other than the above code. – Peppermintology Aug 17 '22 at 11:26
  • Double checked... that's not the case – burf Aug 17 '22 at 11:32
  • Can you share the stack trace, or error message you see in the logs? – Bernard Wiesner Aug 17 '22 at 12:29
  • @BernardWiesner I see the whole Exception (`ocal.ERROR: cURL error 6: Could not resolve host:` and `[previous exception] [object] (GuzzleHttp\\Exception\\ConnectException(code: 0): cURL error 6: Could not resolve host:`) getting printed in the log while I am expecting just the string `Connection refused #6` instead, that I am logging – burf Aug 17 '22 at 12:32
  • The full stack trace should contain your class name, if it does not it might come from elsewhere the error. – Bernard Wiesner Aug 17 '22 at 12:41
  • 1
    You can easily debug it, just add a dd("works") just before your Http::get, and see if it works. – Bernard Wiesner Aug 17 '22 at 12:57
  • Catching \Exception $e should work in your case – Bernard Wiesner Aug 17 '22 at 12:58
  • If you see the exception in your logs, then there is a full stack trace in your logs. As others have requested, share the stack trace from your logs. – miken32 Aug 17 '22 at 18:56
  • 1
    thanks @BernardWiesner ... the solution I found disappointingly can't really be called one as it was a cache issue after all... it was resolved only after I restarted everything (including the machine). Your suggestion of adding dd (although I used a log) helped me though. – burf Aug 18 '22 at 03:49

1 Answers1

0

Rather than catching a specific exception, try catching all exceptions using:

try {
    $response = Http::timeout(3)->get('google.cim');
} catch (\Exception $e) {
    Log::info($e->getMessage()); 
}

Also, check your config/logging.php file to make sure your logging level is set correctly. It should be debug or info:

'daily' => [
    'driver' => 'daily',
    'path' => storage_path('logs/laravel.log'),
    'level' => 'debug',
    'days' => 14,
],

Don't forget to clear the config cache if you need to make change. Run: php artisan config:clear.

Niraj Shah
  • 15,087
  • 3
  • 41
  • 60
  • Hi, thanks for your time... however there was nothing in your reply that I haven't already tried and I mentioned it too in my comment except for explicitly checking the logs settings which was anyway implicitly confirmed. – burf Aug 18 '22 at 03:44