We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36793
    • 5 Posts
    I have searched the web and the docs but I couldn't find an answer to how to custom write to the Manager Logs.

    I want to log when a user has logged in to the Manager, like it was way back in evo. Is there some easy setting?
    And how to write to the Manager Log when, for example, a user runs my custom Component?

    Thanks!

    This question has been answered by multiple community members. See the first response.

      • 3749
      • 24,544 Posts
      https://forums.modx.com/thread/28478/user-action-reporting

      I don't know of a setting for it, but you could use a plugin tied to OnManagerLogin to record when a user logs in.

      These variables would be available to your plugin:

      $postLoginAttributes = array(
          'user' => $user,
          'attributes' => array(
              'rememberme' => $rememberme,
              'lifetime' => $lifetime,
              'loginContext' => $loginContext,
              'addContexts' => $addContexts,
          )
      );


      Note that $user is the user object, so you'd have to use $user->get('username') if you wanted to log that.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • discuss.answer
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        You should be able to save to to the actual manager log - something like
        <?php
        if ($user instanceof modUser) {
            $userId = $user->get('id');
        }
        $ml = $modx->newObject('modManagerLog');
        $ml->set('user', (integer) $userId);
        $ml->set('occurred', strftime('%Y-%m-%d %H:%M:%S'));
        $ml->set('action', 'login');
        if (!$ml->save()) {
            $modx->log(modX::LOG_LEVEL_ERROR, $this->lexicon('manager_log_err_save'));
            return null;
        }
        [ed. note: sottwell last edited this post 13 years, 3 months ago.]
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
        • discuss.answer
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          This will log login and logout; check OnManagerLogin and OnManagerLogout in the plugin's System Settings.
          <?php
          switch ($modx->event->name) {
              case 'OnManagerLogin':
                  if ($user instanceof modUser) {
                      $userId = $user->get('id');
                  }
                  $ml = $modx->newObject('modManagerLog');
                  $ml->set('user', (integer) $userId);
                  $ml->set('occurred', strftime('%Y-%m-%d %H:%M:%S'));
                  $ml->set('action', 'login');
                  if (!$ml->save()) {
                      $modx->log(modX::LOG_LEVEL_ERROR, $this->lexicon('manager_log_err_save'));
                      return null;
                  }
                  break;
              case 'OnManagerLogout':
                  if ($user instanceof modUser) {
                      $userId = $user->get('id');
                  }
                  $ml = $modx->newObject('modManagerLog');
                  $ml->set('user', (integer) $userId);
                  $ml->set('occurred', strftime('%Y-%m-%d %H:%M:%S'));
                  $ml->set('action', 'logout');
                  if (!$ml->save()) {
                      $modx->log(modX::LOG_LEVEL_ERROR, $this->lexicon('manager_log_err_save'));
                      return null;
                  }
                  break;
          }
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 36793
            • 5 Posts
            You guys are so great!
            I didn't expect such a ready to use code in such a fast response! Thanks!

            One step further in my learning curve of modx! This system is so great! smiley
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Well, this has been bothering me for quite a while now, I just haven't been motivated to dig into how manager events are logged. I'm still not exactly sure how the system does it, but at least I finally found the above code and was able to modify it for our purposes (after several false starts leading to white screens when testing it).
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 3749
                • 24,544 Posts
                Susan, I think this single line will do the same thing:


                $modx->logManagerAction('login','modUser',$user->get('id'));

                [ed. note: BobRay last edited this post 13 years, 3 months ago.]
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  Hm. doesn't seem to work at all. Logout hangs, login causes white page.

                  Oops... that's what I get for copy/paste! Missing ' after 'login there.

                  Hm again. Logs "logout" OK, but no "login".

                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Anybody see anything wrong with this?

                    switch ($modx->event->name) {
                    case 'OnManagerLogin':
                        $modx->logManagerAction('login','modUser',$user->get('id'));
                        break;
                    case 'OnManagerLogout':
                        $modx->logManagerAction('logout','modUser',$user->get('id'));
                        break;
                    }
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 3749
                      • 24,544 Posts
                      Looks good to me, though OnManagerLogout is only called in the logout processor, so if the user is logged out some other way, it won't fire.

                      [ed. note: BobRay last edited this post 13 years, 3 months ago.]
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting