PHP code example of hehex / hehep-hlogger
1. Go to this page and download the library: Download hehex/hehep-hlogger library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hehex / hehep-hlogger example snippets
$conf = [
'defaultLogger'=>"hehe",
// 预定义上下文
'contexts'=>[
'default'=>[
'class'=>'hehe\core\hlogger\contexts\TraceContext',
'skipClasses'=>[],// 跳过的类名,
'skipFuns'=>[],// 跳过的函数名,
]
],
// 预定义日志过滤器
'filters'=>[
'info'=>[
//'class'=>'',// 过滤器类名,未填则默认为LogFilter
'levels'=>['info'],
'categorys'=>['admin\controller*']
]
],
// 预定义日志格式器
'formatters'=>[
'default'=>[
//'class'=>'',// 消息格式器类名,未填则默认为LogFormatter
'tpl'=>'{date:Y-m-d:H:i} :{msg},file:{file}, line:{line},{class}->{fn} {n}',
]
],
// 预定义日志处理器
'handlers'=>[
'default'=>[
'class'=>'FileHandler',
'logFile'=>'/home/hehe/www/logs/hehep.log',
'formatter'=>'default'
]
],
// 预定义日志记录器
'loggers'=>[
'hehe'=>[
'bufferLimit'=>0,// 缓冲日志数量
'handlers'=>['default'],
'levels'=>['info','error','warning','exception','debug'],// 设置filter 后,此设置项将无效
'categorys'=>['admin\controller\*'],// 设置filter 后,此设置项将无效
'filter'=>'info',// 定义过滤器
'formatter'=>'default',// 定义日志格式器
'context'=>['default'],// 定义上下文
],
]
];
use hehe\core\hlogger\LogManager;
use hehe\core\hlogger\Log;
// 创建日志管理器
$hlogger = new LogManager([]);
// 记录日志
$hlogger->info('info log message');
$hlogger->error('error log message');
// 设置默认"default"处理器参数
Log::setHandler('default',['logFile'=>'/home/hehe/www/logs/hehep.log']);
Log::info('info log message');
Log::error('error log message');
use hehe\core\hlogger\LogManager;
use hehe\core\hlogger\Log;
// 创建日志管理器
$hlog = new LogManager([]);
// 默认日志记录器记录日志
$hlog->info('info log message');
$hlog->error('error log message');
// 获取日志记录器hehe,单例对象
$heheLogger = $hlog->getLogger('hehe');
// 创建新日志记录器
$heheLogger = $hlog->newLogger('hehe');
// 创建日志过滤器
$levelFilter = $hlog->levelFilter('error,info');
$heheLogger->addFilter($levelFilter);
// 创建日志处理器
$fileHandler = $hlog->fileHandler('/home/hehe/www/logs/hehep.log');
$heheLogger->addHandler($fileHandler);
// 创建日志格式器
$lineFormatter = $hlog->lineFormatter('{date:Y-m-d:H:i},{level},{userid} :{msg} ,file:{file}, line:{line},{class}->{fn}{n}');
$heheLogger->setFormatter($lineFormatter);
// 新增上下文变量
$heheLogger->addContext(function(){
return [
'userid'=>1
];
});
// 写入日志
$heheLogger->info('info log message');
$heheLogger->error('error log message');
use hehe\core\hlogger\LogManager;
use hehe\core\hlogger\Log;
// 创建日志管理器
$hlog = new LogManager([]);
// 设置默认"default"处理器参数
$hlog->setHandler('default',['logFile'=>'/home/hehe/www/logs/xxxx.log']);
// 设置名称为“hehe”格式器
$hlog->setFormatter('hehe',[
'class'=>'lineFormatter',
'tpl'=>'{date:Y-m-d:H:i} :{msg},file:{file}, line:{line},{class}->{fn} {cate} {n}'
]);
// 设置名称为“hehe”日志过滤器
$hlog->setFilter('hehe',[
'levels'=>'error,info',
'categorys'=>'admin\controller*',
]);
// 设置名称为“hehe”日志处理器
$hlog->setHandler('hehe',[
'class'=>'FileHandler',
'logFile'=>'/home/hehe/www/logs/xxxx.log',
'formatter'=>'hehe',
//'filter'=>'hehe'
]);
// 设置名称为“hehe”日志记录器
$hlog->setLogger('hehe',[
'handlers'=>'hehe',
'filters'=>'hehe',
'levels'=>'error,info',
]);
// 获取预定义hehe日志记录器单例对象
$heheLogger = $hlog->getLogger('hehe');
// 获取预定义hehe日志记录器新对象
$heheLogger = $hlog->newLogger('hehe');
// 写入日志
$heheLogger->error('error log message');
use hehe\core\hlogger\LogManager;
$hlog = new LogManager([]);
// 获取预定义hehe日志记录器单例对象
$heheLogger = $hlog->getLogger('hehe');
// 获取预定义hehe日志记录器新对象
$heheLogger = $hlog->newLogger('hehe');
// 获取一个空的日志记录器对象
$heheLogger = $hlog->newLogger();
$heheLogger = $hlog->newLogger([
'bufferLimit'=>2,
'levels'=>'error,info',
'categorys'=>'',// 'admin\controller*'
'filters'=>'default,hehe',
'handlers'=>'default,hehe',
]);
// 记录器新增日志过滤器
$filter = $hlog->levelFilter('error,info');
$heheLogger->addFilter($filter);
// 记录器新增日志处理器
$handler = $hlog->fileHandler('user/xxx.log');
$heheLogger->addHandler($handler);
// 设置记录器日志格式器
$hlogger->setFormatter($hlog->lineFormatter('{date:Y-m-d:H:i},{level},:{msg} ,file:{file}, line:{line},{class}->{fun}{n}'));
// 记录日志
$heheLogger->info('info log message');
use hehe\core\hlogger\base\Message;
$msg = new Message();
// 获取原始日志字符串
$msg->getMsg();
// 获取日志经过格式化处理后的日志字符串
$msg->getMessage();
// 获取日志级别
$msg->getLevel();
// 获取上下文对象
$context = $msg->getContext();
// 获取上下文对象值
$pid = $context->getValue('pid');
// 增加上下文对象值
$context->addValue('username','hehe');
$context->addValue('userid',function(){
// 获取用户id
});
// $context->addValue('userid',['类对象','对象方法名']);
// 获取用户传入的额外参数,如Log::error("error message",['id'=>1])
$context->getExtra();
use hehe\core\hlogger\LogManager;
$conf = [
// 预定义日志处理器定义
'handlers'=>[
// 日志文件处理器
'default'=>[
'class'=>'FileHandler',
'logFile'=>'/home/hehe/www/logs/hehep.log',// 日志文件
'filter'=>'',// 日志过滤器
'formatter'=>'default'// 日志格式器
],
'default1'=>[
'class'=>'FileHandler',
'logFile'=>'/home/hehe/www/logs/hehep.log',// 日志文件
'filter'=>[// 日志过滤器
'levels'=>['error'],
'categorys'=>['admin\controller*']
],
'formatter'=>[// 日志格式器
'tpl'=>'{date:Y-m-d:H:i} :{msg} ,file:{file}, line:{line},{class}->{fun} {n}',
]
]
],
// 预定义其他配置
];
namespace hehe\core\hlogger\handlers;
use hehe\core\hlogger\base\LogHandler;
use hehe\core\hlogger\base\Message;
class FileHandler extends LogHandler
{
// 构造函数
public function __construct(string $logFile = '',array $propertys = [])
{
$this->logFile = $logFile;
parent::__construct($propertys);
}
// 同时处理多条日志消息
public function handleMessages(array $messages)
{
foreach ($messages as $message) {
$this->handleMessage($message);
}
}
// 处理日志消息
public function handleMessage(Message $message):void
{
// 获取格式化后消息
$log_format_msg = $message->getMessage();
// 获取原始日志消息
$raw_msg = $message->getMsg();
// 获取日志上下文对象
$context = $message->getContext();
// 获取当前进程ID
$pid = $context->getValue('pid');
// 增加上下文对象值
$context->addValue('username','hehe');
$context->addValue('userid',function(){
// 获取用户id
});
// $context->addValue('userid',['类对象','对象方法名']);
// 获取用户传入的额外参数,如Log::error("error message",['id'=>1])
$context->getExtra();
}
}
use hehe\core\hlogger\LogManager;
$hlog = new LogManager([]);
$logger = $hlog->newLogger();
// 创建预定义“default1”日志处理器
$handler = $hlog->newHandler('default1');
// 创建空的日志处理器对象
$handler = $hlog->newHandler();
$handler->addFilter($hlog->levelFilter('error,info'));
$handler->setFormatter($hlog->lineFormatter('{date:Y-m-d:H:i} :{msg} ,file:{file}, line:{line},{class}->{fn} {n}'));
$logger->addHandler($handler);
// 记录日志
$logger->error('error log message');
use hehe\core\hlogger\LogManager;
$hlog = new LogManager();
$logger = $hlog->newLogger();
// 创建处理器, 默认日志文件为/home/hehe/www/logs/hehep.log,
$handler = $hlog->fileHandler('/home/hehe/www/logs/hehep.log');
$handler = $hlog->fileHandler([
'logFile'=>'/home/hehe/www/logs/hehep.log',
'useLock'=>false,// 是否使用文件锁(flock),默认为false
]);
$logger->addHandler($handler);
$logger->info('info log message');
use hehe\core\hlogger\LogManager;
$hlog = new LogManager();
$logger = $hlog->newLogger();
// 创建处理器, 默认日志文件为/home/hehe/www/logs/hehep.log, 日志文件大小为5M
$handler = $hlog->byteRotatingFileHandler('/home/hehe/www/logs/hehep.log',1024 * 5);
$handler = $hlog->byteRotatingFileHandler([
'logFile'=>'/home/hehe/www/logs/hehep.log',
// 最大文件容量,单kb,5M
'maxByte'=>1024 * 5,
// filename:当前日志文件名 ,date:为当前日期,"YmdHi" 日期格式
'rotatefmt'=>'{filename}_{date:YmdHi}',
]);
$logger->addHandler($handler);
$logger->info('info log message');
use hehe\core\hlogger\LogManager;
$hlog = new LogManager();
$logger = $hlog->newLogger();
$timedRotatingFileHandler = $logger->timedRotatingFileHandler('/home/hehe/www/logs/hehep.log','s');
$timedRotatingFileHandler->setMaxFiles(2);
$timedRotatingFileHandler->setRotatefmt('{filename}_{date:YmdHi}_hehe');
// $timedRotatingFileHandler->setRotatefmt('{date:Ym/d/H/}{filename}_{date:YmdHi}_hehe'); 目录日期格式
$timedRotatingFileHandler->setMaxByte(10);
$timedRotatingFileHandler->setBackupCount(4);
$logger->addHandler($timedRotatingFileHandler);
$logger->setFormatter($logger->lineFormatter('{date:Y-m-d:H:i},{level},{msg} ,file:{file}, line:{line} {n}'));
$logger->error("default logger error message");
继承:hehe\core\hlogger\handlers\RotatingFileHandler;
属性:
'logFile'=>'',// 日志文件
'rotating'=>null,// 轮转状态,true 表示正在轮转中(日志文件未生成)
'maxFiles'=>0,// 最大文件数量,默认为0,表示不限制
'rotatefmt'=>'',// 轮转文件格式,变量可以取自日志上下文,
'rotatefmtParams'=>[],// 轮转文件格式参数,可设置变量的正则表达式
'backupCount'=>0,// 最大备份文件数量,默认为0,表示不限制
'backupfmt'=>'',// 备份文件格式,变量可以取自日志上下文,
'backupfmtParams'=>[],// 备份文件格式参数,可设置变量的正则表达式
class TimedRotatingFileHandler extends RotatingFileHandler
{
// 重写rotate 方法即可
public function rotate(Message $message):void
{
// 第一次轮转,初始化轮转时间,轮转文件
if ($this->rotateFile === '') {
// 首次轮转文件
$this->rotateFile($message);
}
// 轮转条件
if ($message->getDataTime() > $this->endDate) {
$this->rotating = true;
// 轮转文件
$this->rotateFile($message);
}
// 备份轮转文件
if ($this->rotating === false && $this->maxByte > 0) {
clearstatcache();
if (@filesize($this->rotateFile) > ($this->maxByte * 1024)) {
// 备份轮转文件
$this->backupFile($message);
}
}
return ;
}
}
use hehe\core\hlogger\LogManager;
$conf = [
// 预定日志过滤器
'filters'=>[
'default'=>[
//'class'=>'',// 过滤器类名,未填则默认为LevelFilter
'levels'=>['info','error'],// 过滤器支持的级别
'categorys'=>['admin\controller*'],// 过滤器支持分类
],
'default1'=>[
'class'=>'LevelFilter',// 过滤器类名,未填则默认为LevelFilter
'levels'=>['info','error'],// 过滤器支持的级别
'categorys'=>['admin\controller*'],// 过滤器支持分类
],
],
];
namespace hehe\core\hlogger\filters;
use hehe\core\hlogger\base\LogFilter;
use hehe\core\hlogger\base\Message;
use hehe\core\hlogger\Utils;
class LevelFilter extends LogFilter
{
public function __construct(string $levels = '',array $propertys = [])
{
parent::__construct($propertys);
}
// 检查消息是否满足条件
public function check(Message $message):bool
{
// @todo 实现自己的过滤规则
}
}
use hehe\core\hlogger\LogManager;
$hlog = new LogManager([]);
$logger = $hlog->newLogger();
// 创建预定义“default1”日志处理器
$defaultFilter = $hlog->newFilter('default1');
$logger->addFilter($defaultFilter);
// 创建空的日志处理器对象
$emptyFilter = $hlog->newFilter();
$emptyFilter->setLevels('error,info');
$emptyFilter->setCategorys(['admin\controller*']);
$logger->addFilter($emptyFilter);
// 创建level过滤器对象
$levelFilter = $hlog->levelFilter('error,info',['admin\controller*']);
$logger->addFilter($levelFilter);
$logger->addHandler($hlog->fileHandler('/home/hehe/www/logs/hehep.log',1024 * 5));
// 记录日志
$logger->error('error log message');
$conf = [
// 日志格式器定义
'formatters'=>[
'default'=>[
//'class'=>'',// 消息格式器类名,未填则默认为LineFormatter
'tpl'=>'{date:Y-m-d:H:i} :{msg},file:{file}, line:{line},{class}->{fn} {n}',
]
],
];
namespace hehe\core\hlogger\formatters;
use hehe\core\hlogger\base\LogFormatter;
use hehe\core\hlogger\base\Message;
use hehe\core\hlogger\Utils;
class LineFormatter extends LogFormatter
{
public function __construct(string $tpl = '',array $propertys = [])
{
$this->tpl = $tpl;
parent::__construct($propertys);
}
// 解析消息
public function parse(Message $message)
{
// 获取上下文对象
$context = $message->getContext();
// 获取上下文值
$ip = $context->getValue('ip');
$class = $context->getValue('class');
$msg = $context->getValue('msg');
$date = $context->getValue('date',['Y-m-d:H:i']);
}
}
use hehe\core\hlogger\LogManager;
$hlog = new LogManager([]);
$logger = $hlog->newLogger();
// 创建预定义“default1”日志处理器
$defaultFormatter = $hlog->newFormatter('default1');
$logger->setFormatter($defaultFormatter);
// 创建空的日志处理器对象
$emptyFormatter = $hlog->newFormatter();
$emptyFormatter->setTpl('{date:Y-m-d:H:i} :{msg},file:{file}, line:{line},{class}->{fn} {n}');
$logger->setFormatter($emptyFormatter);
// 创建level过滤器对象
$lineFormatter = $hlog->lineFormatter('{date:Y-m-d:H:i} :{msg} ,file:{file}, line:{line},{class}->{fn} {n}');
$logger->setFormatter($lineFormatter);
$logger->addHandler($hlog->fileHandler('/home/hehe/www/logs/hehep.log',1024 * 5));
// 记录日志
$logger->error('error log message');
use hehe\core\hlogger\LogManager;
$hlog = new LogManager();
$logger = $hlog->newLogger();
// 创建单行字符串日志格式器
$lineFormatter = $hlog->lineFormatter('{date:Y-m-d:H:i} :{msg} ,file:{file}, line:{line},{class}->{fn} {n}');
// 创建预定义“default”日志格式器
$lineFormatter = $hlog->newFormatter('default');
$logger->setFormatter($lineFormatter);
$conf = [
'contexts'=>[
'default'=>[
'class'=>'hehe\core\hlogger\contexts\TraceContext',
'skipClasses'=>[],// 跳过的类名,
'skipFuns'=>[],// 跳过的函数名,
]
],
];
namespace hehe\core\hlogger\contexts;
use hehe\core\hlogger\base\LogContext;
class LineContext extends LogContext
{
public function handle():array
{
return [
'user'=>'admin',
'ip'=>'127.0.0.1'
];
}
}
use hehe\core\hlogger\LogManager;
use \hehe\core\hlogger\contexts\TraceContext;
$hlog = new LogManager();
$logger = $hlog->newLogger();
// 创建单行字符串日志格式器
$lineFormatter = $hlog->lineFormatter('{date:Y-m-d:H:i},{msg} {user}:{ip},file:{file}, line:{line},{class}->{fn} {n}');
$logger->setFormatter($lineFormatter);
// 创建预定义“default”日志上下文
$defaultContext = $hlog->newContext('default');
// 快速创建traceContext日志上下文
$traceContext = $hlog->traceContext(["hlogger"]);
$logger->addContext($traceContext);
// new TraceContext 上下文
$traceContext = new \hehe\core\hlogger\contexts\TraceContext();
$logger->addContext($traceContext);
$logger->addContext(TraceContext::class);
// 添加闭包上下文
$logger->addContext(function(){
return [
'user'=>'admin',
'ip'=>'127.0.0.1'
];
});
// 数组上下文
$logger->error('error log message',['goodid'=>'123']);
类名:hehe\core\hlogger\base\Logger
作用:用于记录日志,可以设置日志级别,日志过滤器,日志处理器,
属性:
'bufferLimit'=>0,// 缓冲日志数量
'handlers'=>['default'],// 日志处理器
'levels'=>['info','error','warning','debug'],// 设置允许的消息级别
'categorys'=>['admin\controller*'],// 设置允许的日志类别
'filter'=>'info',// 定义过滤器
'formatter'=>'default',// 定义日志格式器
基类:hehe\core\hlogger\base\LogFilter
作用:过滤日志消息,比如只记录error级别日志,或者只记录"admin\controller*"控制器的日志
属性:
'levels'=>'info,error',// 支持的日志级别
'categorys'=>['admin\controller*'],// 支持的日志分类