yii2日志组件,Sentry日志收集,kafka消息队列

jianfly.com 2019-01-31 7162次浏览

日志系统

web.php配置

'log' => [
'travelevel'=> YII_DEBUG ? 3 : 0;//追踪级别
'targets' => [
[
‘class’ => 'yii\log\FileTarget',  //文件
'levels' => ['error', 'warning'],  //记录的错误级别
‘logFile’ => '@app/runtime/logs/shop/application.log',  //指定日志文件位置
],
[
'class' =>  'yii\log\FileTarget',
'levels' => ['info', 'trace'],
'logFile' => '@app/runtime/logs/shop/info.log',
'categories' => ['myinfo'], //指定分类 Yii::info('this is info', myinfo);
'logVars' => [],  //不记默认的
],
[ //邮件日志,不建议使用,速度慢
'class' => 'yii\log\EmailTarget',
'mailer' => 'mailer',
'levels' => ['error', 'warning'],
'message' => [
'from' => ['imooc_shop@163.com'],
'to' => ['86267659@qq.com'],
'subject' => 'imooc_shop的日志'
],
],
],
]

写日志

Yii::trace('this is trace');  //levels中要增加trace     'levels' => ['trace', 'error', 'warning']
Yii::info('this is info');  //'levels' => ['info', 'trace', 'error', 'warning']

性能分析

Yii::beginProfile('mybench');
Yii::endProfile('mybench');

Sentry日志收集,官网sentry.io,github搜索安装Yii2 Sentry(mito/yii2-sentry)

'sentry' => [
    'class' => 'mito\sentry\SentryComponent',
    'dsn' => 'https://b54b26f05efd42e8879e957cb7d76273:93014636f46a4620a40ca35155385e56@sentry.io/122995', // private DSN
    'publicDsn' => 'https://b54b26f05efd42e8879e957cb7d76273@sentry.io/122995',
    'environment' => YII_CONFIG_ENVIRONMENT, // if not set, the default is `development`
    'jsNotifier' => true, // to collect JS errors
    'clientOptions' => [ // raven-js config parameter
        'whitelistUrls' => [ // collect JS errors from these urls 为空收集所有
            //'http://staging.my-product.com',
            //'https://my-product.com',
        ],
    ],
],

image

kafka消息队列

流量大时redis队列不可靠

需要一台新服务器,wget下载kafka,解压,进入

进入config文件夹,主要做两个配置:server.properties 和 zookeeper.properties

server

image

启动(安装java-jdk)

bin/zookeeper-service-start.sh config/zookeeper.properties

bin/kafka-server-start.sh config/server.properties

启动消费者

bin/kafka-console-consumer.sh –zookeeper localhost:2181 –topic test –from-beginning

启动生产者

bin/kafka-sonsole-producer.sh –broker-list 192.168.199.150:9092 –topic test

生产者端生产消息,消费者端能收到消息

php操作kafka

安装kafka拓展

github搜librdkafka

./configure

make && make install

github搜php-rdkafka

phpize

./configure –with-php-config=/usr/local/php/bin/php-config

make && make install

之后会在/usr/local/php/lib/php/extensions/no-debug-non-zts-20131223/中生产so文件

php.ini中配置

image

php -m查看拓展

编写kafka类

Kafka

web.php中添加

image

Yii::$app->asyncLog->send([‘this is indexcontroller’]); //发送消息

console.php中增加配置

image

image

commands中建立KafkaController.php

KafkaController

命令调用 ./yii kafka/consume