Log table for errors

Log table for errors

Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

I currently use the server side events to log changes, as described here:

function logChange ( $db, $action, $id, $values ) {
    $db->insert( 'staff-log', array(
        'user'   => $_SESSION['username'],
        'action' => $action,
        'values' => json_encode( $values ),
        'row'    => $id,
        'when'   => date('c')
    ) );
}
 
Editor::inst( $db, 'staff' )
    ->fields(
        Field::inst( 'first_name' ),
        Field::inst( 'last_name' ),
        Field::inst( 'position' ),
        Field::inst( 'email' ),
        Field::inst( 'office' )
    )
    ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
        logChange( $editor->db(), 'create', $id, $values );
    } )
    ->on( 'postEdit', function ( $editor, $id, $values, $row ) {
        logChange( $editor->db(), 'edit', $id, $values );
    } )
    ->on( 'postRemove', function ( $editor, $id, $values ) {
        logChange( $editor->db(), 'delete', $id, $values );
    } )
    ->process( $_POST )
    ->json();

However, it would be really useful to also log errors associated with datatables events. For example, the information which appears if you have ->debug( true ) enabled or JSON response errors.

Has anyone else tried/achieved this? I am just at the thinking it through stage so was wondering if anyone had any experience?

Thanks

Answers

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Rather than:

    $editor = Editor::inst( $db, 'staff' )
      ...
        ->process( $_POST )
        ->json();
    

    Use:

    $data = Editor::inst( $db, 'staff' )
      ...
      ->process( $_POST )
      ->data();
    
    // Write to log of
    $data['debug'];
    
    echo json_encode( $data );
    

    Allan

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2
    edited November 2018

    Hi Allan,

    Thanks for your prompt reply. Would this work or am I missing $editor->db() and $id if I try and log the change after ->data(); like this:

    $data = Editor::inst( $db, 'staff' )
      ...
      ->process( $_POST )
      ->data();
    
    $values = $data['debug'];
    
    logChange( $editor->db(), 'debug', $id, $values );
    
    echo json_encode( $data );
    

    Thanks

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2
    edited November 2018

    This does work,

    $id = '0';
    $values = $editor['debug'];   
    insert_error_log ( $db, 'error', 't_company', $id, $values );
    

    But I currently don't have the $id of the item being edited or created in my scope and it also saves the SELECT queries as oppose to just errors.

    Should the $id var be available somewhere in the scope and can I access just errors, will they be in a nested element in the debug array. It's a little tricky to test as I haven't figured out how to force an error!

    Thanks

    Chris

This discussion has been closed.