Table of Contents
高效的日志缓冲、便捷的缓冲debug
自动记录错误信息
自动记录异常信息
目标是怎样的
安装
编译安装 SeasLog
PECL安装SeasLog
seaslog.ini 的配置
Home Backend Development PHP Tutorial SeasLog-1.5.3 发布,轻量高效的 PHP 日志扩展

SeasLog-1.5.3 发布,轻量高效的 PHP 日志扩展

Jun 23, 2016 pm 01:11 PM

SeasLog-1.5.3 发布,bug修复版本,轻量高效的PHP日志扩展。

改进日志:

- Fixed zval_ptr_dtor bug with PHP7. - Fixed issue #68 Fixed _ to . with getDetail function. - Fixed issue #73 Fixed segment fault with setBasePath function.

- Support Datetime Format Config.

为什么使用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
Copy after login

PECL安装SeasLog

$ pecl install seaslog
Copy after login

seaslog.ini 的配置

; configuration for php SeasLog moduleextension = seaslog.soseaslog.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.default_datetime_format = "%Y:%m:%d %H:%M:%S"   ;日期格式配置 默认"%Y:%m:%d %H:%M:%S"
Copy after login

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级别*/
Copy after login

函数列表

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';    }    /**     * 设置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     *     * @return array     */    static public function analyzerDetail($level = SEASLOG_INFO, $log_path = '*', $key_word = NULL, $start = 1, $limit = 20, $order = SEASLOG_DETIAL_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 = '')    {    }}
Copy after login

SeasLog Logger的使用(详细文档)

使用SeasLog进行健康预警

预警的配置

[base]wait_analyz_log_path = /log/base_test[fork];是否开启多线程 1开启 0关闭fork_open = 1;线程个数fork_count = 3[warning]email[smtp_host] = smtp.163.comemail[smtp_port] = 25email[subject_pre] = 预警邮件 -email[smtp_user] = seaslogdemo@163.comemail[smtp_pwd] = seaslog#demoemail[mail_from] = seaslogdemo@163.comemail[mail_to] = gaochitao@weiboyi.comemail[mail_cc] = ciogao@gmail.comemail[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/bbtest1[level] = SEASLOG_ERRORtest1[bar] = 1test1[mail_to] = gaochitao@weiboyi.comtest2[module] = 222test2[level] = SEASLOG_WARNINGtest3[module] = 333test3[level] = SEASLOG_CRITICALtest4[module] = 444test4[level] = SEASLOG_EMERGENCYtest5[module] = 555test5[level] = SEASLOG_DEBUG
Copy after login

crontab配置

;每天凌晨3点执行0 3 * * * /path/to/php /path/to/SeasLog/Analyzer/SeasLogAnalyzer.php
Copy after login

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";
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

See all articles