简体   繁体   English

Laravel Log useFiles方法使Log写入多个文件

[英]Laravel Log useFiles method is making Log write in multiple files

I am using Laravel Log Facade in my app. 我在我的应用程序中使用Laravel Log Facade And I have several services like Mandrill, Twilio, Stripe, etc., that need to be logged in separate file. 我有几个服务,如Mandrill,Twilio,Stripe等,需要记录在单独的文件中。 But when I use Log::useFiles() to set separate file for one of the service wrapper class, like this: 但是当我使用Log :: useFiles()为其中一个服务包装类设置单独的文件时,如下所示:

Class Mailer
{
    static function init()
    {
        Log::useFiles(storage_path('logs/mandrill-'.date("Y-m-d").'.log'));
    }

    static function send()
    {
        // some code here...

        Log::error("Email not sent");
    }
}

And I am ending up with log being written in both Laravel log file, and this Mandrill log file. 我最终会在Laravel日志文件和此Mandrill日志文件中写入日志。

Is there a way to tell Log to write logs only in one file? 有没有办法告诉Log只在一个文件中写日志?

It's generally strange that it does that, because when I use directly Monolog , it writes only in one file, as it should. 它通常很奇怪,因为当我直接使用Monolog时 ,它只能在一个文件中写入。 As far as I know Log Facade is using Monolog. 据我所知,Log Facade正在使用Monolog。

First of all, keep in mind that if you change the log handlers in your Mailer class you'll change them for the whole application. 首先,请记住,如果您更改Mailer类中的日志处理程序,您将为整个应用程序更改它们。

Secondly, the reason that after your changes you get logs written to 2 files is that useFiles() does not overwrite the default log handler but adds a new handler to the handlers that Monolog will use. 其次,在您更改后将日志写入2个文件的原因是useFiles()不会覆盖默认的日志处理程序,但会向Monolog将使用的处理程序添加新的处理程序。 Therefore, you just add a second handler to the list and both of them handle the log message by saving them into different files. 因此,您只需向列表中添加第二个处理程序,它们都会通过将日志消息保存到不同的文件来处理日志消息。

Thirdly, Laravel's Log facade does not provide a way to replace the default handler - if you want to use it you need to use Monolog directly. 第三,Laravel的Log facade没有提供替换默认处理程序的方法 - 如果你想使用它,你需要直接使用Monolog You can access it by calling Log::getMonolog() . 您可以通过调用Log :: getMonolog()来访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM