What is the easiest way to log all the actions to a file or database?
I am planning to create a function, which I will put in all the actions in all the controllers ,which will log what action was tripped,what time,from what IP,what username,etc., into the database (or a log file, whichever I find faster), but just thinking about it makes me tired and bored already.
I have already checked the logging features of Yii here, but I think it just logs everything, plus there is not enough example which just makes me feel less intelligent. What I just want is to log the action, who tripped it, and all the details I could think of.
All my controllers are extended to Controller, so I just added these lines to Controller:
// /protected/components/Controller.php
public $logMessage = NULL;
public $writeLog = false;
protected function afterAction()
{
if($this->writeLog)
{
$sql = 'INSERT INTO tbl_logs VALUES (\''.Yii::app()->user->name.'\',\''.$_SERVER['REMOTE_ADDR'].'\',\''.date("Y-m-d H:i:s").'\',\''.$this->getId().'\',\''.$this->getAction()->getId().'\',\''.$this->logMessage.'\')';
$command = Yii::app()->db->createCommand($sql);
$command->execute();
}
}
$writelog and $logMessage will be passed from the actions: $writeLog will take control whether I want the action to be logged, and $logMesesage will contain the message I would like to include in my log.
Btw, if you’re interested, here’s my tbl_log:
CREATE TABLE `tbl_logs` (
`username` varchar(50) NOT NULL,
`ipaddress` varchar(50) NOT NULL,
`logtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`controller` varchar(255) NOT NULL DEFAULT '',
`action` varchar(255) NOT NULL DEFAULT '',
`details` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Thanks for your input, Niels. I’m satisfied with what I have, since what I just want is to save the username,the IP, and the controller/action that was done. No other setup,just set writlog=true and everything will be logged. Done in 5 seconds (with the power of copy-paste of course )! So I guess I’ll take a look into that in my next projects.
Probably that action is actually called twice? Have you tried debugging it? If not, try to debug it then put a breakpoint to your action or the afterAction. See if it is actually called more than once.
This is what I am looking for. However, I need to add also the model’s attribute values. Hence, if a user/operator change some values or add them, I can see what they are changes. AuditTrail is good, but it generates too many rows for one action, and become difficult to trace.
Any help on getting the model’s attribute on the afterControllerAction method?
I just want to say it is a very good tip I used so far auditTrail but it is not really easy to trace and got very big after few weeks. Your way is easy and works well and that’s why i just love it
Is there something that needs called within the action of my controllers to invoke [font="Courier New"]afterAction()[/font]within Controller.php?
I included the code above within my [font=“Courier New”]protected/components/Controller.php[/font] but nothing is being written to the dB table (It’s obvious that I am missing something)