Laravel Log useFiles方法使Log写入多个文件 [英] Laravel Log useFiles method is making Log write in multiple files

查看:1199
本文介绍了Laravel Log useFiles方法使Log写入多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用了 Laravel Log Facade .我有一些服务,例如Mandrill,Twilio,Stripe等,需要将其记录在单独的文件中.但是,当我使用Log :: useFiles()为服务包装类之一设置单独的文件时,如下所示:

I am using Laravel Log Facade in my app. And I have several services like Mandrill, Twilio, Stripe, etc., that need to be logged in separate file. But when I use Log::useFiles() to set separate file for one of the service wrapper class, like this:

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");
    }
}

最后我要在Laravel日志文件和此Mandrill日志文件中写入日志.

And I am ending up with log being written in both Laravel log file, and this Mandrill log file.

是否可以告诉Log仅将日志写入一个文件中?

Is there a way to tell Log to write logs only in one file?

这样做通常是很奇怪的,因为当我直接使用 Monolog 时,它只写一个文件.据我所知,Log Facade正在使用Monolog.

It's generally strange that it does that, because when I use directly Monolog, it writes only in one file, as it should. As far as I know Log Facade is using Monolog.

推荐答案

首先,请记住,如果更改 Mailer 类中的日志处理程序,则将整个更改应用程序.

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.

第二,更改后,您将日志写入2个文件的原因是 useFiles()不会覆盖默认的日志处理程序,而是向其中添加了新的处理程序 Monolog 将使用的处理程序.因此,您只需在列表中添加第二个处理程序,它们两个都通过将日志消息保存到其他文件中来处理日志消息.

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. Therefore, you just add a second handler to the list and both of them handle the log message by saving them into different files.

第三,Laravel的 Log 门面无法提供替换默认处理程序的方法-如果要使用它,则需要直接使用 Monolog .您可以通过调用 Log :: getMonolog()来访问它.

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. You can access it by calling Log::getMonolog().

这篇关于Laravel Log useFiles方法使Log写入多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆