1. 程式人生 > >nginx+lua openresty開發系列-(log日誌詳解)

nginx+lua openresty開發系列-(log日誌詳解)

最近搭建流媒體伺服器,其中涉及到一些http服務api的操作, 之前一直使用的是python django框架來處理這些資訊, 這次編譯的是nginx添加了lua模組, 就想著使用lua來完成這些功能, 減少服務的搭建。好久沒有lua開發了,現在突然發現好多東西都記不住了, 所以就藉著這次開發, 記錄下經歷的開發經歷, 及必備所需的東西, 以備後續查詢翻閱以及分享給大家。

日誌, 是一個開發必備的東西, 特別是伺服器,處理併發的伺服器所一定除錯要用到的。之前剛開始的時候還曾使用過使用檔案io建立, 檔案進行記錄相關日誌, 結果過程中出現的好多問題, 自己給自己弄得焦頭爛額,後來主攻了這部分內容,最近在寫lua介面的時候不知道怎麼用了, 查詢日誌型別的時候,沒有找到正確的地方, 日誌型別也只查到了error級別, 造成了困擾,所以才有了寫這系列的想法。

廢話少說, 在學習一個領域的時候一定要有相應好的學習的網址來學習, 不管誰寫的部落格,資料,還是最原始的比較好
https://github.com/openresty/lua-nginx-module
nginx 所有模組說明
http://nginx.org/en/docs/
這都是最原始的資料, 裡面也是最官方的說明, 內容非常好,必備網址
openresty是nginx lua的打包程式並對其做了優化, 所以openresty的說明也是官方,必備, 有一點特別好, 中國人自己寫的, 所以有中文版,對於像我這樣英文不怎麼好的人來說就是一個福音
https://legacy.gitbook.com/book/moonbingbing/openresty-best-practices/details


http://openresty.org/cn/
那搭建openresy nginx+lua開發環境呢,我本身是做流媒體開發的,所以我都是整體編譯的。環境搭建過程請看我之前部落格
https://blog.csdn.net/u012618915/article/details/81180421
以下是正題:

日誌級別:
ngx.STDERR 標準輸出
ngx.EMERG 緊急報錯
ngx.ALERT 報警
ngx.CRIT 嚴重,系統故障, 觸發運維告警系統
ngx.ERR 錯誤,業務不可恢復性錯誤
ngx.WARN 提醒, 業務中可忽略錯誤
ngx.NOTICE 提醒, 業務中比較重要資訊
ngx.INFO 資訊, 業務瑣碎日誌資訊, 包含不同情況判斷等
ngx.DEBUG 除錯

這些都是常量, 越往上等級越高。

函式原型
ngx.log(level, …)
基本都是在content階段使用
示例
ngx.log(ngx.ERR, “num:”, num)
ngx.log(ngx.INFO, ” string:”.. str)

注意 print語句是INFO級別

lua中日誌完成了, 那如何設定日誌格式,日誌格式呢, 那就需要使用nginx本身的log_format 進行設定了
log_format 屬於 ngx_http_log_module

示例:
log_format main ‘ r e m o t e a d d r remote_user [ t i m e l o c a l ] " request” ’
s t a t u s body_bytes_sent “ h t t p r e f e r e r http_user_agent" "$http_x_forwarded_for”’;
這是我使用的日誌格式
語法:
log_format name [escape=default|json|none] string …;

預設的log_format 為
log_format combined ‘ r e m o t e a d d r remote_user [ t i m e l o c a l ] request" s t a t u s body_bytes_sent ’
‘” h t t p r e f e r e r "" http_user_agent”’;

現在開始設定日誌輸出level, 那如何設定日誌級別呢, 那就需要使用nginx本身的error_log進行設定了,
error_log屬於ngx_core_module
示例:
error_log logs/error.log error;
語法
error_log file [level];
預設的error_log為
error_log logs/error.log error;
上下文為:
main, http, mail, stream, server, location
level 等級
debug, info, notice, warn, error, crit, alert, emerg
大於等於設定等級的日誌均會被記錄
若要設定debug level則在編譯的時候新增–with-debug 即 ./configure –with-debug

以上為日誌基本常用的功能均已經介紹完,以下為高階功能, 不常用
遠端日誌:
lua-resty-logger-socket
以非阻塞 IO 方式推送 access log 到遠端伺服器上。 對遠端伺服器的要求是支援 syslog-ng 的日誌服務
示例
log_by_lua_block {
local logger = require “resty.logger.socket”
if not logger.initted() then
local ok, err = logger.init{
host = ‘xxx’,
port = 1234,
flush_limit = 1234,
drop_limit = 5678,
}
if not ok then
ngx.log(ngx.ERR, “failed to initialize the logger: “,err)
return
end
end
local bytes, err = logger.log(msg)
if err then
ngx.log(ngx.ERR, “failed to log message: “, err)
return
end
優點:
基於 cosocket 非阻塞 IO 實現
日誌累計到一定量, 集體提交, 增加網路傳輸利用率
短時間的網路抖動, 自動容錯
日誌累計到一定量, 如果沒有傳輸完畢, 直接丟棄
日誌傳輸過程完全不落地, 沒有任何磁碟 IO 消耗

選擇記錄客戶端debug日誌 加編譯選項(–with-debug)
示例
events {
debug_connection 192.168.1.1;
debug_connection 192.168.10.0/24;
}
日誌為迴圈記憶體
error_log memory:32m debug;
這樣做不影響高併發情況

gdb時可以這麼用
set $log = ngx_cycle->log

while l o g > w r i t e r ! = n g x l o g m e m o r y w r i t e r s e t log = $log->next
end

set b u f = ( n g x l o g m e m o r y b u f t ) log->wdata
dump binary memory debug_log.txt b u f > s t a r t buf->end

如果覺得有用, 請關注我的部落格!!!!
做專注最接地氣流媒體相關內容!!!!
我以後也會盡可能,儘自己最大水平持續更新!!!!