It would be great feature to have the option of automatically prefix the session ID to log messages.
It would make the logger a lot more useful since it makes it lot easier to track down bugs and other unusual behavior in the system.
I did take a look at the CLogger and CLogRoute. I don't think it's necessary to prepend this to all log messages in the CLogger since it doesn't change (well, it's not a feature to track changes in the session id).
It would be possible to extend a some log route and do it there, but I think it's such a fundamental thing that it should live in the framework.
It makes it possible to track down what users are doing and what leads to exceptions/bugs/… Without the session it and many users at the same time it makes log hard to follow. It also requires to constantly log redundant information: "$user did this to $that", "$user doing this" "$user stole your beer".
It seems very redundant to me. The reason is that log messages for the same request are clustered together. So a single message showing session ID (and perhaps other information) is sufficient.
Agree it looks reduntant. But this is the scenario:
Within a day, you get 500 users on your web. Each page view yields 1 'cluster' of log messages. Now one of the users e-mail you and says something is not working on the web. Being a not very computer literate person, he is not very precise on the error message he gets or it's unclear what really happened (we service a lot of people like that)
Now, grepping the logfile by his session it gives you the history of what he's been trying to do. The reason for his problems might have been in previous action (another 'cluster').
I just added CLogRoute::filter and CLogFilter. By setting CLogRoute::filter='CLogFilter' in app config, you may see dump of variables like $_GET, $_POST, etc. You can also configure CLogFilter so that each message is prefixed session ID, user name/ID.
I wasn't expecting you stay up all night to implement this!
This is excellent and have no doubts it will make the logger much more useful.
I found 1 bug
If exception occours in your code and error_handler takes over, neither session or user info is prepended. Reason is that $_SESSION is not set, even if session_id() returns session id.
Btw, Shouldn't there be a space between the prefix and the actual log message?
I think this enhancement should be very useful and practical, so I rush to implement it. BTW, it’s my daytime.
In general, we don't want to start a session in the log filter. I think dropping off the check for $_SESSION should be fine since calling session_id() won't start a session. Thanks.
If you didn't try to access $_SESSION or Yii::app()->user, most likely the session is not started. You may force it to start by listing the 'session' component in 'preload' property in app config.