<p>After wasting all of Brad's time (sorry brad!) by sending him an incomplete plugin file with a missing line, I have discovered why I wasn't seeing logs in the console as expected.</p>

<p><code>Craft::log('...')</code> or <code>MyPlugin::log('...')</code> function calls within <code>onSaveEntry</code> or <code>onSaveUser</code> events are in fact properly saved to the log files (<code>craft/app/storage/runtime/logs/</code>). They are just not available in the browser's console, because the event is taking place on the server, while the page is in the process of being redirected when the event fires.</p>

<p>Lesson learned: don't rely on the console for debugging! </p>

<p>The log files can be watched from the command line using <code>tail -F /full/path/to/log/file</code> rather than having to constantly reload the log in a text editor. Although apparently some text editors also support live updates as well.</p>

<p>Feel free to delete this question or leave it here in case others run into the same problem.</p>

CC BY-SA 3.0
2014-10-06T18:10:12.817Z
2014-10-06T18:10:12.817Z
Douglas McDonald
<p>The plugin's full <code>init()</code> code was:</p>

<pre><code>public function init()
{
    parent::init();

    BusinessLogicPlugin::log('init');

    craft()-&gt;on('users.saveUser', function(Event $event) {
        Craft::log('onSaveUSer');
        BusinessLogicPlugin::log('onSaveUSer');
    });

    craft()-&gt;on('entries.saveEntry', function(Event $event) {

        Craft::log('onSaveEntry');
        BusinessLogicPlugin::log('onSaveEntry');

        if ($entry-&gt;section == 'Accounts')
        {   
            if ($event-&gt;params['isNewEntry'])
            {
                ... 
            }
            else
            {   
                ...
            }
        }
    });
}
</code></pre>

<p>Where the <code>saveEntry</code> callback was referencing:</p>

<pre><code>if ($entry-&gt;section == 'Accounts')
</code></pre>

<p>Where <code>$entry</code> hadn't been defined, yet, causing a fatal PHP error not letting Yii/Craft to flush out the log files, which happens in the <code>onEndRequest</code> event.</p>

<p>The entry is defined in the event params, though, so changing it to this is the fix:</p>

<pre><code>craft()-&gt;on('entries.saveEntry', function(Event $event) {

    Craft::log('onSaveEntry');
    BusinessLogicPlugin::log('onSaveEntry');

    $entry = $event-&gt;params['entry'];

    if ($entry-&gt;section == 'Accounts')
    {   
        if ($event-&gt;params['isNewEntry'])
        {
            ... 
        }
        else
        {   
            ...
        }
    }
});
</code></pre>

CC BY-SA 3.0
2014-10-06T17:46:37.763Z
Thanks again Brad. Sorry for the confusion of sending you an incomplete file. I'll try and clean this up by posting the correct answer.
Douglas McDonald
CC BY-SA 3.0
2014-10-06T03:11:05.330Z
2014-10-06T03:11:05.330Z
Brad Bell
<p>Having problems logging to the console using <code>log</code> from within an <code>onSaveEntry</code> or <code>onSaveUser</code> event. Just a simple test below, but will be critical for error logs.</p>

<pre><code>public function init()
{
    parent::init();

    BusinessLogicPlugin::log('init');

    craft()-&gt;on('entries.saveEntry', function(Event $event) 
    {
        Craft::log('onSaveEntry');
        BusinessLogicPlugin::log('onSaveEntry');
    });

}
</code></pre>

<p>'init' gets logged fine, but not 'onSaveEntry'. I seem to be able to log other events fine, like <code>plugins.loadPlugins</code>. I have also confirmed that the event is firing via other tests. Is the <code>log</code> function not available when the <code>onSaveEntry</code> event fires? Scope issue? bug? Or am I missing something else? Thanks.</p>

CC BY-SA 3.0
2014-10-03T21:50:57.157Z
I just think that the saveEntry is not being fired at all. Is saveEntry not used from within the CP?
Douglas McDonald
CC BY-SA 3.0
2014-10-03T21:58:58.737Z
There was a bug a few releases ago where onSaveEntry wouldn't get fired if you were editing an entry from an entry field's relationship modal.  Is that the case here?
Brad Bell
CC BY-SA 3.0
2014-10-03T23:26:22.733Z
No. Just updating and saving the record through the CP directly in the section channel.
Douglas McDonald
CC BY-SA 3.0
2014-10-04T16:33:40.130Z
@Brad `onSaveEntry` is definately firing. I placed an `echo $event->params['entry']->id` which threw an exception but at least output the id. I'm guessing either a scope issue or `log` has not been initialized.
Douglas McDonald
CC BY-SA 3.0
2014-10-06T00:32:00.397Z
Could you zip up your plugin and send it over to support?  I'll update with an answer here with what we find.
Brad Bell
<plugin-development><events>
How to log info from an onSaveEntry event?
# How to log info from an onSaveEntry event?

(Asked by: username_0 on 2014-10-03)

Having problems logging to the console using log from within an onSaveEntry or onSaveUser event. Just a simple test below, but will be critical for error logs.
public function init()
{
    parent::init();

    BusinessLogicPlugin::log('init');

    craft()->on('entries.saveEntry', function(Event $event) 
    {
        Craft::log('onSaveEntry');
        BusinessLogicPlugin::log('onSaveEntry');
    });

}

'init' gets logged fine, but not 'onSaveEntry'. I seem to be able to log other events fine, like plugins.loadPlugins. I have also confirmed that the event is firing via other tests. Is the log function not available when the onSaveEntry event fires? Scope issue? bug? Or am I missing something else? Thanks.

**Tags:** plugin-development, events

> **Comments:**
> **username_0**: I just think that the saveEntry is not being fired at all. Is saveEntry not used from within the CP?  
> **username_1**: There was a bug a few releases ago where onSaveEntry wouldn't get fired if you were editing an entry from an entry field's relationship modal.  Is that the case here?  
> **username_0**: No. Just updating and saving the record through the CP directly in the section channel.  
> **username_0**: @Brad `onSaveEntry` is definately firing. I placed an `echo $event->params['entry']->id` which threw an exception but at least output the id. I'm guessing either a scope issue or `log` has not been initialized.  
> **username_1**: Could you zip up your plugin and send it over to support?  I'll update with an answer here with what we find.  

---

## 2 answers

### Answer by username_0 on 2014-10-06. Score: 3 | Accepted answer

After wasting all of Brad's time (sorry brad!) by sending him an incomplete plugin file with a missing line, I have discovered why I wasn't seeing logs in the console as expected.

`Craft::log('...')` or `MyPlugin::log('...')` function calls within `onSaveEntry` or `onSaveUser` events are in fact properly saved to the log files (`craft/app/storage/runtime/logs/`). They are just not available in the browser's console, because the event is taking place on the server, while the page is in the process of being redirected when the event fires.

Lesson learned: don't rely on the console for debugging!

The log files can be watched from the command line using `tail -F /full/path/to/log/file` rather than having to constantly reload the log in a text editor. Although apparently some text editors also support live updates as well.

Feel free to delete this question or leave it here in case others run into the same problem.


---

### Answer by username_1 on 2014-10-06. Score: 2

The plugin's full init() code was:
public function init()
{
    parent::init();

    BusinessLogicPlugin::log('init');

    craft()->on('users.saveUser', function(Event $event) {
        Craft::log('onSaveUSer');
        BusinessLogicPlugin::log('onSaveUSer');
    });

    craft()->on('entries.saveEntry', function(Event $event) {

        Craft::log('onSaveEntry');
        BusinessLogicPlugin::log('onSaveEntry');

        if ($entry->section == 'Accounts')
        {   
            if ($event->params['isNewEntry'])
            {
                ... 
            }
            else
            {   
                ...
            }
        }
    });
}

Where the saveEntry callback was referencing:
if ($entry->section == 'Accounts')

Where $entry hadn't been defined, yet, causing a fatal PHP error not letting Yii/Craft to flush out the log files, which happens in the onEndRequest event.
The entry is defined in the event params, though, so changing it to this is the fix:
craft()->on('entries.saveEntry', function(Event $event) {

    Craft::log('onSaveEntry');
    BusinessLogicPlugin::log('onSaveEntry');

    $entry = $event->params['entry'];

    if ($entry->section == 'Accounts')
    {   
        if ($event->params['isNewEntry'])
        {
            ... 
        }
        else
        {   
            ...
        }
    }
});

> **Comments:**
> **username_0**: Thanks again Brad. Sorry for the confusion of sending you an incomplete file. I'll try and clean this up by posting the correct answer.  

---

