There was a problem loading the comments.

Activity Logger

Support Portal  »  Knowledgebase  »  Viewing Article

  Print
  • 24/02/2022 1:14 PM

Activity Logger's main purpose is to log/store events from the API activity. It collects all the info it can retrieve - user, user ip, http meta /headers, request_uri, query params/. In order to not slow down the rest of the logic, the current data is saved into NoSQL storage/ right now in Redis, but other storage adapters can be easilly hooked up/. Then, in some defined time interval, a cron will get a chunk from the NoSQL storage data, and will transfer it into the MySQL db, using single transaction.

Database Structure Migrations

Running the needed database migrations can be done in the usual way, using phinx:

$ php vendor/bin/phinx migrate

!!! Important - the MySQL version must be 5.7. or above - we're using json type field in the activity_logger table. Usage + Already Implemented Scenarios

It's currently used in 4 diferent ways/scenarios: directly calling the log method - its purpose is to create custom log records. Anywhere, anytime, putting whatever data we want . Example:

    $someInstance->getServiceLocator()->get('ActivityLogger')->log(
    
        $somePrettyUniqueKey, 
        [
            'entity' => $someEntityNameIfNeeded
            'data' => $someNestedDataArrayHere
            ... 
            // other elements
        ]
    );
  • by triggering an event 'logURLEvents' anywhere in the code - there is attached a listener to that event, so whenever its triggered, the ActivityLogEventListener logURLEvents() method will be invoked. It will collect all request headers, user /by the defined token/ and some more info.

Example:

$this->getEventManager()->trigger('logURLEvents', $this, ['example' => 1]);
  • by defining a list of routes->methods to be monitored - in common/ActivityLoggerV2/config/module.config.php in ActivityLoggerV2->listening->routes we have a collection of routes->methods, which will be logged when called from http.
  • when manipulating a resourse - There is attached a listener, to ViewEvent::EVENTRESPONSE. When a resouse is called, with one of POST, PUT, PATCH and DELETE http methods / which are defined in module.config.php under ActivityLoggerV2->listening->httpmethods, can be adjusted/ , all the request info/http meta, user, token, headers .../ , the content from the request / for instance, whats POST-ed/, pushed and the actual response will be registered.

<h3>Cron Sample Usage</h3>

$ time php public/index.php cronjob dump_activity_from_redis_to_db

Log entity example:

{
  &quot;id&quot;: 1,
  &quot;entity&quot;: &quot;generic&quot;,
  &quot;entity_id&quot;: 0,
  &quot;controller&quot;: &quot;Domain\\V1\\Rest\\Domain\\Controller&quot;,
  &quot;action&quot;: null,
  &quot;message&quot;: null,
  &quot;user_id&quot;: 1,
  &quot;request_uri&quot;: &quot;\&quot;/domain/search/apihawk.com?tlds[0]=net&amp;tlds[1]=info\&quot;&quot;,
  &quot;code&quot;: 0,
  &quot;http_method&quot;: &quot;\&quot;GET\&quot;&quot;,
  &quot;data&quot;: {
    &quot;user&quot;: {
      &quot;id&quot;: 1,
      &quot;role&quot;: &quot;admin&quot;,
      &quot;status&quot;: &quot;active&quot;,
      &quot;username&quot;: &quot;test@gmail.com&quot;,
      &quot;developer&quot;: 1,
      &quot;last_name&quot;: &quot;Test&quot;,
      &quot;first_name&quot;: &quot;Testington&quot;,
      &quot;date_registered&quot;: &quot;2016-01-01 17:00:00&quot;
    },
    &quot;token&quot;: {
      &quot;op_id&quot;: null,
      &quot;scope&quot;: null,
      &quot;expires&quot;: 1479205115,
      &quot;user_id&quot;: &quot;test@gmail.com&quot;,
      &quot;client_id&quot;: &quot;example.test.com&quot;,
      &quot;access_token&quot;: &quot;8ecdc3154d495138f168419fbd965b16e3e2bafb&quot;
    },
    &quot;method&quot;: &quot;GET&quot;,
    &quot;params&quot;: {
      &quot;uri&quot;: &quot;http://apihawk_primeengine.localhost/domain/search/apihawk.com&quot;,
      &quot;post&quot;: [],
      &quot;query&quot;: [],
      &quot;content&quot;: &quot;&quot;
    },
    &quot;headers&quot;: {
      &quot;Host&quot;: &quot;apihawk_primeengine.localhost&quot;,
      &quot;Accept&quot;: &quot;*/*&quot;,
      &quot;Connection&quot;: &quot;keep-alive&quot;,
      &quot;User-Agent&quot;: &quot;Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2783.2 Safari/537.36&quot;,
      &quot;Authorization&quot;: &quot;Bearer 8ecdc3154d495138f168419fbd965b16e3e2bafb&quot;,
      &quot;Cache-Control&quot;: &quot;no-cache&quot;,
      &quot;Postman-Token&quot;: &quot;5170c865-d626-c4b2-f191-f3dd53c68f04&quot;,
      &quot;Accept-Encoding&quot;: &quot;gzip, deflate, sdch&quot;,
      &quot;Accept-Language&quot;: &quot;en-US,en;q=0.8&quot;,
      &quot;X-User-Ip-Address&quot;: &quot;8.8.8.8&quot;
    },
    &quot;version&quot;: &quot;1.1&quot;,
    &quot;request_uri&quot;: &quot;/domain/search/apihawk.com?tlds[0]=net&amp;tlds[1]=info&quot;
  },
  &quot;created_at&quot;: &quot;2016-07-11 11:42:03&quot;,
  &quot;_links&quot;: {
    &quot;self&quot;: {
      &quot;href&quot;: &quot;http://apihawk_primeengine.localhost/activity_logger/events/1&quot;
    }
  }
}

Share via

Related Articles

© ApiHawk