Remove messages to be logged

I am importing a file, and logging the changes made to the DB in a transaction.

When an error occurs, I rollback all the changes done to the database. However the logmessages are being cached and written to the DB when the application exits. This means logmessages are being written of actions that have been rolled back.

I only want to log the change that has caused the rollback, i.e. the last (few) logmessages.

How can I "unlog" those messages, remove them from the queue? Or make them respect the DB-transaction?

Anyone has a suggestion?

You could flush your log via http://www.yiiframework.com/doc/api/1.1/CLogger#flush-detail. You could use that one in your catch block handling the exception/initiating the rollback




$transaction=Yii::app()->db->beginTransaction();

try {

   //transaction stuff

   if(!$model->save())

      throw new CException("1, Couldn't save model");

   Yii::log("Step 1 finished");

   ....

}

catch(Exception $e)

{

    Yii::getLogger()->flush(true);

    $transaction->rollback();

    Yii::log("Rollback caused by step: ".$e->getMessage(), 'error', 'transactions');

}

And if you want to know which step caused the Exception you could throw individual Exceptions in your try {} block giving you the information that could then be accessed with $e->getMessage() in your catch block. I edited the code above for reference

That might solve it when you only use the CDbLogRoute, but I’d rather fix it in the ‘CLogger’ class. Before it gets distributed to the CLogRoute instances… Then it will also work properly with CFileLogRoute, etc.

But it’s not possible to change the CLogger class, and removing the last # entries isn’t that safe. Perhaps implement some kind of ‘transaction’ logic to CLogger, or something… To rollback logmessages together with the db-changes.

And I’d rather not cache the messages until the transaction has succeeded. And then log the messages from the cache. But maybe that is the best option for nwo.