libfastcommon/doc/php_log_file_performance-Ch...

36 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# ⽇志⽂件优化PHP扩展函数
```
出于提升性能目的基于libfastcommon封装的php扩展提供了函数fastcommon_error_log
来替代PHP原⽣的error_log使⽤fastcommon_file_put_contents替换PHP原⽣的file_put_contents。
原理很简单就是⽇志⽂件打开后将其⽂件描述符或⽂件句柄持久化避免每次调⽤error_log
或file_put_contents时都执⾏open和close等⽂件操作。
在短字符串的场景下通过实测fastcommon_file_put_contents⽐file_put_contents性能提升2倍以上。
fastcommon_error_log⽐error_log性能提升50%以上。
两个扩展函数的⽤法和PHP原⽣函数⼀致。在可以优化的场景下由fastcommon扩展接管处理否则透传给PHP原⽣函数处理。
```
## 函数简要说明如下:
```
bool fastcommon_error_log (string $message [, int $message_type = 0, string
$destination, string $extra_headers] )
接管(优化处理)条件: $message_type为3且指定了$destination即⽇志⽂件名
在接管的情况下, $extra_headers可以为下列常量之⼀
FASTCOMMON_LOG_TIME_PRECISION_NONE⽇志⾏⾸不输出⽇期时间字符串默认值
FASTCOMMON_LOG_TIME_PRECISION_SECOND⽇志⾏⾸输出的时间精度到秒
FASTCOMMON_LOG_TIME_PRECISION_MSECOND⽇志⾏⾸输出的时间精度到毫秒
FASTCOMMON_LOG_TIME_PRECISION_USECOND⽇志⾏⾸输出的时间精度到微秒
注:如果$message最后没有换⾏符会⾃动增加。这和error_log的⾏为不⼀致。
```
```
int fastcommon_file_put_contents (string $filename , mixed $data [, int $flags = 0,
resource $context ])
接管优化处理条件需满⾜如下3个条件
* $data为字符串
* $flags 为FILE_APPEND或 (FILE_APPEND | LOCK_EX)
* $context 为null即没有指定$context
```