yii 日志和事件
日志
配置
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
], //以文件的方式记录
[
'class' => 'yii\log\FileTarget',
'levels' => ['info'],
'categories' => ['shopify'],
'logVars' => ['*'],//不记录PHP全局变量$_POST等
'logFile' => '@runtime/logs/shopify.log',
// 'exportInterval' => 1,//在导出消息之前应该累积多少条消息
'maxFileSize' => 1024 * 2,//文件大小
'maxLogFiles' => 20,
], //以邮件的方式记录,这种方式后面还需设置邮箱的账号密码和host
[
'class' => 'yii\log\EmailTarget',
'levels' => ['info'],
'categories' => ['shopify'],
'logVars' => ['*'],//不记录PHP全局变量$_POST等
'message' => [
'from' => ['service@hjk.top'],
'to' => ['yowwoy@hjk.com'],
'subject' => '商城预警',
],
],
],
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.exmail.qq.com',//邮箱服务器
'username' => 'service@hjk.top',//用户名
'password' => 'hujikhllkj',//密码
'port' => '465',//端口
'encryption' => 'ssl',//加密
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['hguhjl@163.com'=>'admin']
],
],
测试调用
public function actionLog1()
{
\Yii::info("出错啦,出错啦", 'shopify'); Yii::getLogger()->log("自定义日志",Logger::LEVEL_ERROR); Yii::trace("trace,开发调试时候记录"); Yii::error("error,错误日志"); Yii::warning("warning,警告信息"); Yii::info("info,记录操作提示");
}
事件
配置
//添加事件
'on beforeRequest' => function($event) {
\yii\base\Event::on(\yii\db\BaseActiveRecord::className(), \yii\db\BaseActiveRecord::EVENT_AFTER_INSERT, ['backend\components\AdminLog', 'write']);
\yii\base\Event::on(\yii\db\BaseActiveRecord::className(), \yii\db\BaseActiveRecord::EVENT_AFTER_UPDATE, ['backend\components\AdminLog', 'write']);
\yii\base\Event::on(\yii\db\BaseActiveRecord::className(), \yii\db\BaseActiveRecord::EVENT_AFTER_DELETE, ['backend\components\AdminLog', 'write']);
},
记录函数
<?php namespace backend\components; use Yii;
use yii\helpers\StringHelper;
use yii\helpers\Url;
use yii\db\ActiveRecord; class AdminLog
{
public static function write($event)
{
// 排除日志表自身,没有主键的表不记录(没想到怎么记录。。每个表尽量都有主键吧,不一定非是自增id)
if($event->sender instanceof \backend\models\AdminLog || !$event->sender->primaryKey()) {
return;
}
// 显示详情有待优化,不过基本功能完整齐全
if ($event->name == ActiveRecord::EVENT_AFTER_INSERT) {
$description = "%s新增了表%s %s:%s的%s";
} elseif($event->name == ActiveRecord::EVENT_AFTER_UPDATE) {
$description = "%s修改了表%s %s:%s的%s";
} else {
$description = "%s删除了表%s %s:%s%s";
}
if (!empty($event->changedAttributes)) {
$desc = '';
foreach($event->changedAttributes as $name => $value) {
if (!is_string($value) and !empty($value)){
$value=var_export($value,true);//解决当为数组时的异常问题
}
$info=$event->sender->getAttribute($name);
if (!is_string($info) and !empty($info)){
$info=var_export($info,true);
} $desc .= $name . ' : ' . $value . '=>' . StringHelper::truncate($info,256) . ',';
}
$desc = substr($desc, 0, -1);
} else {
$desc = '';
}
$userName = Yii::$app->user->identity->username;
$tableName = $event->sender->tableSchema->name;
$description = sprintf($description, $userName, $tableName, $event->sender->primaryKey()[0], $event->sender->getPrimaryKey(), $desc); $route = Url::to();
$userId = Yii::$app->user->id;
$ip = sprintf('%u',ip2long(Yii::$app->request->userIP));
$data = [
'route' => $route,
'description' => $description,
'user_id' => $userId,
'ip' => $ip,
'created_at' => time(),
];
$model = new \backend\models\AdminLog();
$model->setAttributes($data);
$model->save(false);
}
}
yii 日志和事件的更多相关文章
- YII框架的事件机制
一.什么是事件机制 解释:发生了一件事情,然后某些东西对这件事作出反应. 例子:假设发生了A同学结婚事件,然后B同学给份子钱反应,那么,B是怎么知道(监听)A事件的发生了呢,有两种办法. 扫描式:B不 ...
- yii日志保存机制
一.修改yii框架的配置文件(main.php) 'log' => array( 'class' => 'CLogRouter', 'routes' => array( array( ...
- Yii日志记录Logging
.Yii::getLogger()->log($message, $level, $category = 'application') .Yii::trace($message, $catego ...
- 干货:yii日志功能详解
转载请注明来自souldak,微博:@evagle 一.基本日志功能 详细的介绍查看官网的document:http://www.yiiframework.com/doc/guide/1.1/en/t ...
- Yii日志使用
Yii 提供了一个灵活可扩展的日志功能.记录的日志 可以通过日志级别和信息分类进行归类.通过使用 级别和分类过滤器,所选的信息还可以进一步路由到 不同的目的地,例如一个文件,Email,浏览器窗口等. ...
- Yii 日志组件
详细的介绍查看官网的document:http://www.yiiframework.com/doc/guide/1.1/en/topics.logging 也可以看 Yii 1.1 Applicat ...
- 使用EventLog类写Windows事件日志
在程序中经常需要将指定的信息(包括异常信息和正常处理信息)写到日志中.在C#3.0中可以使用EventLog类将各种信息直接写入Windows日志.EventLog类在System.Diagnosti ...
- EventLog组件读写事件日志
使用.Net中的EventLog控件使您可以访问或自定义Windows 事件日志,事件日志记录关于重要的软件或硬件事件的信息.通过 EventLog,可以读取现有日志,向日志中写入项,创建或删除事件源 ...
- .NET 操作 EventLog(Windows事件日志监控)(转载)
操作Windows日志:EventLog 如果要在.NET Core控制台项目中使用EventLog(Windows事件日志监控),首先需要下载Nuget包: System.Diagnostics.E ...
随机推荐
- 使用.NET Core优雅获取并展示最新疫情数据
前言 新型冠状病毒的出现,着实让人紧张.我每天一大早都会去查看今天的最新数据,可是每次的数据都挺让人揪心的.今天突然间很想看看过去的历史的数据,结果查了很多资料都不是很全.反正国家让我们待在家里做贡献 ...
- SpringBoot学习(3) - jdbc
数据库使用MySQL 5.7.18版本. 装配DataSource的步骤:1.加入数据库驱动 pom.xml: <project xmlns="http://maven.apache. ...
- ps-如何移动照片里的内容
1.我们用内容感知移动工具把所要移动的区域大致勾选出来. 2.然后我们将所选区域拖动(点击鼠标左键不放拖动). 3.松开鼠标 4.仿制图章工具,alt 键取样,涂抹
- pycharm中的快捷键
不断更新...... 1.整行向右缩进 方法:选中要向右缩进的代码,点击一次[Tab]键,向右进行一个缩进,点击2次就缩进2个[Tab]键 2.整行向左退[缩进] 方法:选中要退缩进的代码,同时点击[ ...
- Codeforces_446_B
http://codeforces.com/problemset/problem/446/B 分别将每行的和与每列的和存入优先队列,计算操作n次的最大和,保存每一次结果. 枚举行和列操作的次数,注意要 ...
- java2变量数据类型和运算符
public class jh_11_加加减减运算符 { public static void main(String[] args) { int a = 5,b =2 ; a ++;// 对自身 ...
- redis基础知识汇总
- qt creator源码全方面分析(2-10)
目录 Creating Plugins Creating Plugins Qt Creator的核心是一个插件加载程序,加载并运行一组插件,实际上是这些插件提供了您从Qt Creator IDE中了解 ...
- qt creator源码全方面分析(2-3-1)
目录 Using External Tools 使用Qt语言学家 预览QML文件 使用外部文本编辑器 配置外部工具 Using External Tools 您可以直接从Qt Creator中使用外部 ...
- VFP9利用_GdiPlus类处理图片分辨率及缩放
VFP利用GDI来处理图片,已经不是一件太难的事了.GdiPlus类就是专门来干这事的,有关其属性等请参考其它资料.下面将处理图片缩放及分辨率的代码示例贴出来.这些代码都是很久以前的了,由于新冠宅家无 ...