2024 年 PHP 日本研討會

範例

範例 #1 取得和設定基底路徑

<?php
$basePath1
= SeasLog::getBasePath();

SeasLog::setBasePath('/log/base_test');
$basePath2 = SeasLog::getBasePath();

var_dump($basePath1,$basePath2);

?>

以上範例將輸出類似以下的內容

string(12) "/var/log/www"
string(14) "/log/base_test"

範例 #2 取得和設定記錄器

<?php
$lastLogger1
= SeasLog::getLastLogger();

SeasLog::setLogger('testModule/app1');
$lastLogger2 = SeasLog::getLastLogger();

var_dump($lastLogger1,$lastLogger2);

?>

以上範例將輸出類似以下的內容

string(7) "default"
string(15) "testModule/app1"

範例 #3 快速寫入日誌

<?php
SeasLog
::log(SEASLOG_ERROR,'這是一個透過 ::log 測試的錯誤');
SeasLog::debug('這是一個 {userName} 除錯訊息',array('{userName}' => 'neeke'));
SeasLog::info('這是一條資訊日誌');
SeasLog::notice('這是一條通知日誌');
SeasLog::warning('您的 {website} 宕機了,請盡快 {action}!',array('{website}' => 'github.com','{action}' => '重新啟動'));
SeasLog::error('一條錯誤日誌');
SeasLog::critical('發生了嚴重錯誤');
SeasLog::alert('是的,這是一個 {messageName}',array('{messageName}' => '警報訊息'));
SeasLog::emergency('就在剛才,隔壁房子燒光了!{note}',array('{note}' => '開個玩笑'));
?>

預設情況下,seaslog.default_template = "%T | %L | %P | %Q | %t | %M"。這表示預設的日誌記錄格式為:`{日期時間} | {級別} | {行程ID} | {唯一ID} | {時間戳記} | {日誌訊息}`。

以上範例將輸出類似以下的內容

seaslog.appender = 1

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 = 2seaslog.appender = 3

The log style finally formatted such as:
<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

範例 #4 快速計算某些類型的日誌計數值

SeasLog 使用系統管線取得 `grep -wc` 的計數值並回傳給 PHP (陣列或整數)。

<?php
$countResult1
= SeasLog::analyzerCount();
$countResult2 = SeasLog::analyzerCount(SEASLOG_WARNING);
$countResult3 = SeasLog::analyzerCount(SEASLOG_ERROR,date('Ymd',time()));

var_dump($countResult1,$countResult2,$countResult3);

?>

以上範例將輸出類似以下的內容

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)

範例 #5 取得某些類型的日誌列表

SeasLog 使用系統管線取得 `grep -w` 的值並以陣列形式回傳給 PHP。

<?php
$detailErrorArray
= SeasLog::analyzerDetail(SEASLOG_ERROR);
var_dump($detailErrorArray);

var_dump(SeasLog::analyzerDetail(SEASLOG_ERROR,date('Ymd',time())));
?>

以上範例將輸出類似以下的內容

array(6) {
  [0] =>
  string(83) "2014-02-24 00:14:02 | ERROR | 8568 | 599157af4e937 | 1393172042.717 | test error 3 "
  [1] =>
  string(83) "2014-02-24 00:14:04 | ERROR | 8594 | 5991576584446 | 1393172044.104 | test error 3 "
  [2] =>
  string(83) "2014-02-24 00:14:04 | ERROR | 8620 | 1502697015147 | 1393172044.862 | test error 3 "
  [3] =>
  string(83) "2014-02-24 00:14:05 | ERROR | 8646 | 599159975a9ff | 1393172045.989 | test error 3 "
  [4] =>
  string(83) "2014-02-24 00:14:07 | ERROR | 8672 | 599159986ec28 | 1393172047.882 | test error 3 "
  [5] =>
  string(83) "2014-02-24 00:14:08 | ERROR | 8698 | 5991599981cec | 1393172048.736 | test error 3 "
}

array(2) {
  [0] =>
  string(83) "2014-02-24 00:14:02 | ERROR | 8568 | 599157af4e937 | 1393172042.717 | test error 3 "
  [1] =>
  string(83) "2014-02-24 00:14:04 | ERROR | 8594 | 5991576584446 | 1393172044.104 | test error 3 "
}
新增註記

使用者貢獻的註記

此頁面沒有使用者貢獻的註記。
To Top