10

How is logging handled in Craft 3? There's nothing mentioned in the docs at all.

None of the below exist.

  • Craft::log()
  • MyPlugin::log()
  • MyPlugin::$plugin->log()

I'm not seeing any logging in any of the other plugins I've got on this site.

Halp!

3 Answers 3

12

You can use Craft::getLogger()->log($message, $level, $category = 'application')

It uses the Yii2 logger, you can change the $category to your plugin handle

Edit: You can use the "short" syntax as well class reference

Craft::warning($message, $category);

or

Craft::info($message, $category);
3
  • Thanks Robin - I've just given that a go. It works, but a single request to an empty plugin action generates 500 lines of crap in the web.log file (DB connections, queries, php globals like $_SERVER) - with my one entry nestled somewhere in the middle. That makes it pretty unusable. If that's the best it can manage I'm going to have to write a logging system on top of what I'm doing.
    – JamesNZ
    Commented Mar 6, 2018 at 0:09
  • You should be using the Yii2 Debug Toolbar for this... enable it on your Account page in the AdminCP. It lets you search, sort, and filter through the logs very easily. Commented Mar 6, 2018 at 6:25
  • Not in love with the debug toolbar - seems to require a lot of reloading when debugging e.g. ajax requests - would be much better if it did live filtering/updating. And the separate plugin log files were great for longer term monitoring of things... Commented Oct 11, 2018 at 6:05
14

Another perhaps simpler approach is to write to your own log file without using the Craft logger at all.

$file = Craft::getAlias('@storage/logs/pluginhandle.log');
$log = date('Y-m-d H:i:s').' '.$message."\n";

\craft\helpers\FileHelper::writeToFile($file, $log, ['append' => true]);

UPDATE:

I released the Log To File Helper which provides a simple way for logging messages to a specific file and is intended to be used a helper class for modules and plugins.

3
  • 1
    Although Robin's answer was technically the most correct for the question, yours was the one I went with Ben. In my custom plugins I've written a logging service which handles basically doing what you've got above. Thanks for the suggestion.
    – JamesNZ
    Commented Nov 13, 2018 at 3:42
  • Yeah it's sometimes nicer to have a log file dedicated to a plugin. I sometimes put the logic above in the main plugin file as a static method, makes calling it as simple as MyPlugin::$plugin->log(message), but a logging service works too, especially when the logging is a bit more involved.
    – Ben Croker
    Commented Nov 13, 2018 at 7:33
  • I added the Log To File Helper to the answer which I now use for logging in all of my plugins.
    – Ben Croker
    Commented Jul 1, 2019 at 13:10
3

web.log is incredibly verbose, I agree. I think Andrew Welch is right that you should use the Yii2 Debug Toolbar for elaborate stuff, but if you just need some one-line logs to get your bearings in a method, I'd set up the log statement like this:

Craft::info("hello!", __METHOD__);

That second parameter will categorize your call with the method you're calling the log function from. Then, to filter it out from all the rest of web.log, you can run a tail command with a filter on it. Assuming you've got a Unix shell, something like this.

tail -f web.log | grep "YourMethodName"

So now as you execute your commands and trigger the log statements, you'll only see those that are coming from the method you're debugging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.