1

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.

  • I just think that the saveEntry is not being fired at all. Is saveEntry not used from within the CP? – Douglas McDonald Oct 3 '14 at 21:50
  • 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 Oct 3 '14 at 21:58
  • No. Just updating and saving the record through the CP directly in the section channel. – Douglas McDonald Oct 3 '14 at 23:26
  • @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 Oct 4 '14 at 16:33
  • 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 Oct 6 '14 at 0:32
3

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.

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
        {   
            ...
        }
    }
});
  • 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 Oct 6 '14 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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