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

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

\craft\helpers\FileHelper::writeToFile($file, $log, ['append' =&gt; true]);
</code></pre>

<p>UPDATE: </p>

<p>I released the <a href="https://github.com/putyourlightson/craft-log-to-file" rel="noreferrer">Log To File Helper</a> 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.</p>

CC BY-SA 4.0
2018-11-13T03:42:59.703Z
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
CC BY-SA 4.0
2018-11-13T07:33:08.473Z
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
CC BY-SA 4.0
2019-07-01T13:10:43.887Z
I added the Log To File Helper to the answer which I now use for logging in all of my plugins.
Ben Croker
CC BY-SA 4.0
2018-10-05T18:51:33.550Z
2019-07-01T13:09:56.050Z
2019-07-01T13:09:56.050Z
Ben Croker
<p><code>web.log</code> 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:</p>

<p><code>Craft::info("hello!", __METHOD__);</code></p>

<p>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 <code>web.log</code>, you can run a tail command with a filter on it. Assuming you've got a Unix shell, something like this.</p>

<p><code>tail -f web.log | grep "YourMethodName"</code></p>

<p>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.</p>

CC BY-SA 4.0
2018-05-07T15:53:50.083Z
2018-05-07T15:53:50.083Z
Erik G
<p>You can use <code>Craft::getLogger()-&gt;log($message, $level, $category = 'application')</code></p>

<p>It uses the <a href="http://www.yiiframework.com/doc-2.0/yii-log-logger.html" rel="noreferrer">Yii2 logger</a>, you can change the <code>$category</code> to your plugin handle</p>

<p>Edit: You can use the "short" syntax as well <a href="https://docs.craftcms.com/api/v3/craft.html" rel="noreferrer">class reference</a></p>

<pre><code>Craft::warning($message, $category);
</code></pre>

<p>or </p>

<pre><code>Craft::info($message, $category);
</code></pre>

CC BY-SA 3.0
2018-03-06T00:09:21.163Z
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
CC BY-SA 3.0
2018-03-06T06:25:10.250Z
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.
andrew.welch
CC BY-SA 4.0
2018-10-11T06:05:45.840Z
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...
Jeremy Daalder
CC BY-SA 3.0
2018-03-05T23:54:28.140Z
2018-03-06T00:00:58.050Z
2018-03-06T00:00:58.050Z
Robin Schambach
<p>How is logging handled in Craft 3? There's nothing mentioned in <a href="https://github.com/craftcms/docs" rel="noreferrer">the docs</a> at all.</p>

<p>None of the below exist.</p>

<ul>
<li>Craft::log() </li>
<li>MyPlugin::log()</li>
<li>MyPlugin::$plugin->log()</li>
</ul>

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

<p>Halp!</p>

<plugin-development><craft3>
Plugin logging in Craft 3
# Plugin logging in Craft 3

(Asked by: username_0 on 2018-03-05)

How is logging handled in Craft 3? There's nothing mentioned in [the docs](https://github.com/craftcms/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!

**Tags:** plugin-development, craft3


---

## 3 answers

### Answer by username_3 on 2018-03-05. Score: 12 | Accepted answer

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);

> **Comments:**
> **username_0**: 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.  
> **username_4**: 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.  
> **username_5**: 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...  

---

### Answer by username_1 on 2018-10-05. Score: 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.

> **Comments:**
> **username_0**: 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.  
> **username_1**: 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.  
> **username_1**: I added the Log To File Helper to the answer which I now use for logging in all of my plugins.  

---

### Answer by username_2 on 2018-05-07. Score: 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.


---

