日志

配置

'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);
}
}

最新文章

  1. ArrayList源码阅读笔记(基于JDk1.8)
  2. linux 上安装redis
  3. COGS14. [网络流24题] 搭配飞行员
  4. HTMl5的sessionStorage和localStorage
  5. ESPCMS基本导航操作
  6. jsnop
  7. UIMenuController和UIMenuItem的使用
  8. linux 线程笔记
  9. ECSHOP模糊分词搜索和商品列表关键字飘红功能
  10. &lt;script type=&quot;text/html&quot;&gt;&lt;/script&gt; js模版使用
  11. bzoj2631 3282
  12. ng-class的使用
  13. 深入理解Azure自动扩展集VMSS(1)
  14. apache安装扩展模块
  15. connectVisualVMtoTomcat
  16. Android 开发——如何显示 GIF 动画
  17. ps 替换背景以及调整尺寸
  18. 11、ABPZero系列教程之拼多多卖家工具 拼团提醒功能页面实现
  19. IDEA或者WebStorm关闭JS文件的黄色提示
  20. BZOJ1069 SCOI2007最大土地面积(凸包+旋转卡壳)

热门文章

  1. 聊聊“装箱”在CLR内部的实现
  2. winform应用如何发布(不用打包)、并提醒用户自动更新
  3. 2019牛客多校2 F Partition problem(dfs)
  4. 一口气说出Redis 5种数据结构及对应使用场景,面试要加分的
  5. 【WPF学习】第四十四章 图画
  6. 如何高效地远程部署?自动化运维利器 Fabric 教程
  7. [Effective Java 读书笔记] 第6章 枚举和注解
  8. git-gitlab-github集合
  9. k8s系列---hpa扩容
  10. 使用 pyenv 管理不同的 Python 版本