Expand the event log for module

Good day. I would like to know how to expand the event log for a module? For example, I add fn_log_event to the module functions and collect data in message

        fn_log_event(
            'example_type',
            'example_action',
            array(
                'message' => $message
            )
        );

Initializing the hook (my_changes/init.php)

<?php

if (!defined('BOOTSTRAP')) {
    die('Access denied');
}

fn_register_hooks(
    'save_log'
);

Classified according to my own type

function fn_my_changes_save_log($type, $action, $data, $user_id, &$content)
{
  if ($type == 'example_type') {
    $content['message'] = $data['message'];
  }
}

But the data is not being recorded, what could be the problem? Do type and actions need they own schema?

I noticed that in fn_log_event requires actions from settings. So i installed log settings for my addon

function fn_my_changes_add_logs()
{
  $setting = Settings::instance()->getSettingDataByName('log_type_my_changes');
  $lang_codes = array_keys(Languages::getAll());

  if (!$setting) {
    $setting = array(
      'name' => 'log_type_my_changes',
      'section_id' => 12,
      'section_tab_id' => 0,
      'type' => 'N',
      'position' => 10,
      'is_global' => 'N',
      'edition_type' => 'ROOT,ULT:VENDOR',
      'value' => '#M#my_changes_action_1=Y&my_changes_action_2=Y'
    );
    $descriptions = [];
    foreach ($lang_codes as $lang_code) {
      $descriptions[] = [
        'object_type' => Settings::SETTING_DESCRIPTION,
        'lang_code' => $lang_code,
        'value' => __('my_changes_log', [], $lang_code),
      ];
    }

    $setting_id = Settings::instance()->update($setting, null, $descriptions, true);
    $my_changes_actions = [
      'my_changes_action_1',
      'my_changes_action_2'
    ];
    foreach ($my_changes_actions as $position => $variant) {
      $variant_id = Settings::instance()->updateVariant(
        [
          'object_id' => $setting_id,
          'name' => $variant,
          'position' => $position
        ]
      );

      foreach ($lang_codes as $lang_code) {
        $description = [
          'object_id' => $variant_id,
          'object_type' => Settings::VARIANT_DESCRIPTION,
          'lang_code' => $lang_code,
          'value' => __('log_action_' . $variant, [], $lang_code)
        ];
        Settings::instance()->updateDescription($description);
      }
    }
  }

  return true;
}

but actions in fn_log_event returns empty array

1 Like

@ecomlabs could you please help?

Seems like your function fn_my_changes_add_logs missing some important part. Please check the fn_stripe_connect_add_logging_settings for the full example (app/addons/stripe_connect/func.php).

1 Like

Yeah, i figured out, thanks. Maybe it will be useful to someone

function fn_my_changes_add_logs()
{
  $setting_name = 'log_type_my_changes';
  $setting = Settings::instance()->getSettingDataByName($setting_name);
  $logging_section = Settings::instance()->getSectionByName('Logging');
  $lang_codes = array_keys(Languages::getAll());

  if ($setting) {
    return;
  }

  $setting = array(
    'name' => $setting_name,
    'section_id' => $logging_section['section_id'],
    'section_tab_id' => 0,
    'type' => 'N',
    'position' => 10,
    'is_global' => 'N',
    'edition_type' => 'ROOT,ULT:VENDOR',
  );

  $descriptions = [];
  foreach ($lang_codes as $lang_code) {
    $descriptions[] = [
      'object_type' => Settings::SETTING_DESCRIPTION,
      'lang_code' => $lang_code,
      'value' => __('log_type_my_changes'),
    ];
  }

  $setting_id = Settings::instance()->update($setting, null, $descriptions, true);
  foreach (fn_my_changes_get_actions() as $position => $variant) {
    $variant_id = Settings::instance()->updateVariant(
      [
        'object_id' => $setting_id,
        'name' => $variant,
        'position' => $position
      ]
    );

    foreach ($lang_codes as $lang_code) {
      $description = [
        'object_id' => $variant_id,
        'object_type' => Settings::VARIANT_DESCRIPTION,
        'lang_code' => $lang_code,
        'value' => __('log_action_' . $variant)
      ];
      Settings::instance()->updateDescription($description);
    }
  }

  Settings::instance()->updateValue($setting_name, '#M#my_action_1=Y&my_action_2=Y', 'Logging');
}
2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.