Home > Backend Development > PHP Tutorial > Laravel log saving directory problem;

Laravel log saving directory problem;

WBOY
Release: 2023-03-02 10:26:01
Original
1505 people have browsed it

laravel的日志保存目录能不能不保存到 storage/logs 目录下?怎么修改?
可不可以自定义文件名?
按照文档配置按日期分割,不管用!还是在laravel.log 里面追加。

Laravel log saving directory problem;

回复内容:

laravel的日志保存目录能不能不保存到 storage/logs 目录下?怎么修改?
可不可以自定义文件名?
按照文档配置按日期分割,不管用!还是在laravel.log 里面追加。

Laravel log saving directory problem;

自定义日志

Method 1->简单粗暴

<code class="php">use Monolog\Handler\StreamHandler;
use Monolog\Logger;

$log = new Logger('vikin');

$log->pushHandler(
    new StreamHandler(
        storage_path('logs/vikin.log'), 
        Logger::INFO
    )
);

$log->addInfo("test");</code>
Copy after login

Method2->基于ConfigureLogging基类

1、可以使用该方法对laravel原有日志覆盖【一个info,其他级别可自行扩展】

2、作为一个laravel扩展,在需要单独记录日志的地方记录;

1、创建一个类,继承自ConfigureLogging
<code class="php">namespace 你自定义命名空间\ConfigureLogging;

use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseConfigureLogging;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

class CustomLog extends BaseConfigureLogging
{
    protected function configureSingleHandler(Application $app, Writer $log)
    {
        // 同方法1一样, 设置日志路径、设置日志级别
        $path = storage_path('logs/vikin.log');
        $level = Logger::INFO;
        $logStreamHandler = new StreamHandler($path, $level);

        // 日志格式:使用laravel原有格式:
        // the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
        $format = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
        $formatter = new LineFormatter($format);
        $logStreamHandler->setFormatter($formatter);

        // 输出日志
        $logger = $log->getMonolog();
        $logger->pushHandler($logStreamHandler);
    }
}</code>
Copy after login

重写configureSingleHandler就相当于重写了一个日志处理器;

3、自动加载

修改根目录下的composer.json文件

<code class="json">"psr-4": {
    "App\\": "app/",
    "你自定义命名空间\\": "文件路径/"
}</code>
Copy after login
4、覆盖

/app/Http/Kernel.php文件中,扩展Kernel类的构造方法

<code class="php">use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Events\Dispatcher;

public function __construct(Application $app, Dispatcher $events)
{
    parent::__construct($app, $events);

    array_walk($this->bootstrappers, function(&$bootstrapper)
    {
        if($bootstrapper === 'Illuminate\Foundation\Bootstrap\ConfigureLogging')
        {
            //替换为我们自定义的日志处理器
            $bootstrapper = '你自定义命名空间\ConfigureLogging';
        }
    });
}</code>
Copy after login
5、使用
<code class="php">Log::info('自定义log处理器');</code>
Copy after login
6、扩展

创建一个ServiceProvider和facade(个人习惯喜欢使用facade);添加至app config中,使用composer自动加载即可;

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template