0

I want to print a variable value in Laravel log file. How can I do that ? like I have this -

\Log::info("Group Message Cron");

  • `\Log::info(json_encode($variable, JSON_PRETTY_PRINT));` – Sachin Bahukhandi Aug 09 '22 at 17:52
  • 1
    Thanks for your answer but i solved this with this - ```\Log::info("Message $msg is being sent to $mobile");``` – Jayant Singh Aug 09 '22 at 17:59
  • 1
    It all depends on if it's part of the string, or you're trying to log a more complex variable. Your code works for a simple string, Sachin's works for an array or object. You can also use `Log::info(print_r($variable, true));` for the complex structure. – aynber Aug 09 '22 at 18:03
  • Actually you should be importing Laravel's Log Facade. Using the \ is not the best practice – Rotimi Aug 09 '22 at 18:11
  • @Rotimi But ultimately that part is irrelevant to the question being asked here The question is asking how to log a variable inside of an `::info()` call, not how to use/import the `Log` class. Also, for 1-off testing, it's easier/quicker to just do `\Log::info('This is a test');` instead of also including `use Log;`, then `Log::info()`; that's 2 spots you need to delete when you're done with this testing code instead of 1. – Tim Lewis Aug 09 '22 at 18:22
  • @TimLewis it is not a must, but still a valid point. I've had issues with classes of the same name clashing due to the use of the backslahes. Hence why I said "best practice". – Rotimi Aug 10 '22 at 09:06

0 Answers0