We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18373 ☆ A M B ☆
    • 3,141 Posts
    So I’ve been struggling with a problem for a couple days now, in which $modx->runProcessor seemed to break my Registry (a custom log so to speak) which works with $modx->log().

    This is what I was doing:
            logConsole('info','before processor');
            $response = $modx->runProcessor('resource/create',$line);
    logConsole('info','after processor');

    This is my logConsole function that uses $modx->log():
        function logConsole($type,$msg) {
            global $modx;
            switch ($type) {
                case 'error':
                    sleep(1);
                    $modx->log(modX::LOG_LEVEL_ERROR,'FATAL ERROR: '.$msg);
                    sleep(2);
                    $modx->log(modX::LOG_LEVEL_INFO,'COMPLETED');
                    sleep(1);
                    return $modx->error->failure($msg); // Die to prevent the process to go on anyway!
                break;
                default:
                case 'info':
                    $modx->log(modX::LOG_LEVEL_INFO,$msg);
                    //return true;
                break;
                case 'warn':
                    $modx->log(modX::LOG_LEVEL_WARN,$msg);
                    return true;
                break;
            }
        }

    I know the function worked, cause everything I logged with it showed up in the registry (and thus in my console), until I ended up with $modx->runProcessor. Not a single log entry would show up after that.

    After digging into the code of the resource/create processor and finding nothing that linked it to the log, I went a step back and checked out the main connector (connectors/index.php) which would be handling the request. Turned out that it is setting some logging variables for the process..
    /* set debugging/logging options */
    $modx->setDebug(E_ALL | E_STRICT);
    $modx->setLogLevel(modX::LOG_LEVEL_ERROR);
    $modx->setLogTarget('FILE');


    While the registry would be set up automatically by the request handler (I guess - it just works), it is probably instantiating these variables differently to point to the right file to return the information to my console. And now the connector is overwriting that. I tested it by setting the log level in the connector to use INFO and up, and those messages were appearing in the error log instead of on my screen.

    After spending some time trying to nullify the connectors behavior in my own script I ended up commenting the above mentioned three lines. And then my script started working as intended.


    Now my question is whether this is a bug or not. From my point of view, trying to get direct feedback in my CMP, this seems like a bug. But what would happen if those three lines are not there for other processors to use? I assume there’s a default set somewhere, so is it needed in the connector? Shouldn’t it check for existing values before overwriting them? Am I just goofing up? My complete code for this is on Github: https://github.com/Mark-H/importX
      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
      • 2611
      • 394 Posts
      So this problem exists in the core connector file?
      You can just create your own connector, right? And not include the main connector...
        Follow me on twitter: @b03tz
        Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
        CodeMaster
        • 18373 ☆ A M B ☆
        • 3,141 Posts
        The $modx->runProcessor uses the main connector file if I understand it correctly.

        Of course I could use $modx->newObject() and perform all validation and stuff myself, but I seem to recall the core devs encouraging to use the existing processors (and if that saves me a lot of hours of replicating its behavior that sounds the best way to go to me).

        I do use my own connector to connect AJAX requests from the CMP to my processor.
          Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

          Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
          • 24865
          • 289 Posts
          I’m gonna run through your script when I have a bit more time, but the first thing that comes to mind is, why don’t you extend importX’s main class in the processor and use logConsole as an internal function ($importx->logConsole();)? It seems a bit more modular when you do. Just a suggestion.

          Aside from that, when I check out the runProcessor function, I only see that it loads it’s native modProcessor and modProcessorResponse class, which are both defined in modprocessor.class.php. Both the execution and reponse are done by the modProcessorResponse function. It doesn’t implement, extend or overwrite connector functionality. The connector only validates (if I am correct) the request your are making towards the processor (restricted by policies and the site_id).

          As I said, I’m gonna run through it when I have a bit more time but perhaps this can help you get some debugging going.
            @MarkGHErnst

            Developer at Adwise Internetmarketing, the Netherlands.
            • 18373 ☆ A M B ☆
            • 3,141 Posts
            Well, yeah. Like I said, I thought it was using the connector (the whole MVC model thing) and I don’t know where it gets the reference from, but commenting out those lines does fix my problem... Put them back and it wont work again.

            I may put the function within the class, but I just quickly whipped it up to streamline how I’m sending responses, and while less.. OOP-correct... it works. tongue
              Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

              Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
              • 18373 ☆ A M B ☆
              • 3,141 Posts
              Fix/workaround presented by Shaun at the bug report: http://bugs.modx.com/issues/4480

              The following code forces the use of the registry, at least in 2.1.
              $modx->request->registerLogging($_POST);

                Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                • 34027
                • 30 Posts
                I'm writing a CSV user importer that writes to a console, and I just encountered this issue as well. Thank you for reporting the fix!