簡介
為什么使用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端口,以RFC5424格式發送日志
- 連接UDP端口,以RFC5424格式發送日志
- 支持RequestId區分請求
- 支持日志模板自定義
目標是怎樣的
- 便捷、規范的log記錄
- 高效的海量log分析
- 可配置、多途徑的log預警
安裝
編譯安裝 SeasLog
$ wget https://github.com/Neeke/SeasLog/archive/master.zip
$ unzip master.zip
$ cd /path/to/SeasLog
$ phpize
$ ./configure --with-php-config=/path/to/php-config
$ make -j2
$ sudo make install
seaslog.ini的配置
[SeasLog]
;configuration for php SeasLog module
extension = seaslog.so
;默認log根目錄
seaslog.default_basepath = "/var/log/www"
;默認logger目錄
seaslog.default_logger = "default"
;日期格式配置 默認"Y-m-d H:i:s"
seaslog.default_datetime_format = "Y-m-d H:i:s"
;日志格式模板 默認"%T | %L | %P | %Q | %t | %M"
seaslog.default_template = "%T | %L | %P | %Q | %t | %M"
;是否以type分文件 1是 0否(默認)
seaslog.disting_type = 1
;是否每小時劃分一個文件 1是 0否(默認)
seaslog.disting_by_hour = 0
;是否啟用buffer 1是 0否(默認)
seaslog.use_buffer = 0
;buffer中緩沖數量 默認0(不使用buffer_size)
seaslog.buffer_size = 100
;記錄日志級別,數字越大,根據級別記的日志越多。
;0-EMERGENCY 1-ALERT 2-CRITICAL 3-ERROR 4-WARNING 5-NOTICE 6-INFO 7-DEBUG 8-ALL
;默認8(所有日志)
;
; 注意, 該配置項自1.7.0版本開始有變動。
; 在1.7.0版本之前, 該值數字越小,根據級別記的日志越多:
; 0-all 1-debug 2-info 3-notice 4-warning 5-error 6-critical 7-alert 8-emergency
; 1.7.0 之前的版本,該值默認為0(所有日志);
seaslog.level = 8
;日志函數調用回溯層級
;影響預定義變量 %F 中的行數
;默認0
seaslog.recall_depth = 0
;自動記錄錯誤 默認1(開啟)
seaslog.trace_error = 1
;自動記錄異常信息 默認0(關閉)
seaslog.trace_exception = 0
;日志存儲介質 1File 2TCP 3UDP (默認為1)
seaslog.appender = 1
;寫入重試次數
;默認0(不重試)
seaslog.appender_retry = 0
;接收ip 默認127.0.0.1 (當使用TCP或UDP時必填)
seaslog.remote_host = "127.0.0.1"
;接收端口 默認514 (當使用TCP或UDP時必填)
seaslog.remote_port = 514
;過濾日志中的回車和換行符 (默認為0)
seaslog.trim_wrap = 0
;是否開啟拋出SeasLog自身異常 1開啟(默認) 0否
seaslog.throw_exception = 1
;是否開啟忽略SeasLog自身warning 1開啟(默認) 0否
seaslog.ignore_warning = 1
- seaslog.disting_type = 1 開啟以type分文件,即log文件區分info\warn\erro
- seaslog.disting_by_hour = 1 開啟每小時劃分一個文件
- seaslog.use_buffer = 1 開啟buffer。默認關閉。當開啟此項時,日志預存于內存,當請求結束時(或異常退出時)一次寫入文件。
- seaslog.buffer_size = 100 設置緩沖數量為100. 默認為0,即無緩沖數量限制.當buffer_size大于0時,緩沖量達到該值則寫一次文件.
- seaslog.level = 8 記錄的日志級別.默認為8,即所有日志均記錄。
- seaslog.level = 0 記錄EMERGENCY。
- seaslog.level = 1 記錄EMERGENCY、ALERT。
- seaslog.level = 2 記錄EMERGENCY、ALERT、CRITICAL。
- seaslog.level = 3 記錄EMERGENCY、ALERT、CRITICAL、ERROR。
- seaslog.level = 4 記錄EMERGENCY、ALERT、CRITICAL、ERROR、- WARNING。
- seaslog.level = 5 記錄EMERGENCY、ALERT、CRITICAL、ERROR、WARNING、NOTICE。
- seaslog.level = 6 記錄EMERGENCY、ALERT、CRITICAL、ERROR、WARNING、NOTICE、INFO。
- seaslog.level = 7 記錄EMERGENCY、ALERT、CRITICAL、ERROR、WARNING、NOTICE、INFO、DEBUG。
- seaslog.throw_exception = 1 開啟拋出SeasLog拋出自身的異常。當出現錄權限或接收服務器端口不通等情況時,拋出異常;關閉時不拋出異常。
- seaslog.ignore_warning = 1 開啟忽略SeasLog自身的警告。當出現目錄權限或接收服務器端口不通等情況時,將進行忽略;關閉時,將拋出警告。
自定義日志模板
很多朋友在使用過程中提到自定義日志模板的需求,于是自1.7.2版本開始,擁有了這個能力,允許用戶自定義日志的模板, 同時在模板中可以使用預置的諸多預設變量,參照預設變量表。
日志模板說明
- 模板默認為:seaslog.default_template = "%T | %L | %P | %Q | %t | %M",意味著,默認的格式為{dateTime} | {level} | {pid} | {uniqid} | {timeStamp} | {logInfo}
- 如果自定義的格式為:seaslog.default_template = "[%T]:%L %P %Q %t %M"
那么,日志格式將被自定義為:[{dateTime}]:{level} {pid} {uniqid} {timeStamp} {logInfo}
注意:%L 必須在%M之前,即:日志級別,必須在日志內容之前。
自定義日志模版變量表
SeasLog提供了下列預設變量,可以直接使用在日志模板中,將在日志最終生成時替換成對應值。
- %L - Level 日志級別。
- %M - Message 日志信息。
- %T - DateTime 如2017-08-16 19:15:02,受seaslog.default_datetime_format影響。
- %t - Timestamp 如1502882102.862,精確到毫秒數。
- %Q - RequestId 區分單次請求,如沒有調用SeasLog::setRequestId($string)方法,則在初始化請求時,采用內置的static char *get_uniqid()方法生成的惟一值。
- %H - HostName 主機名。
- %P - ProcessId 進程ID。
- %D - Domain:Port 域名:口號,如www.cloudwise.com:8080; Cli模式下為cli。
- %R - Request URI 請求URI,如/app/user/signin; Cli模式下為入口文件,如CliIndex.php。
- %m - Request Method 請求類型,如GET; Cli模式下為執行命令,如/bin/bash。
- %I - Client IP 來源客戶端IP; Cli模式下為local。取值優先級為:HTTP_X_REAL_IP > HTTP_X_FORWARDED_FOR > REMOTE_ADDR
- %F - FileName:LineNo 文件名:行號,如UserService.php:118。
- %C - TODO Class::Action 類名::方法名,如UserService::getUserInfo。
使用
常量與函數
常量列表
SeasLog 共將日志分成8個級別
SEASLOG_DEBUG
- "DEBUG" - debug信息、細粒度信息事件
SEASLOG_INFO
- "INFO" - 重要事件、強調應用程序的運行過程
SEASLOG_NOTICE
- "NOTICE" - 一般重要性事件、執行過程中較INFO級別更為重要的信息
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
*/
define('SEASLOG_ALL', 'ALL');
define('SEASLOG_DEBUG', 'DEBUG');
define('SEASLOG_INFO', 'INFO');
define('SEASLOG_NOTICE', 'NOTICE');
define('SEASLOG_WARNING', 'WARNING');
define('SEASLOG_ERROR', 'ERROR');
define('SEASLOG_CRITICAL', 'CRITICAL');
define('SEASLOG_ALERT', 'ALERT');
define('SEASLOG_EMERGENCY', 'EMERGENCY');
define('SEASLOG_DETAIL_ORDER_ASC', 1);
define('SEASLOG_DETAIL_ORDER_DESC', 2);
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 string
* @return bool
*/
static public function setRequestID($request_id){
return TRUE;
}
/**
* 獲取本次請求標識
* @return string
*/
static public function getRequestID(){
return uniqid();
}
/**
* 設置模塊目錄
* @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 默認為正序 SEASLOG_DETAIL_ORDER_ASC,可選倒序 SEASLOG_DETAIL_ORDER_DESC
*
* @return array
*/
static public function analyzerDetail($level = SEASLOG_INFO, $log_path = '*', $key_word = NULL, $start = 1, $limit = 20, $order = SEASLOG_DETAIL_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 = '')
{
}
}
PHP Re 結果
/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.default_template <ALL> ]
Current = '%L | %P | %Q | %t | %T | %M'
}
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'
}
Entry [ seaslog.trim_wrap <ALL> ]
Current = '0'
}
Entry [ seaslog.throw_exception <ALL> ]
Current = '1'
}
Entry [ seaslog.ignore_warning <ALL> ]
Current = '1'
}
}
- Constants [16] {
Constant [ string SEASLOG_VERSION ] { 1.7.5 }
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 setRequestID ] {
- Parameters [1] {
Parameter #0 [ <required> $request_id ]
}
}
Method [ <internal:SeasLog> static public method getRequestID ] {
}
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 Logger的使用
獲取與設置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::setBasePath() 函數,將改變 SeasLog::getBasePath() 的取值。
設置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 / 20140218.INFO.log
- warnLogFile = basePath / logger / 20140218.WARNING.log
- erroLogFile = basePath / logger / 20140218.ERROR.log
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'));
/*
這些函數同時也接受第3個參數為logger的設置項
注意,當last_logger == 'default'時等同于:
SeasLog::setLogger('test/new/path');
SeasLog::error('test error 3');
如果已經在前文使用過SeasLog::setLogger()函數,第3個參數的log只在此處臨時使用,不影響下文。
*/
log格式受
seaslog.default_template
影響。 seaslog.default_template默認模板為 seaslog.default_template = "%T | %L | %P | %Q | %t | %M" 那么在默認情況下,日志格式為:
{dateTime} | {level} | {pid} | {uniqid} | {timeStamp} | {logInfo}
關于自定義模板,及SeasLog中的預置值,可參閱
自定義日志模板
2014-07-27 08:53:52 | ERROR | 23625 | 599159975a9ff | 1406422432.786 | this is a error test by log
2014-07-27 08:53:52 | DEBUG | 23625 | 599159975a9ff | 1406422432.786 | this is a neeke debug
2014-07-27 08:53:52 | INFO | 23625 | 599159975a9ff | 1406422432.787 | this is a info log
2014-07-27 08:53:52 | NOTICE | 23625 | 599159975a9ff | 1406422432.787 | this is a notice log
2014-07-27 08:53:52 | WARNING | 23625 | 599159975a9ff | 1406422432.787 | your github.com was down,please rboot it ASAP!
2014-07-27 08:53:52 | ERROR | 23625 | 599159975a9ff | 1406422432.787 | a error log
2014-07-27 08:53:52 | CRITICAL | 23625 | 599159975a9ff | 1406422432.787 | some thing was critical
2014-07-27 08:53:52 | EMERGENCY | 23625 | 599159975a9ff | 1406422432.787 | Just now, the house next door was completely burnt out! it is a joke
當seaslog.appender配置為 2(TCP) 或 3(UDP) 時,日志將推送至remote_host:remote_port的TCP或UDP端口
SeasLog發送至遠端時,遵循規范RFC5424
log格式統一為:
<PRI>1 {timeStampWithRFC3339} {HostName} {loggerName}[{pid}]: {logInfo}
上述{logInfo}
受配置 seaslog.default_template影響。
發送出去的格式如:
<15>1 2017-08-27T01:24:59+08:00 vagrant-ubuntu-trusty test/logger[27171]: 2016-06-25 00:59:43 | DEBUG | 21423 | 599157af4e937 | 1466787583.322 | this is a neeke debug
<14>1 2017-08-27T01:24:59+08:00 vagrant-ubuntu-trusty test/logger[27171]: 2016-06-25 00:59:43 | INFO | 21423 | 599157af4e937 | 1466787583.323 | this is a info log
<13>1 2017-08-27T01:24:59+08:00 vagrant-ubuntu-trusty test/logger[27171]: 2016-06-25 00:59:43 | NOTICE | 21423 | 599157af4e937 | 1466787583.324 | this is a notice log
SeasLog Analyzer的使用
快速統計某類型log的count值
SeasLog在擴展中使用管道調用shell命令 grep -wc快速地取得count值,并返回值(array || int)給PHP。
$countResult_1 = SeasLog::analyzerCount();
$countResult_2 = SeasLog::analyzerCount(SEASLOG_WARNING);
$countResult_3 = SeasLog::analyzerCount(SEASLOG_ERROR,date('Ymd',time()));
var_dump($countResult_1,$countResult_2,$countResult_3);
/*
array(8) {
["DEBUG"]=>
int(3)
["INFO"]=>
int(3)
["NOTICE"]=>
int(3)
["WARNING"]=>
int(3)
["ERROR"]=>
int(6)
["CRITICAL"]=>
int(3)
["ALERT"]=>
int(3)
["EMERGENCY"]=>
int(3)
}
int(7)
int(1)
*/
獲取某類型log列表
SeasLog在擴展中使用管道調用shell命令 grep -w快速地取得列表,并返回array給PHP。
$detailErrorArray_inAll = SeasLog::analyzerDetail(SEASLOG_ERROR);
$detailErrorArray_today = SeasLog::analyzerDetail(SEASLOG_ERROR,date('Ymd',time()));
var_dump($detailErrorArray_inAll,$detailErrorArray_today);
/*
SeasLog::analyzerDetail(SEASLOG_ERROR) == SeasLog::analyzerDetail(SEASLOG_ERROR,'*');
取當前模塊下所有level為 SEASLOG_ERROR 的信息列表:
array(6) {
[0] =>
string(66) "2014-02-24 00:14:02 | ERROR | 8568 | 599157af4e937 | 1393172042.717 | test error 3 "
[1] =>
string(66) "2014-02-24 00:14:04 | ERROR | 8594 | 5991576584446 | 1393172044.104 | test error 3 "
[2] =>
string(66) "2014-02-24 00:14:04 | ERROR | 8620 | 1502697015147 | 1393172044.862 | test error 3 "
[3] =>
string(66) "2014-02-24 00:14:05 | ERROR | 8646 | 599159975a9ff | 1393172045.989 | test error 3 "
[4] =>
string(66) "2014-02-24 00:14:07 | ERROR | 8672 | 599159986ec28 | 1393172047.882 | test error 3 "
[5] =>
string(66) "2014-02-24 00:14:08 | ERROR | 8698 | 5991599981cec | 1393172048.736 | test error 3 "
}
SeasLog::analyzerDetail(SEASLOG_ERROR,date('Ymd',time()));
只取得當前模塊下,當前一天內,level為SEASLOG_ERROR 的信息列表:
array(2) {
[0] =>
string(66) "2014-02-24 00:14:02 | ERROR | 8568 | 599157af4e937 | 1393172042.717 | test error 3 "
[1] =>
string(66) "2014-02-24 00:14:04 | ERROR | 8594 | 5991576584446 | 1393172044.104 | test error 3 "
}
同理,取當月
$detailErrorArray_mouth = SeasLog::analyzerDetail(SEASLOG_ERROR,date('Ym',time()));
*/
使用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