目錄

目錄

Log 層級的小筆記

目錄

最近需要對 log 做點調整,因此研究了一下 RFC 對 log 的想法為何。

首先先列出跟 log 有關 RFC 為何:

參考別人的文章,簡單來說原本大家就是各自做,後來出了 RFC 3164 提醒大家應該怎麼做,但它用 UDP,後來出了 RFC 3195 說要用 TCP;在更後來出了 RFC 5424 則是為了取代 RFC 3164 而出的。

只是今天要筆記的主要是 level

RFC 5424 使用了嚴重度(Severity)的關鍵字作為層級的標準,總共分成 8 個層級,與簡單的說明如下:

Numerical CodeSeverity
0Emergency: system is unusable
1Alert: action must be taken immediately
2Critical: critical conditions
3Error: error conditions
4Warning: warning conditions
5Notice: normal but significant condition
6Informational: informational messages
7Debug: debug-level messages

參考 https://tools.ietf.org/html/rfc5424#section-6.2.1

原本預期能在 RFC 上看到不同層級的說明,但它又說(https://tools.ietf.org/html/rfc5424#appendix-A.3)

Because severities are very subjective, a relay or collector should not assume that all originators have the same definition of severity.

確實每個人或每個應用程式,對於錯誤的感受不同,分類就會有所不同。

不過還好它對 Emergency 和 Debug 倒是有建議:

Most importantly, messages designed to enable debugging or testing of software should be assigned Severity 7. Severity 0 should be reserved for messages of very high importance (like serious hardware failures or imminent power failure).

任何為了除錯或測試階段使用的 log,都是 7;而 0 應該保留給系統人員決定。

而再回到各層級的定義,因為 PSR-3 也有提供 comment 說明,綜合 RFC 上的 comment,這裡主觀的總結如下:

SeveritySeverityExample
Emergency系統不可用硬體錯誤、停電
Alert必須要立即處理的錯誤,需要觸發立即訊息通知相關人員處理資料庫當機
Critical重要錯誤,需要被注意的錯誤某個元件不能使用、非預期的例外
Error一般錯誤,不需要立即處理,但需要被記錄與監視未定義的變數(註)
Warning特殊訊息,但它不是錯誤API 接受資料規格帶有攻擊資訊(如 XSS),但可被過濾成可用的資料
Notice正常但需被注意的訊息API 接受資料規格不正確,但可被過濾成可用的資料
Informational一般訊息SQL 記錄
Debug僅在除錯或測試階段使用的訊息除錯訊息

註:對 PHP 來說,未定義變數可能是 notice 錯誤,但對上線來說,可能會是 error 的來源。