Log.php 3 KB
Newer Older
王源's avatar
王源 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
<?php
/**
 * Created by PhpStorm.
 * User: Zero
 * Date: 2021/06/09
 * Time: 10:14:32
 */

namespace Meibuyu\Micro\Tools;

use Hyperf\Logger\Logger;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\FormattableHandlerInterface;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\RotatingFileHandler;
use Psr\Log\LoggerInterface;

/**
 * Class Log
 * @package App\Services
 * @method static emergency($message, string $name = 'app')
 * @method static alert($message, string $name = 'app')
 * @method static critical($message, string $name = 'app')
 * @method static error($message, string $name = 'app')
 * @method static warning($message, string $name = 'app')
 * @method static notice($message, string $name = 'app')
 * @method static info($message, string $name = 'app')
 * @method static debug($message, string $name = 'app')
 */
class Log
{

    /**
     * @var LoggerInterface
     */
    protected static $logs;

    /**
     * @return LoggerInterface
     */
    public static function log($channel = 'app')
    {
        if (!isset(self::$logs[$channel])) {
            self::$logs[$channel] = make(Logger::class, [
                'name' => $channel,
                'handlers' => self::handlers($channel),
            ]);
        }
        return self::$logs[$channel];
    }

    protected static function handlers($channel): array
    {
        $handlerConfigs = [
            [
                'class' => RotatingFileHandler::class,
                'constructor' => [
                    'filename' => BASE_PATH . "/runtime/logs/$channel.log",
                    'level' => Logger::DEBUG,
                ],
                'formatter' => [
                    'class' => LineFormatter::class,
                    'constructor' => [
                        'format' => "[%datetime%] %channel%.%level_name%: %message%\n",
                        'dateFormat' => 'Y-m-d H:i:s',
                        'allowInlineLineBreaks' => true,
                    ],
                ],
            ],
        ];
        $handlers = [];
        foreach ($handlerConfigs as $value) {
            $formatterConfig = $value['formatter'];
            /** @var HandlerInterface $handler */
            $handler = make($value['class'], $value['constructor']);
            if ($handler instanceof FormattableHandlerInterface) {
                $formatterClass = $formatterConfig['class'];
                $formatterConstructor = $formatterConfig['constructor'];
                /** @var FormatterInterface $formatter */
                $formatter = make($formatterClass, $formatterConstructor);
                $handler->setFormatter($formatter);
            }
            $handlers[] = $handler;
        }
        return $handlers;
    }

    public static function __callStatic($name, $arguments)
    {
        $msg = $arguments[0];
92 93 94
        if (is_array($msg)) {
            $msg = var_export($msg, true);
        }
王源's avatar
王源 committed
95
        $channel = $arguments[1] ?? 'app';
王源's avatar
王源 committed
96 97 98 99
        return static::log($channel)->{$name}($msg);
    }

}