69 lines
1.9 KiB
PHP
69 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace common\helpers;
|
|
|
|
class LogHelper extends \yii\helpers\StringHelper
|
|
{
|
|
/**
|
|
* 记录日志
|
|
*
|
|
* @param mixed $message
|
|
* @param string $path
|
|
* @param string $filename
|
|
* @return void
|
|
*/
|
|
public static function log_error($message = '', string $path = '', string $filename = '')
|
|
{
|
|
self::log_level('error', $message, $path, $filename);
|
|
}
|
|
|
|
/**
|
|
* 记录日志
|
|
*
|
|
* @param mixed $message
|
|
* @param string $path
|
|
* @param string $filename
|
|
* @return void
|
|
*/
|
|
public static function log_info($message = '', string $path = '', string $filename = '')
|
|
{
|
|
self::log_level('info', $message, $path, $filename);
|
|
}
|
|
|
|
/**
|
|
* 记录日志
|
|
*
|
|
* @param string $level
|
|
* @param mixed $message
|
|
* @param string $path
|
|
* @param string $filename
|
|
* @return void
|
|
*/
|
|
public static function log_level(string $level = '', $message = '', string $path = '', string $filename = '')
|
|
{
|
|
$logger = self::logger($path, $filename);
|
|
$logger->log($level, $message);
|
|
}
|
|
|
|
/**
|
|
* @param string $path
|
|
* @param string $filename
|
|
* @return \Yiisoft\Log\Logger
|
|
*/
|
|
protected static function logger(string $path = '', string $filename = ''): \Yiisoft\Log\Logger
|
|
{
|
|
if (!$path) {
|
|
$path = 'sample';
|
|
}
|
|
if (!$filename) {
|
|
$filename = 'log';
|
|
}
|
|
$filepath = \Yii::$app->basePath . '/runtime/logs/' . $path . '/' . $filename . '-' . date('Y-m-d') . '.log';
|
|
$fileTarget = new \Yiisoft\Log\Target\File\FileTarget($filepath);
|
|
$fileTarget->setFormat(static function (\Yiisoft\Log\Message $message) {
|
|
return '[' . date('Y-m-d H:i:s') . "] [{$message->level()}] {$message->message()}";
|
|
});
|
|
return new \Yiisoft\Log\Logger([$fileTarget]);
|
|
}
|
|
}
|