在项目中如何使用

获取与设置basePath

  1. $basePath_1 = SeasLog::getBasePath(); 
  2.  
  3. SeasLog::setBasePath('/log/base_test'); 
  4. $basePath_2 = SeasLog::getBasePath(); 
  5.  
  6. var_dump($basePath_1,$basePath_2); 
  7.  
  8. /* 
  9. string(19) "/log/seaslog-ciogao" 
  10. string(14) "/log/base_test" 
  11. */ 

直接使用 SeasLog::getBasePath(),将获取php.ini(seaslog.ini)中设置的seaslog.default_basepath 的值。

使用 SeasLog::getBasePath() 函数,将改变 seaslog_get_basepath() 的取值。

设置logger与获取lastLogger

  1. $lastLogger_1 = SeasLog::getLastLogger(); 
  2.  
  3. SeasLog::setLogger('testModule/app1'); 
  4. $lastLogger_2 = SeasLog::getLastLogger(); 
  5.  
  6. var_dump($lastLogger_1,$lastLogger_2); 
  7. /* 
  8. string(7) "default" 
  9. string(15) "testModule/app1" 
  10. */ 

与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,可以通过简单的配置,完成及时发现问题并实时发送预警邮件的功能。

预警的配置

  1. [base] 
  2. wait_analyz_log_path = /log/base_test 
  3.  
  4. [fork] 
  5. ;是否开启多线程 1开启 0关闭 
  6. fork_open = 1 
  7.  
  8. ;线程个数 
  9. fork_count = 3 
  10.  
  11. [warning] 
  12. email[smtp_host] = smtp.163.com 
  13. email[smtp_port] = 25 
  14. email[subject_pre] = 预警邮件 - 
  15. email[smtp_user] = seaslogdemo@163.com 
  16. email[smtp_pwd] = seaslog#demo 
  17. email[mail_from] = seaslogdemo@163.com 
  18. email[mail_to] = gaochitao@weiboyi.com 
  19. email[mail_cc] = ciogao@gmail.com 
  20. email[mail_bcc] = 
  21.  
  22. [analyz] 
  23. ; enum 
  24. ; SEASLOG_DEBUG      "debug" 
  25. ; SEASLOG_INFO       "info" 
  26. ; SEASLOG_NOTICE     "notice" 
  27. ; SEASLOG_WARNING    "warning" 
  28. ; SEASLOG_ERROR      "error" 
  29. ; SEASLOG_CRITICAL   "critical" 
  30. ; SEASLOG_ALERT      "alert" 
  31. ; SEASLOG_EMERGENCY  "emergency" 
  32.  
  33. test1[module] = test/bb 
  34. test1[level] = SEASLOG_ERROR 
  35. test1[bar] = 1 
  36. test1[mail_to] = gaochitao@weiboyi.com 
  37.  
  38. test2[module] = 222 
  39. test2[level] = SEASLOG_WARNING 
  40.  
  41. test3[module] = 333 
  42. test3[level] = SEASLOG_CRITICAL 
  43.  
  44. test4[module] = 444 
  45. test4[level] = SEASLOG_EMERGENCY 
  46.  
  47. test5[module] = 555 
  48. test5[level] = SEASLOG_DEBUG 

crontab配置

  1. ;每天凌晨3点执行 
  2. 0 3 * * * /path/to/php /path/to/SeasLog/Analyzer/SeasLogAnalyzer.php 




相关内容