I want to log everything that the moderators do on the site, for example, when they have edited the news, or deleted a comment, created a forum topic, etc. Tell me how to do it, I want to write down who did what he did, when, where, what time. Do this through standard events? afterSave afterDelete? Tell me more in detail please. As I see it, I just insert the code into the necessary action, such as Log ($ model); and somewhere it's all written to the database
1 answer
Run the migration to create a table for the logs
yii migrate --migrationPath=@yii/log/migrations/
Add to config
'log' => [ 'targets' => [ 'access' => [ 'except' => ['application'], 'prefix' => function ($message) { $user = Yii::$app->has('user', true) ? Yii::$app->get('user') : null; $userID = $user ? $user->getId(false) : '-'; return $user; }, 'class' => 'yii\log\DbTarget', 'levels' => ['info'], 'categories' => ['access'], ], ] ]
Well, the use is where you need to write to the database
Yii::info(__METHOD__, 'access');
- And what is the profit of this option? Easier through afterSave, isn't it? - Kostya
- Use ready-made or write yourself. Anyway, in aftersafe you will have to add it too - Ninazu
- Is 'log' in the config located inside the components? Is the component class inherited from DbTarget? Should work. - Ninazu
- Thanks, installed, it is written to the database, now I understand it is necessary to make some kind of message parser in the admin panel? Maybe there are ready-made options. And then there is so much information that kapets, get, post, session, etc. I need someone who did, and what he did. Or is it done in app \ components \ DBLog? it is possible then not big primerchik as it becomes - Kostya
- Well, if you use it like in my example, then in app \ components \ DBLog you will receive messages in json format. (Class name, method name, UserID) If you need to process, do what you need with this data, if you simply add it to the database, then it will add it as a string. - Ninazu
|