如何使用Laravel和Monlog创建旋转日志文件 [英] How to create rotating log file with Laravel and Monlog

查看:130
本文介绍了如何使用Laravel和Monlog创建旋转日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Monolog在Laravel中创建自己的旋转日志文件,但是,文件旋转无法正常工作,我也不知道为什么.

I'm trying to create my own rotating log file in Laravel using Monolog, however, the file rotation is not working and I don't know why.

我创建了一个artisan命令,该命令每天运行一次,并记录其活动日志.我希望此文件的旧版本在2天后删除.换句话说,只有今天的运行日志以及昨天的运行日志.

I've created an artisan command that runs once per day, and keeps a log of it's activity. I want old versions of this file to be deleted after 2 days. In other words only the log of today's run, as well as yesterday's run should exist.

在工匠命令的开头,我有以下代码:

At the beginning of my artisan command, I have the following code:

$log = new Logger('MyCustomLog');
$log->pushHandler(new RotatingFileHandler(storage_path().'/logs/mycustomlog.log'), 2);

然后,在整个命令中,我将信息记录到其中:

Then, throughout the command, I log information to it:

$log->addInfo('Info to log');

对我来说,这似乎很简单,但是根本行不通.日志文件已正确生成,但是它们从未被删除.我在app/storage/logs文件夹中看到以下内容:

It seemed pretty straightforward to me, but it's simply not working. The log files are being generated correctly, but they are never getting deleted. I see the following in my app/storage/logs folder:

mycustomlog-2015-01-30.log
mycustomlog-2015-01-31.log
mycustomlog-2015-02-01.log
mycustomlog-2015-02-02.log
mycustomlog-2015-02-03.log

我希望只能看到最后2个文件.我在这里做什么错了?

I would expect to see only the last 2 files. What am I doing wrong here?

推荐答案

您的一个支架确实有点不合适.您必须将日志文件的数量传递给RotatingFileHandler的构造函数,而不是作为pushHandler()的第二个参数:

One of your bracket did get a bit out of place. You have to pass the number of log files to the constructor of RotatingFileHandler and not as second argument to pushHandler():

$log->pushHandler(new RotatingFileHandler(storage_path().'/logs/mycustomlog.log', 2));
//                                                                                ^

这篇关于如何使用Laravel和Monlog创建旋转日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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