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!
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityHow is logging handled in Craft 3? There's nothing mentioned in the docs at all.
None of the below exist.
I'm not seeing any logging in any of the other plugins I've got on this site.
Halp!
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);
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.
MyPlugin::$plugin->log(message)
, but a logging service works too, especially when the logging is a bit more involved.
Nov 13, 2018 at 7:33
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.