使用SeasLog打造PHP项目中的高性能日志组件(1)(3)
在项目中如何使用
获取与设置basePath
- $basePath_1 = SeasLog::getBasePath();
- SeasLog::setBasePath('/log/base_test');
- $basePath_2 = SeasLog::getBasePath();
- var_dump($basePath_1,$basePath_2);
- /*
- string(19) "/log/seaslog-ciogao"
- string(14) "/log/base_test"
- */
直接使用 SeasLog::getBasePath(),将获取php.ini(seaslog.ini)中设置的seaslog.default_basepath 的值。
使用 SeasLog::getBasePath() 函数,将改变 seaslog_get_basepath() 的取值。
设置logger与获取lastLogger
- $lastLogger_1 = SeasLog::getLastLogger();
- SeasLog::setLogger('testModule/app1');
- $lastLogger_2 = SeasLog::getLastLogger();
- var_dump($lastLogger_1,$lastLogger_2);
- /*
- string(7) "default"
- string(15) "testModule/app1"
- */
与basePath相类似的,
直接使用 SeasLog::getLastLogger(),将获取php.ini(seaslog.ini)中设置的seaslog.default_logger 的值。
使用 SeasLog::setLogger() 函数,将改变 SeasLog::getLastLogger() 的取值。
快速写入log
上面已经设置过了basePath与logger,于是log记录的目录已经产生了,
log记录目录 = basePath / logger / {fileName}.log log文件名,以 年月日 分文件,如今天是2014年02月18日期,那么 {fileName} = 20140218;
还记得 php.ini 中设置的 seaslog.disting_type 吗?
默认的 seaslog.disting_type = 0,如果今天我使用了 SeasLog ,那么将产生最终的log文件:
●LogFile = basePath / logger / 20140218.log
如果 seaslog.disting_type = 1,则最终的log文件将是这样的三个文件
●infoLogFile = basePath / logger / INFO.20140218.log ●warnLogFile = basePath / logger / WARN.20140218.log ●erroLogFile = basePath / logger / ERRO.20140218.log
于是可以这么快速地记录log:
SeasLog::debug('this is a {userName} debug',array('{userName}' => 'neeke')); SeasLog::info('this is a info log'); SeasLog::notice('this is a notice log'); SeasLog::warning('your {website} was down,please {action} it ASAP!',array('{website}' => 'github.com','{action}' => 'rboot')); SeasLog::error('a error log'); SeasLog::critical('some thing was critical'); SeasLog::alert('yes this is a {messageName}',array('{messageName}' => 'alertMSG')); SeasLog::emergency('Just now, the house next door was completely burnt out! {note}',array('{note}' => 'it`s a joke'));
既然已经作为PHP的扩展存在,那么,此时可以在任何使用PHP的地方,方便地使用SeasLog来记录你的log了。
这里有一个小技巧,在一个框架controller构造方法中,或在一个Service的构造方法中,使用SeasLog::setLogger() 方法,则可以将下文中所有的log记录在同一个模块中。同时,由于SeasLog记录的log中,默认记录了进程pid,所以可以有效地通过pid,将上下文相关的日志非常快速地联系起来。
如下:
debug | 6886 | 1422894818.129 | 2015:02:03 00:33:38 | this is a neeke debug info | 6886 | 1422894818.129 | 2015:02:03 00:33:38 | this is a info log notice | 6886 | 1422894818.129 | 2015:02:03 00:33:38 | this is a notice log warning | 6886 | 1422894818.129 | 2015:02:03 00:33:38 | your github.com was down,please rboot it ASAP! error | 6886 | 1422894818.129 | 2015:02:03 00:33:38 | a error log critical | 6886 | 1422894818.129 | 2015:02:03 00:33:38 | some thing was critical alert | 6886 | 1422894818.129 | 2015:02:03 00:33:38 | yes this is a alertMSG emergency | 6886 | 1422894818.129 | 2015:02:03 00:33:38 | Just now, the house next door was completely burnt out! it`s a joke debug | 6888 | 1422894820.234 | 2015:02:03 00:33:40 | this is a neeke debug info | 6888 | 1422894820.235 | 2015:02:03 00:33:40 | this is a info log notice | 6888 | 1422894820.235 | 2015:02:03 00:33:40 | this is a notice log warning | 6888 | 1422894820.235 | 2015:02:03 00:33:40 | your github.com was down,please rboot it ASAP! error | 6888 | 1422894820.235 | 2015:02:03 00:33:40 | a error log critical | 6888 | 1422894820.235 | 2015:02:03 00:33:40 | some thing was critical alert | 6888 | 1422894820.235 | 2015:02:03 00:33:40 | yes this is a alertMSG emergency | 6888 | 1422894820.235 | 2015:02:03 00:33:40 | Just now, the house next door was completely burnt out! it`s a joke
使用SeasLog进行健康预警
SeasLog同时也提供了一个简单的预警demo,可以通过简单的配置,完成及时发现问题并实时发送预警邮件的功能。
预警的配置
- [base]
- wait_analyz_log_path = /log/base_test
- [fork]
- ;是否开启多线程 1开启 0关闭
- fork_open = 1
- ;线程个数
- fork_count = 3
- [warning]
- email[smtp_host] = smtp.163.com
- email[smtp_port] = 25
- email[subject_pre] = 预警邮件 -
- email[smtp_user] = seaslogdemo@163.com
- email[smtp_pwd] = seaslog#demo
- email[mail_from] = seaslogdemo@163.com
- email[mail_to] = gaochitao@weiboyi.com
- email[mail_cc] = ciogao@gmail.com
- email[mail_bcc] =
- [analyz]
- ; enum
- ; SEASLOG_DEBUG "debug"
- ; SEASLOG_INFO "info"
- ; SEASLOG_NOTICE "notice"
- ; SEASLOG_WARNING "warning"
- ; SEASLOG_ERROR "error"
- ; SEASLOG_CRITICAL "critical"
- ; SEASLOG_ALERT "alert"
- ; SEASLOG_EMERGENCY "emergency"
- test1[module] = test/bb
- test1[level] = SEASLOG_ERROR
- test1[bar] = 1
- test1[mail_to] = gaochitao@weiboyi.com
- test2[module] = 222
- test2[level] = SEASLOG_WARNING
- test3[module] = 333
- test3[level] = SEASLOG_CRITICAL
- test4[module] = 444
- test4[level] = SEASLOG_EMERGENCY
- test5[module] = 555
- test5[level] = SEASLOG_DEBUG
crontab配置
- ;每天凌晨3点执行
- 0 3 * * * /path/to/php /path/to/SeasLog/Analyzer/SeasLogAnalyzer.php
评论暂时关闭