HAProxy 日志管理
一、HAProxy 配置
HAProxy 日志采用默認配置:
cat /etc/haproxy/haproxy.cfg
globallog /dev/log local0log /dev/log local1 notice注意:
- 如果后臺服務不是 http,而是 redis 等,則對應的 listen 需要覆蓋指定:option tcplog。
- 日志的 level:local0~local7,16~23保留為本地使用,其中:
- 0:emerg,系統不可用;
- 1:alert,必須馬上采取行動的事件;
- 2:crit,關鍵的事件;
- 3:err,錯誤事件;
- 4:warning,警告事件;
- 5:notice,普通但重要的事件;
- 6:info,有用的信息;
- 7:debug,調試信息。
二、修改 /etc/rsyslog.d/49-haproxy.conf
原文件如下:
注釋紅線部分,并在其下面添加:
注意:
-
文件名不一定是 49-haproxy.conf,不過都沒關系,只要文件后綴是“.conf”,并且文件位于目錄:/etc/rsyslog.d/ 就可以。這是因為服務 rsyslog 的配置文件:/etc/rsyslog.conf 中有一句話:
$IncludeConfig /etc/rsyslog.d/*.conf
所以,如果我們要自定義某個服務的日志,最好在 /etc/rsyslog.d/ 目錄下創建相應的文件,而不是直接修改 /etc/rsyslog.conf。
-
由于 HAProxy 的日志是采用 UDP 傳輸,所以需啟用 rsyslog 的 udp監聽。
-
兩種定義 template 路徑的語句都是正確的,但無論使用哪一種格式,在引用 template 路徑時,必須在 template 名稱前加一個 “?”。
-
最后一行:&~,如果不加這條的配置,則除了將日志寫入自定義文件之外,同時會寫入 /var/log/messages。
重啟 rsyslog 與 HAProxy:
systemctl restart rsyslog
systemctl restart haproxy
查看日志文件:
三、設置日志內容格式
HAProxy 默認日志比較雜亂,字段含義也比較難懂。
可使用 log-format,對格式進行自定義:
注意:
-
tcplog 與 httplog 模式下,某些字段不通用,注意區分,否則 HAProxy 可能無法啟動;
-
若某個后端不想寫日志,比如 HAProxy web 管理平臺,每次刷新會寫兩條記錄,可以將“log global”只放到需要寫日志的后端配置下;
-
默認情況下,tcplog 與 httplog 模式只會在連接中斷后寫日志,以便能夠在日志中記錄時間和字節等信息。這樣可能導致,tcp 連接只要不關閉,永遠不會寫日志。解決方法:option logasap,表示只要服務發送了完整的 header,就馬上寫日志。缺點:
- 日志中可能缺少某些數據;
- 連接關閉時,不會寫日志;
-
日志格式:
HAProxy 配置手冊Rvarfield name (8.2.2 and 8.2.3 for description)type %o special variable, apply flags on all next var %B bytes_read (from server to client) numeric H %CC captured_request_cookie string H %CS captured_response_cookie string %H hostname string H %HM HTTP method (ex: POST) string H %HP HTTP request URI without query string (path) string H %HQ HTTP request URI query string (ex: ?bar=baz) string H %HU HTTP request URI (ex: /foo?bar=baz) string H %HV HTTP version (ex: HTTP/1.0) string %ID unique-id string %ST status_code numeric %T gmt_date_time date %Tc Tc numeric %Td Td = Tt - (Tq + Tw + Tc + Tr) numeric %Tl local_date_time date H %Tq Tq numeric H %Tr Tr numeric %Ts timestamp numeric %Tt Tt numeric %Tw Tw numeric %U bytes_uploaded (from client to server) numeric %ac actconn numeric %b backend_name string %bc beconn (backend concurrent connections) numeric %bi backend_source_ip (connecting address) IP %bp backend_source_port (connecting address) numeric %bq backend_queue numeric %ci client_ip (accepted address) IP %cp client_port (accepted address) numeric %f frontend_name string %fc feconn (frontend concurrent connections) numeric %fi frontend_ip (accepting address) IP %fp frontend_port (accepting address) numeric %ft frontend_name_transport (’~’ suffix for SSL) string %lc frontend_log_counter numeric %hr captured_request_headers default style string %hrl captured_request_headers CLF style string list %hs captured_response_headers default style string %hsl captured_response_headers CLF style string list %ms accept date milliseconds (left-padded with 0) numeric %pid PID numeric H %r http_request string %rc retries numeric %rt request_counter (HTTP req or TCP session) numeric %s server_name string %sc srv_conn (server concurrent connections) numeric %si server_IP (target address) IP %sp server_port (target address) numeric %sq srv_queue numeric S %sslc ssl_ciphers (ex: AES-SHA) string S %sslv ssl_version (ex: TLSv1) string %t date_time (with millisecond resolution) date %ts termination_state string H %tsc termination_state with cookie status string
總結
以上是生活随笔為你收集整理的HAProxy 日志管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将Sublime Text添加到鼠标右键
- 下一篇: win10卸载电脑管家就蓝屏_win10