运行时配置
这些函数的行为受php.ini中的设置影响。
| 名字 | 默认 | 可修改范围 | 更新日志 | 
|---|---|---|---|
| seaslog.appender | 1 | PHP_INI_SYSTEM | |
| seaslog.appender_retry | 0 | PHP_INI_ALL | |
| seaslog.level | 8 | PHP_INI_ALL | |
| seaslog.remote_host | 127.0.0.1 | PHP_INI_ALL | |
| seaslog.remote_port | 514 | PHP_INI_ALL | |
| seaslog.remote_timeout | 1 | PHP_INI_SYSTEM | |
| seaslog.default_basepath | /var/log/www | PHP_INI_SYSTEM | |
| seaslog.default_logger | default | PHP_INI_SYSTEM | |
| seaslog.default_template | %T | %L | %P | %Q | %t | %M | PHP_INI_SYSTEM | |
| seaslog.default_datetime_format | Y-m-d H:i:s | PHP_INI_SYSTEM | |
| seaslog.trace_error | 1 | PHP_INI_ALL | |
| seaslog.trace_exception | 0 | PHP_INI_SYSTEM | |
| seaslog.trace_notice | 0 | PHP_INI_ALL | |
| seaslog.trace_warning | 0 | PHP_INI_ALL | |
| seaslog.use_buffer | 0 | PHP_INI_SYSTEM | |
| seaslog.buffer_size | 0 | PHP_INI_ALL | |
| seaslog.buffer_disabled_in_cli | 0 | PHP_INI_SYSTEM | |
| seaslog.disting_type | 0 | PHP_INI_SYSTEM | |
| seaslog.disting_folder | 1 | PHP_INI_SYSTEM | |
| seaslog.disting_by_hour | 0 | PHP_INI_SYSTEM | |
| seaslog.recall_depth | 0 | PHP_INI_ALL | |
| seaslog.trim_wrap | 0 | PHP_INI_ALL | |
| seaslog.ignore_warning | 1 | PHP_INI_ALL | |
| seaslog.throw_exception | 1 | PHP_INI_ALL | 
这是配置指令的简短说明。
- seaslog.appenderinteger
- 日志存储介质的切换选型。1File 2TCP 3UDP (默认为1) - 当seaslog.appender被切换至 `2 (TCP)` 或者 `3 (UDP)` 时, SeasLog 会将日志发送至 tcp://remote_host:remote_port 或者 udp://remote_host:remote_port 服务器。 - 当SeasLog将日志发往 TCP/UDP 时,格式遵守 RFC5424 规范。 此时 `{logInfo}` 受配置项中seaslog.default_template的影响。 - 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
- seaslog.appender_retryinteger
- 记录日志时的重试次数。 默认为 0 (不重试) 
- seaslog.buffer_disabled_in_cliinteger
- 在 CLI 模式下是否关闭 Buffer 的选项。 1-Y 0-N(默认值) - 在配置中留有 buffer_disabled_in_cli 的开关项。 默认情况下,这个选项是关闭的。 如果将 buffer_disabled_in_cli 设为开启,并且运行在 CLI 时, 配置中的 seaslog.use_buffer 设置将是被忽略的,此时 Seaslog 将立即把日志信息写往存储介质。 
- seaslog.buffer_sizeinteger
- 可以通过该配置项将内存中 Buffer 的条数修改为 100 条。 配置项中 buffer_size 默认值为 0, 这意味着将不使用 Buffer。 如果 buffer_size > 0,SeasLog 将预先将日志写入内存 Buffer,并在 Buffer 的条数大于或等于该值时,写往存储介质,然后刷新内存中的 Buffer。 
- seaslog.default_basepathstring
- 日志存储的默认根路径。默认值为 "/var/log/www"。 
- seaslog.default_datetime_formatstring
- 时间的格式。默认值为 "Y-m-d H:i:s"。 
- seaslog.default_loggerstring
- 日志记录的默认 Logger。默认值为 "default"。 
- seaslog.disting_by_hourinteger
- 是否按每小时一个记录进行区分。1-Y 0-N(默认值) - Note: - 当seaslog.disting_by_hour = 1时,会将日志区分小时记录。 这意味着,SeasLog 将每隔一个小时创建一个文件。 
- seaslog.disting_folderinteger
- 是否按目录进行区分。1-Y(默认值) 0-N - Note: - 当seaslog.disting_folder = 1时,按目录区分地使用 Logger。 这意味着,SeasLog 将为每一个 Logger 创建一个单独的目录进行区分,比如 default/20180211.log, 而当该选项关闭时,SeasLog 将使用下划线连接 Logger 与时间,比如 default_20180211.log。 
- seaslog.disting_typeinteger
- 是否按日志级别进行区分。1-Y 0-N(默认值) - Note: - 当seaslog.disting_type = 1时,按日志级别使用 Logger。 这意味着,SeasLog 将在创建日志文件时,使用 info/warn/error 或其他级别进行区分。 
- seaslog.ignore_warninginteger
- 是否忽略 SeasLog 警告。1-On(默认值) 0-Off - Note: - 当seaslog.ignore_warning = 1时,忽略 SeasLog 自身的警告, 此时日志目录权限不足、或从远端 Server 端口不能正常响应等导致的警告,将被忽略; 而当关闭该选项时,警告将会出现。 
- seaslog.levelinteger
- 允许日志被记录的级别。默认为 8 (全部日志)。 0-EMERGENCY 1-ALERT 2-CRITICAL 3-ERROR 4-WARNING 5-NOTICE 6-INFO 7-DEBUG 8-ALL - Note: - 提示: 该配置项从 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.recall_depthinteger
- 日志函数所在的层级。这将影响预置变量中的行号取值 `%F`。 默认值为 0。 
- seaslog.remote_hoststring
- 如果要使用 TCP 或者 UDP 为存储介质,需要配置远端的 IP。默认值为 "127.0.0.1" 
- seaslog.remote_portinteger
- 如果要使用 TCP 或者 UDP 为存储介质,需要配置远端服务的端口号。默认值为 514 
- seaslog.remote_timeoutinteger
- 如果要使用 TCP 或者 UDP 为存储介质,需要配置超时时间。默认值为 1 秒。 
- seaslog.throw_exceptioninteger
- 是否接受 SeasLog 抛出异常。1-On(默认值) 0-Off - Note: - 当seaslog.throw_exception = 1时,接受 SeasLog 抛出自身的异常, 此时由于日志目录权限问题、或者从远端 Server 端口不能正常响应而导致的中断, 将抛出一个异常;而当关闭该选项时,将不抛出异常。 
- seaslog.trace_errorinteger
- 自动将 PHP 的 Final Error 记录在默认 Logger中。1-Y(默认值) 0-N 
- seaslog.trace_exceptioninteger
- 自动将 PHP 的异常记录在默认 Logger中。1-Y 0-N(默认值) 
- seaslog.trace_noticeinteger
- 自动将 PHP 的 Notice 记录在默认 Logger中。1-Y 0-N(默认值) 
- seaslog.trace_warninginteger
- 自动将 PHP 的 Warning 记录在默认 Logger中。1-Y 0-N(默认值) 
- seaslog.trim_wrapinteger
- 自动地 Trim 掉日志信息中的 \n 和 \r。1-On 0-Off(默认值) 
- seaslog.use_bufferinteger
- 开启使用内存中的日志 Buffer。1-Y 0-N(默认值) - Note: - 当seaslog.use_buffer = 1时,开启使用内存 Buffer。 默认情况下,内存 Buffer 是关闭的。 如果 Buffer 是开启状态,SeasLog 会将日志预先记录在内存中, 并且在请求结束时、或 PHP 进程结束时(PHP RSHUTGOWN 或 PHP MSHUTDOWN)时写往存储介质。 
- seaslog.default_templatestring
- 默认日志模板。 默认值是 "%T | %L | %P | %Q | %t | %M". - Note: - SeasLog 提供了一系列的默认变量,可以在日志模板中使用,并在最终日志生成时,这些变量的占位符会被替换成对应的值。 - 默认的日志模板是:`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}` Seaslog 内置变量表变量名描述%L日志级别。%M日志信息。%T时间。 比如:`2017-08-16 19:15:02`,受配置项 `seaslog.default_datetime_format` 的影响。%t时间戳。比如:`1502882102.862`,精确到毫秒。%Q 请求ID。用于区分每一个请求, 如果没有调用 `SeasLog::setRequestId($string)` 函数, 将在请求初始化的时候,使用 PHP 内置函数 `static char *get_uniqid ()` 来生成 Unique ID。 %H主机名。%P进程ID。%D域名:端口号。比如:`www.cloudwise.com:80`;在 CLI 下运行时,该值为 `cli`。%R 请求 URI。比如:`/app/user/signin`。 在 CLI 下运行时,值为 Index Script 名称,比如:`CliIndex.php`。 %m 请求 Method。比如:`Get`。 在 CLI 下运行时,值为 Command Script,比如:`/bin/bash`。 %I 客户端IP;在 CLI 下运行时,值为`local`。 取值优先级为:HTTP_X_REAL_IP > HTTP_X_FORWARDED_FOR > REMOTE_ADDR %F 文件名:行号。比如:`UserService.php:118`。 %U 内存使用量。单位为 byte。 调用 PHP 内置方法`zend_memory_usage`得到该值。 %u 最大内存使用峰值。单位为 byte。 调用 PHP 内置方法`zend_memory_peak_usage`得到该值。 %C `TODO` Class::Action. Such as `UserService::getUserInfo`
范例
Example #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"
Example #2 获取与设置 Logger
<?php
$lastLogger1 = SeasLog::getLastLogger();
SeasLog::setLogger('testModule/app1');
$lastLogger2 = SeasLog::getLastLogger();
var_dump($lastLogger1,$lastLogger2);
?>
以上例程的输出类似于:
string(7) "default" string(15) "testModule/app1"
Example #3 快速写入日志
<?php
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'));
?>
日志的默认模板是seaslog.default_template = "%T | %L | %P | %Q | %t | %M". 这意味着,在默认情况下,日志的记录格式是: `{dateTime} | {level} | {pid} | {uniqid} | {timeStamp} | {logInfo}`.
以上例程的输出类似于:
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 = 2orseaslog.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
Example #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)
Example #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 "
}
SeasLog类
(PECL seaslog >=1.0.0)
SeasLog
{
	/* 方法 */
	public static alert ( string $message [, array $content [, string $logger ]] ) : bool
	public static analyzerCount ( string $level [, string $log_path [, string $key_word ]] ) : mixed
	public static analyzerDetail ( string $level [, string $log_path [, string $key_word [, int $start [, int $limit [, int $order ]]]]] ) : mixed
	public static closeLoggerStream ( int $model , string $logger ) : bool
	public static critical ( string $message [, array $content [, string $logger ]] ) : bool
	public static debug ( string $message [, array $content [, string $logger ]] ) : bool
	public __destruct ( void )
	public static emergency ( string $message [, array $content [, string $logger ]] ) : bool
	public static error ( string $message [, array $content [, string $logger ]] ) : bool
	public static flushBuffer ( void ) : bool
	public static Seaslog::getBasePath ( void ) : string
	public static getBuffer ( void ) : array
	public static getBufferEnabled ( void ) : bool
	public static getDatetimeFormat ( void ) : string
	public static getLastLogger ( void ) : string
	public static getRequestID ( void ) : string
	public static getRequestVariable ( int $key ) : bool
	public static info ( string $message [, array $content [, string $logger ]] ) : bool
	public static log ( string $level [, string $message [, array $content [, string $logger ]]] ) : bool
	public static notice ( string $message [, array $content [, string $logger ]] ) : bool
	public static setBasePath ( string $base_path ) : bool
	public static setDatetimeFormat ( string $format ) : bool
	public static setLogger ( string $logger ) : bool
	public static setRequestID ( string $request_id ) : bool
	public static setRequestVariable ( int $key , string $value ) : bool
	public static warning ( string $message [, array $content [, string $logger ]] ) : bool
}
