13. Log x Why x How

为什麽要写日志(Log)

Log 主要是要记录错误以及警告,而不是让工程师写心情小语。

为什麽要记录错误呢?有错就直接喷给使用者,让使用者通知工程师、用客服信箱纪录错误不就好啦。

这篇文章要告诉各位大老板,为什麽应该要有自己的通知系统与纪录系统。

当发生紧急状况例如服务崩溃时,通知系统可以立刻通知 on call 工程师,让他知道哪里有问题、出什麽问题,这样他可以尽快掌握状况。而身为老板的你也就可以传 Line 跟工程师说这是你负责的部分,如果你休假也没在干嘛就负责修一下你自己写的 bug。

纪录系统可以帮助我们记录一些重要资讯,以便日後出错时有迹可循。例如,还有对接外部 API 时出了状况,如果有这些 log,我们就可以自信地把锅甩回去给对方的工程师。

怎麽开始写日志

被动的日记
当程序抛出错误(Error)时,laravel helper 会自动记录 log 预设是 stack,会存在 app/storage/logs/laravel.log
https://laravel-guide.readthedocs.io/en/latest/errors/

LOG_CHANNEL=daily

可以将 .env 的 LOG_CHANEL 改成 daily,这样每天的 log 会在不同档案。

主动写日记
Laravel logger 预设是纪录 Warning 以上的错误(Error 在 Warning 之上,所以会被记录),如果你想要更多资讯需要自己埋。

Laravel 的日志提供了符合 RFC 5424 定义的八种日志级别:emergency、alert、critical、error、warning、notice、info 和 debug。

八个级别不一定全都用到,一般只用到 4~5 个,也可以参考一下别人的使用情境

  • Trace - Only when I would be "tracing" the code and trying to find one part of a function specifically.
  • Debug - Information that is diagnostically helpful to people more than just developers (IT, sysadmins, etc.).
  • Info - Generally useful information to log (service start/stop, configuration assumptions, etc). Info I want to always have available but usually don't care about under normal circumstances. This is my out-of-the-box config level.
  • Warn - Anything that can potentially cause application oddities, but for which I am automatically recovering. (Such as switching from a primary to backup server, retrying an operation, missing secondary data, etc.)
  • Error - Any error which is fatal to the operation, but not the service or application (can't open a required file, missing data, etc.). These errors will force user (administrator, or direct user) intervention. These are usually reserved (in my apps) for incorrect connection strings, missing services, etc.
  • Fatal - Any error that is forcing a shutdown of the service or application to prevent data loss (or further data loss). I reserve these only for the most heinous errors and situations where there is guaranteed to have been data corruption or loss.

Ref


<<:  Day13罐头变身日本料理-鳗鱼盖饭

>>:  EP13 - [TDD] 使用工厂模式让测试更简单

[Angular] Day5. Lifecycle hooks

在 Angular 的 Component 中有一个生命周期,当 Angular 实例化这个 Com...

D17 - 从TiDB将资料同步出去

TiDB可以同步MySQL的资料异动,那麽能不能反过来让其他DB同步随着TiDB异动呢。 答案是可以...

学习Python纪录Day12 - Python模组

Python模组 python模组就是单一python档案(.py档) 套件是一个目录中含有多个模组...

并行程序的潜在问题 (一)

建立 Thread 的成本远低於 Process 的成本,执行 Context switch 时的效...

DAY19 MongoDB Oplog 是什麽?迈向高手之路

DAY19 MongoDB Oplog 到底是什麽? oplog 是什麽? 如果你的 MongoDB...