• Resolved aaron13223

    (@aaron13223)


    Hi There,

    I hope you are doing well! I was trying to log some custom events using SimpleLogger() and had some issues with the _initiator, could you help with that?

    I want to log events from specific users after receiving a webhook. If I log it directly, the initiator is empty/”other” or my admin user.

    SimpleLogger()->info(
    'User ' . $user->user_email . ' purchased ' . $product . ' successfully for $' . $amount_total,
    [
    'username' => $user->user_login,
    'category' => 'payments',
    ]
    );

    I checked the documentation here which pointed me to “_initiator”. I saw that a class is called for this, particular of notice to us: “Simple_History\Log_Initiators::::WP_USER”

    This seems to return the following string:

    $user->user_login ($user->user_email)

    But if add the same to the logger, it silently fails without anything in the debug log either. I also tried adding “_user_id”, “_user_login”, “_user_email” which exhibit the same behaviour.

    SimpleLogger()->info(
    'User ' . $user->user_email . ' purchased ' . $product . ' successfully for $' . $amount_total,
    [
    'username' => $user->user_login,
    'category' => 'payments',
    '_initiator' => $user->user_login . ' (' . $user->user_email . ')'
    ]
    );

    I suppose we can include the plugin file directly and then call the method but I don’t think it should matter much at least for testing.

    I would really appreciate if you could help out with this. Thanks a lot!

    • This topic was modified 11 months ago by aaron13223. Reason: Better title for people searching
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Pär Thernström

    (@eskapism)

    Hi!

    You’re close, but you’re using the initator wrong.

    Here is an example that would work. Now I manually add the user id, login, and email, but if you have their wp_user object you should be able to use $user->id, $user->login, $user->email just like you did in your example. This assumes that it is always logged in users that are performing the actions.

    SimpleLogger()->info(
    'Purchased product "{product_name}" successfully for {amount}',
    [
    'product_name' => 'My test product',
    'amount' => "100 dollars",
    '_initiator' => Simple_History\Log_Initiators::WP_USER,
    '_user_id' => 5,
    '_user_login' => 'par',
    '_user_email' => '[email protected]',
    ]
    );

    This would result in something like this being shown in the log:

    Thread Starter aaron13223

    (@aaron13223)

    Worked like a charm. Thanks so much!

    if( function_exists( 'SimpleLogger' ) ){
    $user = get_user_by( 'id', $user_id );
    $log = SimpleLogger()->info(
    'User purchased {product} successfully for ${amount}.',
    [
    'product' => get_the_title( $data['metadata']['product_id'] ),
    'amount' => $amount_total,
    'category' => 'payments',
    '_initiator' => Simple_History\Log_Initiators::WP_USER,
    '_user_id' => $user_id,
    '_user_login' => $user->user_login,
    '_user_email' => $user->user_email,
    ]
    );
    }
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘SimpleLogger() – _initiator – Not Working’ is closed to new replies.