skip navigation
skip mega-menu

对于我们来说,在我们的生产网站上发生的任何错误都必须得到通知.

能够搜索我们所有的服务器日志也是非常有用的, 查找错误的原因, or just to keep an eye on things.

经过多年使用大量的系统, 这是我们在所有大型Laravel项目中使用的配置. 我们选择了两个第三方服务:Airbrake, for error monitoring, and Papertrail, for log file analysis.

Airbrake

有各种各样的服务被设计用来捕获和存储来自web应用程序的错误; Sentry, Rollbar, BugSnag, Raygun and many more.

然而,尝试了以上所有,我们最喜欢的是空气制动.

Setting up an account is straightforward; simply visit http://airbrake.io/ and click "Get Started". 他们要求你提供一些个人信息,然后邀请你创建你的第一个项目.

请注意,当你第一次在Airbrake中创建一个项目,并表明你正在使用Laravel, you're directed to this page:

本页上的说明不正确, and should be disregarded!

如果你在主Airbrake网站上搜索文档,你可能会找到这个页面:

http://airbrake.io / docs / installing-airbrake / installing-airbrake-in-a-laravel-application /

This page is also incorrect! Ignore it.

This doesn't bode well, but in fact, Airbrake is excellent, 一旦安装正确,就能很好地工作. 正确的说明(截至2020年6月10日)如下:

http://github.com/TheoKouzelis/laravel-airbrake

我们在这里就不重复说明了,但它们很容易遵循. Note that you will be adding 'airbrake' to your log 'stack'; see below for more information on this.

Also, 气闸(相对于其他类似的服务), 例如Rollbar)似乎不会记录使用Laravel创建的事件 Log:: facade. However, we don't find that this is an issue; we prefer to use Airbrake for actual errors, 和Papertrail来审查日志信息.

Papertrail

Papertrail本质上是将日志文件托管在云中. 如果您运行的是基于容器的服务器(重新构建服务器时会覆盖本地存储),这将非常方便。, 但在标准服务器上也容易得多. 而不是使用grep或类似的查询静态日志文件, 您可以使用Papertrail web界面快速轻松地搜索日志.

Papertrail非常容易与Laravel集成. Create an account at papertrailapp.com,你会得到一个网址和项目编号. For example, logs5.papertrailapp.com and 12345. 然后将它们添加到中的“papertrail”数组中 config/logging.php:


'papertrail' => [  
            'driver' => 'monolog',  
            'level' => 'debug',  
            'handler' => SyslogUdpHandler::class,   
            'handler_with' => [  
                'host' => 'logs5.papertrailapp.com',  
                'port' => '12345'  
            ],  
        ],   
}

然后,您可以将“papertrail”添加到日志堆栈中(见下文)。.

Logfiles

拥有静态日志文件可能仍然很有用. 是否使用“每日”文件(Laravel每天创建单独的日志文件), 或者一个“单一”文件(即一个巨大的日志文件,随着时间的推移而增长)是由你决定的. 无论哪种方式,您都可以将相关的间隔添加到日志堆栈中(见下文).

The log stack

这是告诉应用程序如何记录所有错误的地方. 如果你想使用Airbrake,和Papertrail,和个人的每日日志文件,你的配置/日志.php file will look like this:

{
'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['airbrake', 'papertrail', 'daily'],
            'ignore_exceptions' => false,
        ],
}

Done

That's it! Once this code is in place, 空气制动系统会通知你任何新的错误发生, Papertrail会将你所有的日志文件存储在云端. 您的日志将保留在服务器上,以防万一.

A bonus tip

Laravel can struggle with log permissions sometimes; the webserver might create a file that an artisan命令不能更新,反之亦然.

这可以通过确保web服务器的用户帐户(例如 www-data)与命令行用户属于同一组,等等. However, this can be a bit fiddly.

Instead, 在Laravel中有一个配置设置,没有太多文档记录, but which is very helpful. 在配置“每日”日志文件(或, if you prefer to use 'single'), you can set an option for permission, like so:

'daily' => [
    'driver' => 'daily',
    'path' => storage_path('logs/laravel.log'),
    'level' => 'debug',
    'days' => 14,
    'permission' => 0666
],

Setting this option to 0666 表示日志文件可由任何用户写入.

从技术上讲,这里可能存在一些安全问题... but really, they're pretty minimal. 如果与其他用户共享服务器可能不明智,但实际上,现在谁会这样做呢? For convenience, 0666 works very well.

Subscribe to our newsletter

Sign up here