简介:SeasLog是一个C语言编写的PHP扩展,提供一组规范标准的功能函数,在PHP项目中方便、规范、高效地写日志,以及快速地读取和查询日志。今天就给大家总结下PHP扩展高性能日志系统SeasLog简单上手使用教程。

Seaslog:An effective,fast,stable log extension for PHP

官网地址:https://neeke.github.io/SeasLog/

github地址:https://github.com/Neeke/SeasLog

为什么使用SeasLog?

1.高性能

SeasLog是用C语言写的,并且它自带一个缓冲池的功能,我们每一次产生的日志,是首先写到内存当中的,当达到一定量时,才一次性写到文件当中,这样比我们频繁写入文件中的性能高很多

2.无需复杂的配置

 3.功能完善,使用简单

日志系统最好有以下几点功能:除了读写日志以外;还希望可以分模块:比如网站分前台后台两个大的模块,写到不同的文件里去;并且日志系统有统计、分析的功能。

如何安装SeasLog?

Linux系统安装SeasLog

方式一:编译安装 SeasLog:

pecl安装包地址:https://pecl.php.net/package/seaslog

选择你喜欢的版本,我这里演示的是最新版本的安装方法:

# 下载:
wget https://pecl.php.net/get/SeasLog-2.2.0.tgz

# 安装:
tar -zxvf SeasLog-2.2.0.tgz
cd SeasLog-2.2.0

# 这里是你php的安装目录,如果环境内只安装一个php版本,可以直接 ./phpize
/www/server/php/56/bin/phpize
./configure --with-php-config=/www/server/php/56/bin/php-config
make && make install

然后去php.ini加入配置:(具体看自己的项目需求)

[SeasLog]
;configuration for php SeasLog module
extension = seaslog.so

;Default Log Base Path
seaslog.default_basepath = "/www/wwwlogs/seaslog"

;Default Logger Path
seaslog.default_logger = "default"

;The DateTime Style.  Default "Y-m-d H:i:s"
seaslog.default_datetime_format = "Y-m-d H:i:s"

;Default Log template. 
;Default "%T | %L | %P | %Q | %t | %M"
seaslog.default_template = "%T | %L | %P | %Q | %t | %M"

;Switch use the logger with type.
;1-Y 0-N(Default)
seaslog.disting_type = 0

;Switch use the logger with hour.
;1-Y 0-N(Default)
seaslog.disting_by_hour = 0

;Switch use the log buffer with memory.
;1-Y 0-N(Default)
seaslog.use_buffer = 0

;The buffer size
seaslog.buffer_size = 100

;Record logger level. 
;0-EMERGENCY 1-ALERT 2-CRITICAL 3-ERROR 4-WARNING 5-NOTICE 6-INFO 7-DEBUG 8-ALL
;Default 8 (All of them).
;
;   Tips: The configuration item has changed since the 1.7.0 version.
;   Before the 1.7.0 version, the smaller the value, the more logs are taken according to the level: 
;   0-all 1-debug 2-info 3-notice 4-warning 5-error 6-critical 7-alert 8-emergency
;   Before the 1.7.0 version, Default 0 (All of them).
seaslog.level = 8

;Automatic Record final error with default logger. 
;1-Y(Default) 0-N
seaslog.trace_error = 1

;Automatic Record exception with default logger. 
;1-Y 0-N(Default)
seaslog.trace_exception = 0

;Switch the Record Log Data Store.     
;1File 2TCP 3UDP (Switch default 1)
seaslog.appender = 1

;If you use  Record TCP or UDP, configure this remote ip.
;Default "127.0.0.1"
seaslog.remote_host = "127.0.0.1"

;If you use Record TCP or UDP, configure this remote port.
;Default 514
seaslog.remote_port = 514

;Trim the \n and \r in log message.
;1-On 0-Off(Default)
seaslog.trim_wrap = 0

;Switch throw SeasLog exception.
;1-On(Default) 0-Off
seaslog.throw_exception = 1

;Switch ignore SeasLog warning.
;1-On(Default) 0-Off
seaslog.ignore_warning = 1

最后记得重启服务

方式二:PECL安装SeasLog

pecl install seaslog

 

2.Window系统安装SeasLog:

dll下载地址:https://pecl.php.net/package/SeasLog/1.6.9/windows

根据您安装的PHP版本,是否线程安全 进行选择,这些都可以在phpinfo里查得到

下载之后 把里面的php_seaslog.dll拷贝到PHP的安装目录下的ext里

然后打开php.ini进行配置

extension=php_seaslog.dll

如何在项目中快速使用SeasLog?

简单使用代码:

//判断是否安装SeasLog扩展
if(extension_loaded('SeasLog')){
    //设置存储目录
    \SeasLog::setLogger('bbsnew'.DS.request()->module().DS.request()->controller());
    //记录日志
    \SeasLog::log(SEASLOG_INFO,json_encode($data));
}