log all actions

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.

Nevermind. I solved the problem myself.

hi!

How did yo solve that?

thanks for sharing…

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 (+1)

I’ll try that

Thanks also. I’m glad I was able to help. :)

Btw, just so you know, macinville helped me here. It was because of his patience that I was able to learn what I just wrote. :)

Your gratefulness is infectious. Keep it up :)

For future references. You could also use the AuditTrail extension. Another option is to use Stored routines to log DB activity.

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.

@mjkulet:

Thanks. :)

Just to prevent any errors when E_STRICT is on, use




protected function afterAction($action)

...



And you can make use of the values inside that $action too! :)

Hi,

thanks for the great tutorial.

I have apply the same code instead only I changed user_id instead of username.

But for single action its logging twice in DB. Is there any changes require.

I have extended handleBeginRequest($event) of CBehavior.

Thanks

M Hussain

Hi M Hussain,

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.

if you are log all actions you also need to log $_SERVER, $_COOKIE, $_SESSION, $_POST, $_GET variables to make more clear.

A great job! It’s helpful!

Thanks. Your solution is Good. But in my case, not all controllers extend common "Controller". What should be done in this case?

I want to create similar functionality for my site, where logging function should be globally accessible through out the system.

Thanks in advance.

Overriding CWebApplication::afterControllerAction() might be an option in that case.

Hi All,

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?

Thanks in advance.

Kind regards,

Daniel

Hi,

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 :)

Thanks

lukBB

Thanks a lot, this was what I was looking for !

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)