发布于 2017-01-23 23:53:28 | 97 次阅读 | 评论: 0 | 来源: 网友投递
SeasLog PHP日志扩展
SeasLog是一个PHP的日志扩展.log日志,通常是系统或软件、应用的运行记录。通过log的分析,可以方便用户了解系统或软件、应用的运行情况;如果你的应用log足够丰富,也可以分 析以往用户的操作行为、类型喜好、地域分布或其他更多信息;如果一个应用的log同时也分了多个级别,那么可以很轻易地分析得到该应用的健康状况,及时发 现问题并快速定位、解决问题,补救损失。
SeasLog-1.6.9 发布了,修复setBasePath函数bug,增加php --re SeasLog骨架输出。
改进日志:
1.6.9
- Fixed issue #113 setBastPath.
- Fixed PHP —re SeasLog result.
1.6.8: 优化内存使用和性能,修复已知Bug.
- Fixed issue #97 PHP5.* Cached Block.
- Fixed issue #98 SeasLog::analyzerDetail(NULL).
- Fixed issue #100 #102 #103 memory leak.
- Fixed Dir chmod 0755 and File chmod 0666.
1.6.0:此次改进支持appender配置,同时支持File,TCP,UDP三种发送方式。
- Fixed issue #90 #92 Date format.
- Fixed issue #96 Clear memory leaks.
- Fixed issue #91 Support TCP UDP appender.
- Support appender with config, switch File TCP UDP; config the remote_host and remote_port.
为什么使用 SeasLog
log日志,通常是系统或软件、应用的运行记录。通过log的分析,可以方便用户了解系统或软件、应用的运行情况;如果你的应用log足够丰富,也可以分析以往用户的操作行为、类型喜好、地域分布或其他更多信息;如果一个应用的log同时也分了多个级别,那么可以很轻易地分析得到该应用的健康状况,及时发现问题并快速定位、解决问题,补救损失。
php内置error_log、syslog函数功能强大且性能极好,但由于各种缺陷(error_log无错误级别、无固定格式,syslog不分模块、与系统日志混合),灵活度降低了很多,不能满足应用需求。
好消息是,有不少第三方的log类库弥补了上述缺陷,如log4php、plog、Analog等(当然也有很多应用在项目中自己开发的log类)。其中以log4php最为著名,设计精良、格式完美、文档完善、功能强大。推荐。
不过log4php在性能方面表现非常差,下图是SeasLog与log4php的ab并发性能测试( 测试环境:Ubuntu12.04单机,CPU I3,内存 16G,硬盘 SATA 7200):
那么有没有一种log类库满足以下需求呢:
分模块、分级别
配置简单(最好是勿须配置)
日志格式清晰易读
应用简单、性能很棒
SeasLog 正是应此需求而生。
目前提供了什么
在PHP项目中便捷、规范地记录log
可配置的默认log目录与模块
指定log目录与获取当前配置
初步的分析预警框架
高效的日志缓冲、便捷的缓冲debug
遵循 PSR-3 日志接口规范
自动记录错误信息
自动记录异常信息
支持TCP发送
支持UDP发立大
目标是怎样的
便捷、规范的log记录
高效的海量log分析
可配置、多途径的log预警
编译安装 SeasLog
$ /path/to/phpize $ ./configure --with-php-config=/path/to/php-config $ make && make install
PEC 安装 SeasLog
$ pecl install seaslog
seaslog.ini 的配置
; configuration for php SeasLog module extension = seaslog.so seaslog.default_basepath = /log/seaslog-test ;默认log根目录 seaslog.default_logger = default ;默认logger目录 seaslog.disting_type = 1 ;是否以type分文件 1是 0否(默认) seaslog.disting_by_hour = 1 ;是否每小时划分一个文件 1是 0否(默认) seaslog.use_buffer = 1 ;是否启用buffer 1是 0否(默认) seaslog.buffer_size = 100 ;buffer中缓冲数量 默认0(不使用buffer_size) seaslog.level = 0 ;记录日志级别 默认0(所有日志) seaslog.trace_error = 1 ;自动记录错误 默认1(开启) seaslog.trace_exception = 0 ;自动记录异常信息 默认0(关闭) seaslog.default_datetime_format = "Y:m:d H:i:s" ;日期格式配置 默认"Y:m:d H:i:s" seaslog.appender = 1 ;日志存储介质 1File 2TCP 3UDP (默认为1) seaslog.remote_host = 127.0.0.1 ;接收ip 默认127.0.0.1 (当使用TCP或UDP时必填) seaslog.remote_port = 514 ;接收端口 默认514 (当使用TCP或UDP时必填)
seaslog.disting_type = 1 开启以type分文件,即log文件区分infowarnerro
seaslog.disting_by_hour = 1 开启每小时划分一个文件
seaslog.use_buffer = 1 开启buffer。默认关闭。当开启此项时,日志预存于内存,当请求结束时(或异常退出时)一次写入文件。
常量列表
* SEASLOG_DEBUG "debug" * SEASLOG_INFO "info" * SEASLOG_NOTICE "notice" * SEASLOG_WARNING "warning" * SEASLOG_ERROR "error" * SEASLOG_CRITICAL "critical" * SEASLOG_ALERT "alert" * SEASLOG_EMERGENCY "emergency" var_dump(SEASLOG_DEBUG,SEASLOG_INFO,SEASLOG_NOTICE); /* string('debug') debug级别 string('info') info级别 string('notice') notice级别 */
函数列表
SeasLog 提供了这样一组函数,可以方便地获取与设置根目录、模块目录、快速写入与统计log。 相信从下述伪代码的注释中,您可以快速获取函数信息,具体使用将紧接其后:
<?php /** * @author neeke@php.net * Date: 14-1-27 下午4:47 */ class SeasLog { public function __construct() { #SeasLog init } public function __destruct() { #SeasLog distroy } /** * 设置basePath * * @param $basePath * * @return bool */ static public function setBasePath($basePath) { return TRUE; } /** * 获取basePath * * @return string */ static public function getBasePath() { return 'the base_path'; } /** * 设置模块目录 * @param $module * * @return bool */ static public function setLogger($module) { return TRUE; } /** * 获取最后一次设置的模块目录 * @return string */ static public function getLastLogger() { return 'the lastLogger'; } /** * 设置DatetimeFormat配置 * @param $format * * @return bool */ static public function setDatetimeFormat($format) { return TRUE; } /** * 返回当前DatetimeFormat配置格式 * @return string */ static public function getDatetimeFormat() { return 'the datetimeFormat'; } /** * 统计所有类型(或单个类型)行数 * @param string $level * @param string $log_path * @param null $key_word * * @return array | long */ static public function analyzerCount($level = 'all', $log_path = '*', $key_word = NULL) { return array(); } /** * 以数组形式,快速取出某类型log的各行详情 * * @param $level * @param string $log_path * @param null $key_word * @param int $start * @param int $limit * @param $order * * @return array */ static public function analyzerDetail($level = SEASLOG_INFO, $log_path = '*', $key_word = NULL, $start = 1, $limit = 20, $order = SEASLOG_DETIAL_ORDER_ASC) { return array(); } /** * 获得当前日志buffer中的内容 * * @return array */ static public function getBuffer() { return array(); } /** * 将buffer中的日志立刻刷到硬盘 * * @return bool */ static public function flushBuffer() { return TRUE; } /** * 记录debug日志 * * @param $message * @param array $content * @param string $module */ static public function debug($message, array $content = array(), $module = '') { #$level = SEASLOG_DEBUG } /** * 记录info日志 * * @param $message * @param array $content * @param string $module */ static public function info($message, array $content = array(), $module = '') { #$level = SEASLOG_INFO } /** * 记录notice日志 * * @param $message * @param array $content * @param string $module */ static public function notice($message, array $content = array(), $module = '') { #$level = SEASLOG_NOTICE } /** * 记录warning日志 * * @param $message * @param array $content * @param string $module */ static public function warning($message, array $content = array(), $module = '') { #$level = SEASLOG_WARNING } /** * 记录error日志 * * @param $message * @param array $content * @param string $module */ static public function error($message, array $content = array(), $module = '') { #$level = SEASLOG_ERROR } /** * 记录critical日志 * * @param $message * @param array $content * @param string $module */ static public function critical($message, array $content = array(), $module = '') { #$level = SEASLOG_CRITICAL } /** * 记录alert日志 * * @param $message * @param array $content * @param string $module */ static public function alert($message, array $content = array(), $module = '') { #$level = SEASLOG_ALERT } /** * 记录emergency日志 * * @param $message * @param array $content * @param string $module */ static public function emergency($message, array $content = array(), $module = '') { #$level = SEASLOG_EMERGENCY } /** * 通用日志方法 * @param $level * @param $message * @param array $content * @param string $module */ static public function log($level, $message, array $content = array(), $module = '') { } }
SeasLog扩展骨架
/usr/local/php/php-7.0.6-zts-debug/bin/php --re seaslog Extension [ <persistent> extension #32 SeasLog version 1.6.9 ] { - Dependencies { } - INI { Entry [ seaslog.default_basepath <ALL> ] Current = '/var/log/www' } Entry [ seaslog.default_logger <ALL> ] Current = 'defauult' } Entry [ seaslog.default_datetime_format <ALL> ] Current = 'Y:m:d H:i:s' } Entry [ seaslog.disting_type <ALL> ] Current = '0' } Entry [ seaslog.disting_by_hour <ALL> ] Current = '0' } Entry [ seaslog.use_buffer <ALL> ] Current = '1' } Entry [ seaslog.trace_error <ALL> ] Current = '1' } Entry [ seaslog.trace_exception <ALL> ] Current = '1' } Entry [ seaslog.buffer_size <ALL> ] Current = '10' } Entry [ seaslog.level <ALL> ] Current = '0' } Entry [ seaslog.appender <ALL> ] Current = '1' } Entry [ seaslog.remote_host <ALL> ] Current = '127.0.0.1' } Entry [ seaslog.remote_port <ALL> ] Current = '514' } } - Constants [16] { Constant [ string SEASLOG_VERSION ] { 1.6.9 } Constant [ string SEASLOG_AUTHOR ] { Chitao.Gao [ neeke@php.net ] } Constant [ string SEASLOG_ALL ] { all } Constant [ string SEASLOG_DEBUG ] { debug } Constant [ string SEASLOG_INFO ] { info } Constant [ string SEASLOG_NOTICE ] { notice } Constant [ string SEASLOG_WARNING ] { warning } Constant [ string SEASLOG_ERROR ] { error } Constant [ string SEASLOG_CRITICAL ] { critical } Constant [ string SEASLOG_ALERT ] { alert } Constant [ string SEASLOG_EMERGENCY ] { emergency } Constant [ integer SEASLOG_DETAIL_ORDER_ASC ] { 1 } Constant [ integer SEASLOG_DETAIL_ORDER_DESC ] { 2 } Constant [ integer SEASLOG_APPENDER_FILE ] { 1 } Constant [ integer SEASLOG_APPENDER_TCP ] { 2 } Constant [ integer SEASLOG_APPENDER_UDP ] { 3 } } - Functions { Function [ <internal:SeasLog> function seaslog_get_version ] { } Function [ <internal:SeasLog> function seaslog_get_author ] { } } - Classes [1] { Class [ <internal:SeasLog> class SeasLog ] { - Constants [0] { } - Static properties [0] { } - Static methods [19] { Method [ <internal:SeasLog> static public method setBasePath ] { - Parameters [1] { Parameter #0 [ <required> $base_path ] } } Method [ <internal:SeasLog> static public method getBasePath ] { } Method [ <internal:SeasLog> static public method setLogger ] { - Parameters [1] { Parameter #0 [ <required> $logger ] } } Method [ <internal:SeasLog> static public method getLastLogger ] { } Method [ <internal:SeasLog> static public method setDatetimeFormat ] { - Parameters [1] { Parameter #0 [ <required> $format ] } } Method [ <internal:SeasLog> static public method getDatetimeFormat ] { } Method [ <internal:SeasLog> static public method analyzerCount ] { - Parameters [3] { Parameter #0 [ <required> $level ] Parameter #1 [ <optional> $log_path ] Parameter #2 [ <optional> $key_word ] } } Method [ <internal:SeasLog> static public method analyzerDetail ] { - Parameters [6] { Parameter #0 [ <required> $level ] Parameter #1 [ <optional> $log_path ] Parameter #2 [ <optional> $key_word ] Parameter #3 [ <optional> $start ] Parameter #4 [ <optional> $limit ] Parameter #5 [ <optional> $order ] } } Method [ <internal:SeasLog> static public method getBuffer ] { } Method [ <internal:SeasLog> static public method flushBuffer ] { } Method [ <internal:SeasLog> static public method log ] { - Parameters [4] { Parameter #0 [ <required> $level ] Parameter #1 [ <optional> $message ] Parameter #2 [ <optional> $content ] Parameter #3 [ <optional> $logger ] } } Method [ <internal:SeasLog> static public method debug ] { - Parameters [3] { Parameter #0 [ <required> $message ] Parameter #1 [ <optional> $content ] Parameter #2 [ <optional> $logger ] } } Method [ <internal:SeasLog> static public method info ] { - Parameters [3] { Parameter #0 [ <required> $message ] Parameter #1 [ <optional> $content ] Parameter #2 [ <optional> $logger ] } } Method [ <internal:SeasLog> static public method notice ] { - Parameters [3] { Parameter #0 [ <required> $message ] Parameter #1 [ <optional> $content ] Parameter #2 [ <optional> $logger ] } } Method [ <internal:SeasLog> static public method warning ] { - Parameters [3] { Parameter #0 [ <required> $message ] Parameter #1 [ <optional> $content ] Parameter #2 [ <optional> $logger ] } } Method [ <internal:SeasLog> static public method error ] { - Parameters [3] { Parameter #0 [ <required> $message ] Parameter #1 [ <optional> $content ] Parameter #2 [ <optional> $logger ] } } Method [ <internal:SeasLog> static public method critical ] { - Parameters [3] { Parameter #0 [ <required> $message ] Parameter #1 [ <optional> $content ] Parameter #2 [ <optional> $logger ] } } Method [ <internal:SeasLog> static public method alert ] { - Parameters [3] { Parameter #0 [ <required> $message ] Parameter #1 [ <optional> $content ] Parameter #2 [ <optional> $logger ] } } Method [ <internal:SeasLog> static public method emergency ] { - Parameters [3] { Parameter #0 [ <required> $message ] Parameter #1 [ <optional> $content ] Parameter #2 [ <optional> $logger ] } } } - Properties [0] { } - Methods [2] { Method [ <internal:SeasLog, ctor> public method __construct ] { } Method [ <internal:SeasLog, dtor> public method __destruct ] { } } } } }
使用SeasLog进行健康预警
预警的配置
[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
Demo:
<?php /** * @author ciogao@gmail.com * Date: 14-1-27 下午4:41 */ SeasLog::log(SEASLOG_ERROR,'this is a error test by ::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')); echo "n";