Laravel 数据库日志 - 最佳实践是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25910108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:07:35  来源:igfitidea点击:

Laravel Database Log - what is the best practice

phpmysqllogginglaravelpdo

提问by Hrusdik

I have a pretty common problem I think, but I can't find any "best practice" answer to it.

我认为我有一个非常普遍的问题,但我找不到任何“最佳实践”的答案。

I developped a lot of small web apps based on a home made framework for my clients. Those apps are used only in a very specific business related context, they are used for something like a mounth in a year by an average of ten users.

我为我的客户开发了许多基于自制框架的小型网络应用程序。这些应用程序仅在非常特定的业务相关环境中使用,平均每年有 10 名用户使用它们。

Each app has a very diffrent purpose and "technology" behind it (sometimes it's Zend modules, TCPDF, etc...). Of course, once in a while users find some minor bugs (not handled exceptions, not set arrays ...).

每个应用程序背后都有非常不同的目的和“技术”(有时是 Zend 模块、TCPDF 等……)。当然,偶尔用户会发现一些小错误(未处理异常,未设置数组...)。

The problem is, that users won't communicate about it for days or weeks, so when I have to debug, sometimes I don't have all the information I need (like, how to reproduce it, which route it was, etc ...).

问题是,用户在几天或几周内都不会就它进行交流,所以当我必须调试时,有时我没有我需要的所有信息(例如,如何重现它,它是哪条路线,等等。 ...)。

For my home made framework (very Laravel-like) I made some functions that handles those errors (using set_error_handler, register_shutdown_function, ...). Basicly those function will create some log entry in client's MySQL database.

对于我自制的框架(非常类似于 Laravel),我制作了一些处理这些错误的函数(使用 set_error_handler、register_shutdown_function 等)。基本上这些函数会在客户端的 MySQL 数据库中创建一些日志条目。

I use those functions for two purposes :

我将这些函数用于两个目的:

  1. Log user actions AND php errors + context (routes, controllers, some data flow, php predefined variables, ...)
  2. Warn me when something goes wrong (by mail, SMS, etc ...)
  1. 记录用户操作和 php 错误 + 上下文(路由、控制器、一些数据流、php 预定义变量,...)
  2. 出现问题时警告我(通过邮件、短信等...)

The only way I can analyse all these logs efficientlyis when they are in a database (i have a JQuery table rendring them optimized for faster reading and tracking). Otherwise, i would just use text files, but I can't analyse them so fast (too much information).

我可以分析所有这些日志的唯一方法有效地是,当他们在一个数据库(我有一个jQuery表rendring他们更快的读取和跟踪优化)。否则,我只会使用文本文件,但我无法如此快速地分析它们(信息太多)。

Question

I just started a project with Laravel (which I'll be using for all my projects now), after some documentation reading I've found that Laravel uses Monolog for logs. BUT, Monolog don't have a PDO handler, some people may say that using MySQL for logs is a very bad idea (Can Laravel 4 log to a MySQL database?) and I totally agree with them. But it is the only way i've found to do efficient bug correction when working with a lot of small projects with a limited time to dedicate to it.

我刚刚用 Laravel 开始了一个项目(我现在将在我的所有项目中使用它),在阅读了一些文档之后,我发现 Laravel 使用 Monolog 来记录日志。但是,Monolog 没有 PDO 处理程序,有些人可能会说将 MySQL 用于日志是一个非常糟糕的主意(Laravel 4 可以登录到 MySQL 数据库吗?),我完全同意他们的看法。但这是我发现在处理大量小项目且时间有限的情况下进行有效错误纠正的唯一方法。

Note : My clients only have MySQL, I don't have the time to use unit testing (small company, no money for that).

注意:我的客户只有 MySQL,我没有时间使用单元测试(小公司,没有钱)。

Is there a better solution for my problem (log users activities AND php errors in a structured way, mail/SMS notification when errors are catched) using some Laravel magic I don't know about yet ?

是否有更好的解决方案来解决我的问题(以结构化的方式记录用户活动和 php 错误,捕获错误时的邮件/短信通知)使用一些我还不知道的 Laravel 魔法?

采纳答案by Dwight

Sure, there are number of tools that can monitor your log files for you and then collate them into meaningful information. They can group similar errors and provide you with detailed data on the kinds of requests that cause them. Better yet, you can elect to pass user data along with errors so you can see exactly what is causing issues.

当然,有许多工具可以为您监控日志文件,然后将它们整理成有意义的信息。他们可以对类似的错误进行分组,并为您提供有关导致它们的请求类型的详细数据。更好的是,您可以选择将用户数据与错误一起传递,以便您可以准确查看导致问题的原因。

Here are some that I've come across in my time

这是我在我的时间遇到​​的一些