phalcon 自定义超强日志类
程序员文章站
2022-05-31 10:06:56
...
phalcon自带的日志文件类,有两个麻烦之处。
1、路径不能自动创建
2、打印消息非得要求是字符串。
自定义类如下:
<?php
use Phalcon\Logger\Adapter\File as FileAdapter;
use Phalcon\Logger\AdapterInterface;
/**
* 自定义 phalcon 日志类
*
* 解决两大痛点:
* 1、路径不能自动创建
* 2、打印消息非得要求是字符串。
*
* 使用方式
*
* $logger = SuperLogger::getInstance(SuperLogger::CLIENT_LOG);
* $logger
* ->info( 'This is a {message}',
* [
* 'message' => time()
* ] )
* ->log( time() )
* ->log(Logger::NOTICE, time() )
* ->error( ['aa'=>11111,'b'=>22222] );
*
* echo "读取日志:".$logger->getPath()."<br>";
* echo file_get_contents($logger->getPath());
*
* 也可以构造方法参数为空,再 select,或 setPath
*
* Class SuperLogger
*
* @method AdapterInterface setFormatter(FormatterInterface $formatter)
* @method FormatterInterface getFormatter()
* @method AdapterInterface setLogLevel($level)
* @method AdapterInterface log($type, $message = null, array $context = null)
* @method AdapterInterface debug($message, array $context = null)
* @method AdapterInterface error($message, array $context = null)
* @method AdapterInterface info($message, array $context = null)
* @method AdapterInterface notice($message, array $context = null)
* @method AdapterInterface warning($message, array $context = null)
* @method AdapterInterface alert($message, array $context = null)
* @method AdapterInterface emergency($message, array $context = null)
* @method AdapterInterface begin()
* @method AdapterInterface commit()
*
*
* @author yyy
*/
class SuperLogger
{
/**
* @var FileAdapter
*/
protected $logger;
protected $file; // 文件完整路径
protected $baseDir;// 预定义日志文件基路径
/**
* 这里自定义一些常量。日志类型。
*/
const HTTP_LOG = 1;
const CLIENT_LOG = 2;
const MENU_LOG = 3;
const PATH_ARR =[
self::HTTP_LOG => 'http/',
self::CLIENT_LOG => 'client/',
self::MENU_LOG => 'menu/',
];
/**
* 这里自定义默认日志根路径。
*
* @param mixed $file
*/
public function __construct( $file = null)
{
$this->baseDir = BASE_PATH . '/cache/logs/';
if ($file) {
if (is_int($file)) {
$this->select($file);
} else {
$this->setPath($file);
}
}
}
public static function getInstance( $file = null):self
{
return new self($file);
}
/**
* 选择不同的日志类型,自行修改。
*
* @param $type
* @return $this
*/
public function select(int $type=null):self
{
if (in_array( $type, array_keys( self::PATH_ARR) )) {
$this->setPath($this->baseDir . self::PATH_ARR[$type] . date("Ymd") . '.log');
} else {
$this->setPath($this->baseDir . date("Ymd") . '.log');
}
return $this;
}
public function getPath(): string
{
return $this->file;
}
public function setPath(string $file):self
{
$this->file = $file;
$folder = preg_replace('#^(.+)/[^/]+$#', '$1', $file);
if (!file_exists($folder)) {
mkdir($folder, 0777, true);
}
$this->logger = new FileAdapter($file);
return $this;
}
function __call($funName, $arguments)
{
if ($this->logger == null) {
$this->select();
}
if (in_array($funName, ['debug', 'error', 'info', 'notice', 'warning', 'alert', 'emergency',])
|| ($funName == 'log' && (!(is_int($arguments[0]) && $arguments[0] >= 0 && $arguments[0] <= 9)))
) {
if (is_array($arguments[0])) {
$arguments[0] = var_export($arguments[0], 1);
} else {
$arguments[0] = strval($arguments[0]);
}
$this->logger->$funName(...$arguments);
return $this;
} elseif ($funName == 'log' && is_int($arguments[0]) && $arguments[0] >= 0 && $arguments[0] <= 9) {
if (is_array($arguments[1])) {
$arguments[1] = var_export($arguments[1], 1);
} else {
$arguments[1] = strval($arguments[1]);
}
$this->logger->$funName(...$arguments);
return $this;
} else {
return $this->logger->$funName(...$arguments);
}
}
}
1、路径不能自动创建
2、打印消息非得要求是字符串。
自定义类如下:
<?php
use Phalcon\Logger\Adapter\File as FileAdapter;
use Phalcon\Logger\AdapterInterface;
/**
* 自定义 phalcon 日志类
*
* 解决两大痛点:
* 1、路径不能自动创建
* 2、打印消息非得要求是字符串。
*
* 使用方式
*
* $logger = SuperLogger::getInstance(SuperLogger::CLIENT_LOG);
* $logger
* ->info( 'This is a {message}',
* [
* 'message' => time()
* ] )
* ->log( time() )
* ->log(Logger::NOTICE, time() )
* ->error( ['aa'=>11111,'b'=>22222] );
*
* echo "读取日志:".$logger->getPath()."<br>";
* echo file_get_contents($logger->getPath());
*
* 也可以构造方法参数为空,再 select,或 setPath
*
* Class SuperLogger
*
* @method AdapterInterface setFormatter(FormatterInterface $formatter)
* @method FormatterInterface getFormatter()
* @method AdapterInterface setLogLevel($level)
* @method AdapterInterface log($type, $message = null, array $context = null)
* @method AdapterInterface debug($message, array $context = null)
* @method AdapterInterface error($message, array $context = null)
* @method AdapterInterface info($message, array $context = null)
* @method AdapterInterface notice($message, array $context = null)
* @method AdapterInterface warning($message, array $context = null)
* @method AdapterInterface alert($message, array $context = null)
* @method AdapterInterface emergency($message, array $context = null)
* @method AdapterInterface begin()
* @method AdapterInterface commit()
*
*
* @author yyy
*/
class SuperLogger
{
/**
* @var FileAdapter
*/
protected $logger;
protected $file; // 文件完整路径
protected $baseDir;// 预定义日志文件基路径
/**
* 这里自定义一些常量。日志类型。
*/
const HTTP_LOG = 1;
const CLIENT_LOG = 2;
const MENU_LOG = 3;
const PATH_ARR =[
self::HTTP_LOG => 'http/',
self::CLIENT_LOG => 'client/',
self::MENU_LOG => 'menu/',
];
/**
* 这里自定义默认日志根路径。
*
* @param mixed $file
*/
public function __construct( $file = null)
{
$this->baseDir = BASE_PATH . '/cache/logs/';
if ($file) {
if (is_int($file)) {
$this->select($file);
} else {
$this->setPath($file);
}
}
}
public static function getInstance( $file = null):self
{
return new self($file);
}
/**
* 选择不同的日志类型,自行修改。
*
* @param $type
* @return $this
*/
public function select(int $type=null):self
{
if (in_array( $type, array_keys( self::PATH_ARR) )) {
$this->setPath($this->baseDir . self::PATH_ARR[$type] . date("Ymd") . '.log');
} else {
$this->setPath($this->baseDir . date("Ymd") . '.log');
}
return $this;
}
public function getPath(): string
{
return $this->file;
}
public function setPath(string $file):self
{
$this->file = $file;
$folder = preg_replace('#^(.+)/[^/]+$#', '$1', $file);
if (!file_exists($folder)) {
mkdir($folder, 0777, true);
}
$this->logger = new FileAdapter($file);
return $this;
}
function __call($funName, $arguments)
{
if ($this->logger == null) {
$this->select();
}
if (in_array($funName, ['debug', 'error', 'info', 'notice', 'warning', 'alert', 'emergency',])
|| ($funName == 'log' && (!(is_int($arguments[0]) && $arguments[0] >= 0 && $arguments[0] <= 9)))
) {
if (is_array($arguments[0])) {
$arguments[0] = var_export($arguments[0], 1);
} else {
$arguments[0] = strval($arguments[0]);
}
$this->logger->$funName(...$arguments);
return $this;
} elseif ($funName == 'log' && is_int($arguments[0]) && $arguments[0] >= 0 && $arguments[0] <= 9) {
if (is_array($arguments[1])) {
$arguments[1] = var_export($arguments[1], 1);
} else {
$arguments[1] = strval($arguments[1]);
}
$this->logger->$funName(...$arguments);
return $this;
} else {
return $this->logger->$funName(...$arguments);
}
}
}
上一篇: 让php-fpm以root账号运行
下一篇: 反射api