PHP 日志擴展 SeasLog-1.4.6 發布,性能更優
SeasLog-1.4.6 發布!
Change log:
1.4.6
- Fixed issue #52 update README fixed SEASLOG_ERRO and company list from.
- Fixed issue #53 Add function flushBuffer,support independent refresh by manual.
- Fixed issue #57 and #58 Update analyzerDetail by desc and fixed limit value. </p>
為什么使用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 日志接口規范
-
自動記錄錯誤信息
-
自動記錄異常信息
目標是怎樣的
-
便捷、規范的log記錄
-
高效的海量log分析
-
可配置、多途徑的log預警
安裝
編譯安裝 seaslog
$ /path/to/phpize $ ./configure --with-php-config=/path/to/php-config $ make && make install
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.disting_type = 1 開啟以type分文件,即log文件區分info\warn\erro
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級別/</pre>
函數列表
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'; } /** * 統計所有類型(或單個類型)行數 * @param $level * @param string $log_path * @return array | long */ static public function analyzerCount($level = 'all',$log_path = '*') { return array(); } /** * 以數組形式,快速取出某類型log的各行詳情 * @param $level * @param string $log_path * @return array */ static public function analyzerDetail($level = SEASLOG_INFO,$log_path = '*') { 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進行健康預警
預警的配置
[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";